【问题标题】:How to use Outlook's Application_AdvancedSearchComplete event handler in Excel VBA?如何在 Excel VBA 中使用 Outlook 的 Application_AdvancedSearchComplete 事件处理程序?
【发布时间】:2021-05-24 13:47:22
【问题描述】:

我在 Outlook 中编写了 VBA 代码以使用 AdvancedSearch。它奏效了。

当我将它移至 Excel 以成为更大例程的一部分时,事件处理程序停止工作。

主代码如下所示。

Public gblnProcessAttachmentsDone As Boolean

Public gblnProcessAttachmentsStopped As Boolean

Sub ProcessAttachmentsSub()

' this routine performs the advanced search on a folder

...
    
    gblnProcessAttachmentsDone = False
    gblnProcessAttachmentsStopped = False

...

    'perform search
    Set objSearch = objOL.AdvancedSearch(strScope, strFilter, True, "ProcessAttachments")
        
    Do Until gblnProcessAttachmentsDone
        DoEvents
    Loop

这些是事件处理程序。

Private Sub Application_AdvancedSearchComplete(ByVal SearchObject As Outlook.Search)

' this routine identifies the search that has just completed

    If SearchObject.Tag = "ProcessAttachments" Then
        Debug.Print "Search completed at " & Time
        gblnProcessAttachmentsDone = True
    End If

End Sub

Private Sub Application_AdvancedSearchStopped(ByVal SearchObject As Outlook.Search)

' this routine identifies the search that has just been stopped by the user

    If SearchObject.Tag = "ProcessAttachments" Then
        Debug.Print "Search stopped at " & Time
        gblnProcessAttachmentsStopped = True
        gblnProcessAttachmentsDone = True
    End If

End Sub

我尝试将它们放在“ThisWorkbook”和 Class 模块中,但在这两种情况下,事件都不会被捕获。

【问题讨论】:

  • 我已经编辑了您的问题,在您的源代码前后添加了一些反引号(或重音符号),如此 URL 所示:stackoverflow.com/help/formatting,它使您的源代码更具可读性。跨度>
  • 谢谢你——我的第一篇文章。我尝试格式化文本,但失败了!我今天会检查那个格式链接!

标签: excel vba outlook


【解决方案1】:

在 Excel VBA 中,Application 内部变量指向 Excel.Application,而不是 Outlook.Application。您的事件处理程序 (Application_AdvancedSearchStopped) 不会自动连接。声明 objOL with events 并设置事件处理程序。

【讨论】:

  • 我在类模块中添加了Public WithEvents objOL As Outlook.Application,但现在Set objOL = GetObject(, "Outlook.Application") 行抛出“变量未定义”错误。我显然仍然没有抓住重点!
  • 不要在 Outlook 中使用 GetObject。始终使用 CreeateObject - Outlook 是一个单例。
【解决方案2】:

要启动 Outlook 自动化会话,您可以使用早期绑定或后期绑定。后期绑定使用 Visual Basic GetObject 函数或 CreateObject 函数来初始化 Outlook。例如,以下代码将对象变量设置为 Outlook Application 对象,这是 Outlook 对象模型中的最高级别对象。所有自动化代码必须首先定义一个 Outlook 应用程序对象才能访问任何其他 Outlook 对象。

Dim objOL as Object 
Set objOL = CreateObject("Outlook.Application")

要使用早期绑定,您首先需要设置对 Outlook 对象库的引用。使用 Visual Basic for Applications (VBA) 工具菜单上的引用命令设置对 Microsoft Outlook xx.x 对象库的引用,其中 xx.x 表示您正在使用的 Outlook 版本。然后,您可以使用以下语法启动 Outlook 会话。

Dim objOL as Outlook.Application 
Set objOL = New Outlook.Application

在外部应用程序中处理 Outlook 应用程序级事件:

  1. 首先,您必须使用WithEvents 关键字声明一个变量,以标识您要处理其事件的对象。
Dim WithEvents objOL as Outlook.Application 
Set objOL = New Outlook.Application
  1. 然后您可以在模块窗口的Objects list 中选择一个Outlook 应用程序实例对象,然后在过程列表中选择该事件。 Visual Basic 编辑器随后会将事件过程的模板添加到模块窗口。然后,您可以键入要在事件发生时运行的代码。
Private Sub objOL_AdvancedSearchComplete(ByVal SearchObject As Outlook.Search)

' this routine identifies the search that has just completed

    If SearchObject.Tag = "ProcessAttachments" Then
        Debug.Print "Search completed at " & Time
        gblnProcessAttachmentsDone = True
    End If

End Sub

Advanced search in Outlook programmatically: C#, VB.NET 文章中阅读有关AdvancedSearch 方法的更多信息。

【讨论】:

    猜你喜欢
    • 2020-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多