转载自:https://analysistabs.com/excel-vba/interact-with-other-applications/

Interact with PowerPoint from Excel VBA

The following code will show you how to deal and interact with PowerPoint. We can create PowerPoint presentation from Excel or modify the existing presentation using Excel VBA.

Add Reference:Microsoft PowerPoint Object Library

Sub sbPowePoint_SendDataFromExcelToPPT()
'Declarations
Dim oPPT As PowerPoint.Application
Dim oPPres As PowerPoint.Presentation
Dim oPSlide As PowerPoint.Slide
Dim sText As String
'Open PowerPoint
Set oPPT = New PowerPoint.Application
Set oPPres = oPPT.Presentations.Add
oPPT.Visible = True
'Add a Slide
Set oPSlide = oPPres.Slides.Add(1, ppLayoutTitleOnly)
oPSlide.Select
'Copy a range as a picture and align it
ActiveSheet.Range("A1:B10").CopyPicture Appearance:=xlScreen, Format:=xlPicture
oPSlide.Shapes.Paste.Select
oPPT.ActiveWindow.Selection.ShapeRange.Align msoAlignCenters, True
oPPT.ActiveWindow.Selection.ShapeRange.Align msoAlignMiddles, True
'Add the title text
sText = "My Header"
oPSlide.Shapes.Title.TextFrame.TextRange.Text = sText
oPPT.Activate
'Release Objects
Set oPSlide = Nothing
Set oPPres = Nothing
Set oPPT = Nothing
End Sub

Dealing with MS Word From Excel VBA

The following code will show you how to deal and interact with Word. We can create Word document from scratch or modify the existing document using Excel VBA.

Sub sbWord_FormatingWordDoc()
'Declarations
Dim oWApp As Word.Application
Dim oWDoc As Word.Document
Dim sText As String
Dim iCntr As Long
Set oWApp = New Word.Application
Set oWDoc = oWApp.Documents.Add() '("C:\Documents\Doc1.dot") 'You can specify your template here
'Adding new Paragraph
Dim para As Paragraph
Set para = oWDoc.Paragraphs.Add
para.Range.Text = "Paragraph 1 - My Heading"
para.Format.Alignment = wdAlignParagraphCenter
para.Range.Font.Size = 18
para.Range.Font.Name = "Cambria"
For i = 0 To 2
Set para = oWDoc.Paragraphs.Add
para.Space2
Next
Set para = oWDoc.Paragraphs.Add
With para
.Range.Text = "Paragraph 2 - Some Text for the next Paragraph"
.Alignment = wdAlignParagraphLeft
.Format.Space15
.Range.Font.Size = 14
.Range.Font.Bold = True
End With
oWDoc.Paragraphs.Add
Set para = oWDoc.Paragraphs.Add
With para
.Range.Text = "Paragraph 3 - This is another Paragraph, you can create number of paragraphs like this and format it"
.Alignment = wdAlignParagraphLeft
.Format.Space15
.Range.Font.Size = 12
.Range.Font.Bold = False
End With
oWApp.Visible = True
End Sub

Interact with MS Access from Excel VBA

The following code will show you how to deal and interact with Access.

Add Reference: Microsoft Access Object Library

Sub sbAccess_OpenAForm()
'Declaring Access Application
Dim oAApp As Access.Application
'Connecting Access Data base
Set oAApp = New Access.Application
oAApp.OpenCurrentDatabase ("C:\ExampleDatabase.accdb")
'Opening a From
With oAApp
.DoCmd.OpenForm "MyForm", acNormal
.Visible = True
End With
End Sub

Interact with Outlook from Excel VBA

Sub sbOutlook_SendAMail()
'Declaration
Dim oOApp As Object
Dim oMail As Object
Set oOApp = CreateObject("Outlook.Application")
Set oMail = oOApp.CreateItem(0)
On Error Resume Next
' Change the mail address and subject in the macro before you run it.
With oMail
.To = "userid@organization.com"
.CC = ""
.BCC = ""
.Subject = "Write Your Subject Here"
.Body = "Hi, This is example Body Text."
'.Attachments.Add ("C:\Temp\ExampleFile.xls") '=> To add any Attcahment
.Display '=> It will display the message
'.Send '=> It will send the mail
End With
On Error GoTo 0
Set oMail = Nothing
Set oOApp = Nothing
End Sub

Interact with MS Word from Excel VBA -Another Example

Add Reference: Microsoft Word Object Library

Sub sbWord_ExcelToWord()
'Declarations
Dim oWApp As Word.Application
Dim oWDoc As Word.Document
Dim sText As String
Dim iCntr As Long
set oWApp = New Word.Application
Set oWDoc = oWApp.Documents.Add() '("C:\Documents\Doc1.dot") 'You can specify your template here
For iCntr = 2 To Cells.SpecialCells(xlCellTypeLastCell).Row
sText = Cells(iCntr, 1)
sText = sText & " " & Cells(iCntr, 2)
sText = sText & " " & Cells(iCntr, 3)
sText = sText & " " & Cells(iCntr, 4)
oWDoc.Content.InsertAfter (sText)
Next iCntr
oWApp.Visible = True
' Releasing objects
Set oWDoc = Nothing
Set oWApp = Nothing
End Sub

Dealing with Internet Explorer

The following code will show you how to deal and interact with Internet Explorer.

Sub sbIE_OpenASite()
Dim IE As Object
' Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")
' Send the form data To URL As POST binary request
IE.Navigate "http://www.excely.com/"
' Wait while loading...
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
IE.Visible = True
'Release
Set IE = Nothing
End Sub

Dealing with Other Applications from Excel VBA – Calculator

The following code will show you how to deal and interact with Calculator.

Sub sbAnyApplication_OpenCalculator()
Dim sProg As String
Dim tID As Double
On Error Resume Next
sProg = "Calc.exe"
tID = Shell(sProg)
If Err <> 0 Then
MsgBox "Can't Start Calculator"
End If
End Sub

Run VBScript from Excel VBA

Sub sbVBScript_RunVBS()
Dim SFilename As String
SFilename = "C:\Temp\Test.vbs" 'Change the file path
' Run VBScript file
Set wshShell = CreateObject("Wscript.Shell")
wshShell.Run """" & SFilename & """"
End Sub

VBA to Attach Send An Excel Chart to Outlook Email

Sub emailingProgram()
Dim olapp As Outlook.Application
Dim objmail As Outlook.mailitem
Dim pos As Integer
Set olapp = Outlook.Application
For Each xcell In Sheets("Sheet1").Range(Range("RangetoCopy"), _
Range("RangetoCopy").End(xlDown))
msgText = Range("Msg")
xcell.Activate
ActiveCell.Offset(0, 1).Select
'If you think that the email ID is in the pattern firstname.lastname@mail.com use this if block
'The code will go into the else statement if the First Name is not mentioned
If Selection.Value = "" Then
pos = InStr(1, xcell.Value, ".")
Fname = Mid$(xcell.Value, 1, InStr(1, xcell.Value, ".") - 1)
Else
'If you have mentioned the first names in the First Name column this part will read it directly
Fname = Selection.Value
End If
'For each of the cells present in the To List we create a MailItem and send it
Set objmail = olapp.CreateItem(olMailItem)
objmail.BodyFormat = olFormatRichText
'Setting the subject
objmail.Subject = "Example Subject"
'Uncomment the following line of code in case you want to send a plain message
'objmail.Body = "Hi " + UCase(Mid$(Fname, 1, 1)) + Mid$(Fname, 2) + "," + Chr(13) + Chr(10) + msgText
'For using an image in your mail or an HTML body for styling
objmail.HTMLBody = "<p><font size='6' face='arial' color='red'><i>Dear " & UCase(Mid$(Fname, 1, 1)) + Mid$(Fname, 2) & "<br></font></p><br><p align='CENTER'><font size='5' face='COMIC SANS' color='RED'>Wishing you a Wonderful Birthday</p><br><br></font><p align='CENTER'><a href='http://www.abrahamsarah.com'><img src='http://www.abrahamsarah.com/bilder/Happy-Birthday005.png' width=450 height=412 border=0></a></a><br><br><br><p align='left'>Thanks & Regards <br><br/> _<p><p align='left'><br>Anshuman Pandey<br>http://www.anshumusing.co.in/</p>"
objmail.To = emailid@domain.com
objmail.Send
Set objmail = Nothing
Next xcell
End Sub

Excel VBA to Interact with Other Applications的更多相关文章

  1. 杂项-软件: VBA(Visual Basic for Applications)

    ylbtech-杂项-软件: VBA(Visual Basic for Applications) VBA (Visual Basic宏语言) Visual Basic for Application ...

  2. 来吧!带你玩转 Excel VBA

    来吧!带你玩转 Excel VBA 从错失良机到艰辛的DOS征程,从坎坷购机自学路到转机起程,从爱好到事业,他从一个完全不懂电脑的人到VBA高级应用者,一切全是自学…… 我是罗刚君,来自四川的一个小县 ...

  3. C#调用Excel VBA宏

    近日的一系列工作是做网站的营运维护,因此做了大量的支持工具.有Excel中写VBA的,也有直接C#做的工具.有时需要在C#中执行Excel VBA宏,甚至有时还需要在执行了VBA宏之后,获取返回值再进 ...

  4. 2017-5-29 Excel VBA 小游戏

    ---恢复内容开始--- 转一个Excel VBA的小游戏,最近对excel有了更深入的了解,功能很强大,也刷新了我对待事情的态度. 一.准备界面 我们先来把游戏界面准备好,选中前4行,行高调成50, ...

  5. 使用Excel VBA编程将网点的百度坐标转换后标注到高德地图上

    公司网点表存储的坐标是百度坐标,现需要将网点位置标注到高德地图上,研究了一下高德地图的云图数据模版 http://lbs.amap.com/yuntu/reference/cloudstorage和坐 ...

  6. Excel VBA附合导线平差自动计算表

    这是6,7年前做的一个excel vba自动计算附合导线平差的表格. 对于做测绘的朋友来说,附合导线平差是最基础的技能,目前来说,能平差的软件和工具也很多,像南方的平差易,科傻平差.清华三维平差等,但 ...

  7. Excel VBA 连接各种数据库(二) VBA连接Oracle数据库

    本文主要内容: Oracle环境配置 ODBC驱动设置.第三方驱动下载 VBA连接Oracle连接方法 Oracle10g官方免账号下载地址 系统环境: Windows 7 64bit Excel 2 ...

  8. Excel vba:批量生成超链接,添加边框,移动sheet等

    Excel vba 操作 批量生成sheet目录并添加超链接 Sub Add_Sheets_Link() 'Worksheets(5)为清单目录页 '在sheet页上生成sheet页名字并超链接 To ...

  9. Excel VBA(宏):添加宏

    写在前面: .编写宏,打开VBA,双击ThisWorkbook对当前工作薄进行编写宏:双击Sheet1,对整个sheet编写宏: 或者创建模块,在模块里,编写.调试代码. 打开VBA的方法见第一讲,结 ...

随机推荐

  1. Java环境搭建指南

    1.1 配置Java环境 1.  下载并安装Jdk1.7或Jdk1.8 http://www.oracle.com/technetwork/java/javase/downloads/index.ht ...

  2. 我的nlp之路(1)

    1/8日任务 基础篇: 如何使用远程连接从windows或者linux连到服务器进行操作(切换用户,传递文件) (严禁使用root账户) Linux基本bash命令 a)        查看文件大小, ...

  3. codeforces570D Tree Requests

    题目链接:codeforces570D 正解:$dsu$ $on$ $tree$ 解题报告: 考虑这又是一类子树内的不带修改统计问题,直接上$dsu$ $on$ $tree$好咯. 直接按上一道题的做 ...

  4. 24,25-request对象

    var http = require('http'); var server = http.createServer(); server.listen() console.log(server.add ...

  5. vue-cli Uncaught SyntaxError: Use of const in strict mode解决办法

    vue-cli初始化项目,开发环境运行项目使用了webpack-dev-server,而最新版本webpack-dev-server@2.9.1运行项目时,并不能成功的把es6语法转化为es5,所以在 ...

  6. PIL.Image与Base64 String的互相转换

    https://www.jianshu.com/p/2ff8e6f98257 PIL.Image与Base64 String的互相转换 mona_alwyn 2018.01.18 19:02* 字数 ...

  7. 开启Tomcat APR运行模式,优化并发性能

    Tomcat支持三种接收请求的处理方式:BIO.NIO.APR 1>.BIO模式:阻塞式I/O操作,表示Tomcat使用的是传统JavaI/O操作(即Java.io包及其子包).Tomcat7以 ...

  8. C++(二十七) — 深拷贝、浅拷贝、复制构造函数举例

    1.复制构造函数.及new空间申请 复制构造函数,也是构造函数.只在初始化时调用,如果定义对象后赋值,比如,t1=t2,则只是运算符重载,没有调用构造函数. #include <iostream ...

  9. nyoj993——容斥

    How many integers can you find 时间限制:1000 ms  |  内存限制:65535 KB 难度:1   描述 给你三个数,n,m1,m2,找出所有小于n的能被m1或m ...

  10. Mutations

    蛤蟆可以吃队友,也可以吃对手. 如果数组第一个字符串元素包含了第二个字符串元素的所有字符,函数返回true. 举例,["hello", "Hello"]应该返回 ...