Excel VBA to Interact with Other Applications
转载自: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的更多相关文章
- 杂项-软件: VBA(Visual Basic for Applications)
ylbtech-杂项-软件: VBA(Visual Basic for Applications) VBA (Visual Basic宏语言) Visual Basic for Application ...
- 来吧!带你玩转 Excel VBA
来吧!带你玩转 Excel VBA 从错失良机到艰辛的DOS征程,从坎坷购机自学路到转机起程,从爱好到事业,他从一个完全不懂电脑的人到VBA高级应用者,一切全是自学…… 我是罗刚君,来自四川的一个小县 ...
- C#调用Excel VBA宏
近日的一系列工作是做网站的营运维护,因此做了大量的支持工具.有Excel中写VBA的,也有直接C#做的工具.有时需要在C#中执行Excel VBA宏,甚至有时还需要在执行了VBA宏之后,获取返回值再进 ...
- 2017-5-29 Excel VBA 小游戏
---恢复内容开始--- 转一个Excel VBA的小游戏,最近对excel有了更深入的了解,功能很强大,也刷新了我对待事情的态度. 一.准备界面 我们先来把游戏界面准备好,选中前4行,行高调成50, ...
- 使用Excel VBA编程将网点的百度坐标转换后标注到高德地图上
公司网点表存储的坐标是百度坐标,现需要将网点位置标注到高德地图上,研究了一下高德地图的云图数据模版 http://lbs.amap.com/yuntu/reference/cloudstorage和坐 ...
- Excel VBA附合导线平差自动计算表
这是6,7年前做的一个excel vba自动计算附合导线平差的表格. 对于做测绘的朋友来说,附合导线平差是最基础的技能,目前来说,能平差的软件和工具也很多,像南方的平差易,科傻平差.清华三维平差等,但 ...
- Excel VBA 连接各种数据库(二) VBA连接Oracle数据库
本文主要内容: Oracle环境配置 ODBC驱动设置.第三方驱动下载 VBA连接Oracle连接方法 Oracle10g官方免账号下载地址 系统环境: Windows 7 64bit Excel 2 ...
- Excel vba:批量生成超链接,添加边框,移动sheet等
Excel vba 操作 批量生成sheet目录并添加超链接 Sub Add_Sheets_Link() 'Worksheets(5)为清单目录页 '在sheet页上生成sheet页名字并超链接 To ...
- Excel VBA(宏):添加宏
写在前面: .编写宏,打开VBA,双击ThisWorkbook对当前工作薄进行编写宏:双击Sheet1,对整个sheet编写宏: 或者创建模块,在模块里,编写.调试代码. 打开VBA的方法见第一讲,结 ...
随机推荐
- Learning Perl 第九章习题第二题
把输入文件中的所有Fred换成Larry, 不区分大小写. 知识点 1. 文本文件读写 2. 简单的正则替换 3. unless 的用法 4. $_ 的用法
- Vue——解决[Vue warn]: Invalid prop: custom validator check failed for prop "index". found in错误
Invalid prop: custom validator check failed for prop "index". 错误重现: 使用element-ui的菜单,在SubMe ...
- wamp升级php7
原文:http://blog.csdn.net/cheng6251/article/details/50730441 1.下载php7 http://windows.PHP.net/downloa ...
- lessc的安装
win+R,cmd打开命令面板,输入cnpm install -g less,如图,然后就可以通过lessc -v查询lessc的版本,出现版本号证明安装成功
- VS2017 MVC项目,新建控制器提示未能加载文件或程序集“Dapper.Contrib解决方法
VS2017中MVC项目中,右键新建控制器时,提示 未能加载文件或程序集“Dapper.Contrib, Version=1.50.0.0, Culture=neutral, PublicKeyTok ...
- Kotlin------数据类型和语法
今天简单的来介绍Kotlin的基本语法.编程语言大多相通的,会基础学起来都很快,理论都一样,实现的代码语言不一样而已. 数值类型 Kotlin 处理数值的方法和 java 很相似,但不是完全一样.比如 ...
- poj2187凸包最远点对
暴力过了 #include<map> #include<set> #include<cmath> #include<queue> #include< ...
- SpringIOC源码分析总结
大致的加载过程: spring ioc容器的加载,大体上经过以下几个过程: 资源文件定位.解析.注册.实例化 1.资源文件定位:主要发生在ApplicationContext中,由于applicati ...
- 1-26-1-expect无交互式-正则表达式
大纲: 1.expect环境搭建及脚本编写 概述 expect脚本详解 expect环境搭建 expect脚本实现ssh远程连接 expect脚本实现ssh远程连接(通过shell传递参数) 2.正则 ...
- IOS-源代码管理工具(SVN)
一.使用环境 要想利用SVN管理源代码,必须得有2套环境 服务器 用于存储客户端上传的源代码 可以在Windows上安装Visual SVN Server 大部分情况下,公司的开发人员不必亲自搭建SV ...