转载自: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. NPM Scripts -- onchange parallelshell

    Watch for changes to the styles.scss file and automatically compile it to the css file. Run multiple ...

  2. Memcached get 命令

    Memcached get 命令获取存储在 key(键) 中的 value(数据值) ,如果 key 不存在,则返回空. 语法: get 命令的基本语法格式如下: get key 多个 key 使用空 ...

  3. 【三小时学会Kubernetes!(二) 】Kubernetes 简介及Pod实践

    Kubernetes 简介 我向你保证我没有夸大其词,读完本文你会问“为什么我们不称它为 Supernetes?” Kubernetes 是什么? 从容器启动微服务后,我们有一个问题,让我们通过如下问 ...

  4. KVM与XEN虚拟化环境究竟有何不同

    虚拟化的概念在近些年收到了很大程度上的普及,求其原因很简单:虚拟化能够最大程度利用资源,为企业节约成本.目前市面较受欢迎的虚拟架构主要有KVM.XEN和VMware,其中,KVM和XEN都是免费开源的 ...

  5. 9.深入理解AbstractQueuedSynchronizer(AQS)

    1. AQS简介 在上一篇文章中我们对lock和AbstractQueuedSynchronizer(AQS)有了初步的认识.在同步组件的实现中,AQS是核心部分,同步组件的实现者通过使用AQS提供的 ...

  6. msdia80.dll文件出现在磁盘根目录下的解决方案(转)

    情况描述: 当安装某些软件后,磁盘根目录中多出了msdia80.dll文件,该文件显示为2006年12月1日,884KB. 原因:   当使用64位操作系统时,在电脑上安装 Microsoft Vis ...

  7. HDU1595-最短路-删边

    find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  8. leetcode 849. Maximize Distance to Closest Person

    In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is emp ...

  9. JavaScript---js的模块化

    js的模块模式被定义为给类提供私有和公共封装的一种方法,也就是我们常说的“模块化”. 怎么实现“模块化”? 通过闭包的原理来实现“模块化”  ,具体实现:1.必须有外部的封闭函数,该函数必须至少被调用 ...

  10. django中如何将多个app归到一个目录下。

    1.当startapps 生成多个app后,为了便于管理,可新建一个apps目录,把应用全部剪切进apps. 如果是在pycharm中,会提示是否自动更新路径,这里要全部选择取消. QQ群交流:697 ...