在VBA(我希望VB与之类似)中,可以使用以下步骤来控制另一个Office应用程序(自动化):
- 确保已选中要自动化的应用程序的参考(VBA窗口/工具/参考)。
- 设置要使用的应用程序对象变量(如果愿意,可以使用With ... End With )。
要么:- 使用CreateObject(Class)为多实例程序打开一个新对象(对于单实例程序,如果已经打开,它将返回当前实例)。
- 如果所需的应用程序支持,请使用GetObject(Document,Class)打开特定的文档。
- 设置应用程序的.Visible属性。 这确定您的更改是否对操作员可见。
- 当应用程序的对象处于活动状态时,您只需在引用该应用程序对象时即可在其他应用程序中运行代码(例如, EG。accApp.Visible = True ... 调用accApp.CurrentDB.Close )。
- 如果已完成一份文档,但仍要准备好使用该应用程序,则将.Visible属性设置为False,然后稍后重新激活。
- 当您完全完成以下任一操作时:
- 如果要关闭该应用程序,只需将其关闭( 调用accApp.Quit )。
- 如果要让操作员可以使用它,请确保将.Visible设置为True。
- 释放所有关联的对象(设置为Nothing或End With )。
- 退出 -每个Application对象都有一个Quit方法。
- 激活 -将焦点设置到应用程序 。
- 运行 -使您能够从应用程序 (或文档)运行代码。
- 复制 / 粘贴 -在不同的应用程序之间应该可以正常工作
- WindowState-可以设置为“ 最大化” ,“ 最小化”或“ 正常” 。
当使用任何Office应用程序自己的VBA IDE(独立开发环境)时,通常会遇到很多默认设置。 例如,在Excel中引用
Range("A1")
对象,它将理解对Excel的Application
的ActiveSheet
对象的引用。
在任何外部环境中,都不会设置这些默认值,因此,假设您有一个名为appXL
的Excel.Application
对象,则可以将其称为appXL.ActiveSheet.Range("A1")
。
这只是一个示例,但这一点对所有Office应用程序都适用。
大多数人在第一次开始使用Application Automation时,会发现他们在这个问题上遇到了很多麻烦。 注意它,并尽一切可能避免它。
张贴者
fauxanadu Basic Outlook Automation(包括发送新电子邮件)' Requires A References to the Microsoft Outlook Object Library
' in VBE, select Tools->References, and ensure it is checked
Private Sub ExportToOutlook()
' Variable Declaration
Dim objOutlook as Object
Dim objEMail as Object
' If Outlook is open, use GetObject, otherwise open Outlook
Set objOutlook = GetObject(, "Outlook.Application")
If objOutlook Is Nothing Then Set objOutlook = CreateObject("Outlook.Application")
' Creates a new e-mail
Set objEMail = objOutlook.CreateItem(0)
With objEMail
' Adds To Recipient
Set ToContact = .Recipients.Add("Me@Gmail.Com")
' Adds CC recipient
ToContact.Type = olCC
Set ToContact = .Recipients.Add("You@Gmail.com")
' Sets the Subject
.Subject = "Service Report 1234"
' Sets the Body
.Body = "Attached herein are the Reports"
' Adds attachment
.Attachments.Add "c:\Service Report 1234", olByValue, , "Service Report"
' Embeds attachment
.Attachments.Add "c:\JoeBob.gif", olEmbeddedItem, , "Joe Bob's Picture"
' Receipt upon delivery
.OriginatorDeliveryReportRequested = True
' Recipt upon read
.ReadReceiptRequested = True
' Displays the E-Mail
.Display
' Sends the E-Mail
.Send
' Saves a Draft of the E-Mail
.Save
End With
End Sub
基本Excel自动化
' Requires a Reference to Microsoft Excel 8.0 Object Library or Higher
' In VBE, goto Tools->References... and select it from the list
Private Const conAppNotRunning As Long = 429
Private Sub ExportToExcel()
' Variable Declarations
Dim objExcel As Excel.Application
' If Excel is open, use GetObject, otherwise create a new Excel object
On Error Resume Next
Set objExcel = GetObject(, "Excel.Application")
If Err = conAppNotRunning Then Set objExcel = New Excel.Application
With objExcel
' Adds a new workbook to the Excel environment
.Workbooks.Add
' Excel VBA Code goes here
' Causes the Excel window to become visible
.Visible = True
End With
End Sub
From: https://bytes.com/topic/access/insights/600219-application-automation