转载自: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. vue2.0--vue-router路由

    一.vue-router如何进行参数传递 1.name  $route.name 通过在路由router/index.js中配置路由时定义的name属性来传递 ① 有一个页面components/he ...

  2. 定时模块app_timer用法及常见问题—nRF5 SDK模块系列二

    app_timer是大家经常用到的一个库,app_timer的功能就是定时,也就是说,你在某一时刻启动一个app timer并设定超时时间,超时时间一到,app_timer就会回调timeout ha ...

  3. UTF-8文件的Unicode签名BOM(Byte Order Mark)问题记录(EF BB BF)

    背景 楼主测试的批量发送信息功能上线之后,后台发现存在少量的ERROR日志,日志内容为手机号码格式不正确. 此前测试过程中没有出现过此类问题,从运营人员拿到的发送列表的TXT,号码是符合规则的,且格式 ...

  4. PHP函数前面添加@的作用

    @是PHP提供的错误信息屏蔽的专用符号. 比如在一个函数前使用@ @mysql_query 不会出现Warning, 而原来mysql_query 在遇到错误时会在页面上访提示Warning. @是可 ...

  5. 英语每日阅读---7、VOA慢速英语(翻译+字幕+讲解):卡梅伦呼吁女性移民学英语

    英语每日阅读---7.VOA慢速英语(翻译+字幕+讲解):卡梅伦呼吁女性移民学英语 一.总结 一句话总结: a.learn English or be deported:British Prime M ...

  6. customs event

    // First create the event var myEvent = new CustomEvent("userLogin", { detail: { username: ...

  7. [嵌入式培训笔记]----Linux命令简介

    Linux文件系统的结构类似一棵树,是从一个树根生长出来的.树根叫做/.从树根长出很多分叉,就这么一直生长下去.我们使用Linux操作系统的时候总是落在某个分叉上.ls命令可以帮助我们查看当前分叉上所 ...

  8. 12.18 webSocket消息推送

    ---恢复内容开始--- 准备工作: 在要跳转到的页面加入js <#--弹窗--> <div class="modal fade" id="myModa ...

  9. OpenVPN CreateProcess Failed 问题解决

    启动 OpenVPN GUI 时失败 显示如下信息 CreateProcess Failed, exe=’X:XXXXXOpenVPNbinopenvpn.exe’ cmdline=’openvpn ...

  10. JQuery强大的操控标签能力

    jquery选择器 $("#myId")          选择id为myId的元素 $(".myClass")     选择class为myClass的元素 ...