应用自动化

在VBA(我希望VB与之类似)中,可以使用以下步骤来控制另一个Office应用程序(自动化):

  1. 确保已选中要自动化的应用程序的参考(VBA窗口/工具/参考)。
  2. 设置要使用的应用程序对象变量(如果愿意,可以使用With ... End With )。
    要么:
    1. 使用CreateObject(Class)为多实例程序打开一个新对象(对于单实例程序,如果已经打开,它将返回当前实例)。
    2. 如果所需的应用程序支持,请使用GetObject(Document,Class)打开特定的文档。
  3. 设置应用程序的.Visible属性。 这确定您的更改是否对操作员可见。
  4. 当应用程序的对象处于活动状态时,您只需在引用该应用程序对象时即可在其他应用程序中运行代码(例如, EG。accApp.Visible = True ... 调用accApp.CurrentDB.Close )。
  5. 如果已完成一份文档,但仍要准备好使用该应用程序,则将.Visible属性设置为False,然后稍后重新激活。
  6. 当您完全完成以下任一操作时:
    1. 如果要关闭该应用程序,只需将其关闭( 调用accApp.Quit )。
    2. 如果要让操作员可以使用它,请确保将.Visible设置为True。
  7. 释放所有关联的对象(设置为NothingEnd With )。
这是可以在您的代码中使用的一些有用的方法/属性:
  • 退出 -每个Application对象都有一个Quit方法。
  • 激活 -将焦点设置到应用程序
  • 运行 -使您能够从应用程序 (或文档)运行代码。
  • 复制 / 粘贴 -在不同的应用程序之间应该可以正常工作
  • WindowState-可以设置为“ 最大化” ,“ 最小化”或“ 正常”
注意

当使用任何Office应用程序自己的VBA IDE(独立开发环境)时,通常会遇到很多默认设置。 例如,在Excel中引用

Range("A1")对象,它将理解对Excel的ApplicationActiveSheet对象的引用。 在任何外部环境中,都不会设置这些默认值,因此,假设您有一个名为appXLExcel.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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值