我有一个宏来导出子文件夹中的相关电子邮件字段,但我需要它在每次收到新电子邮件时自动运行。事实上,我只想导出主题行,当电子邮件到达“M_M_Asia”时,是否有一个脚本会触发该宏(“ExportMessagesToExcel”)?
我在 Outlook 2007 中。
如果有人能提供帮助,我将永远感激不尽。干杯,伙计们。
您应该能够使用模块中的Application_NewMail
(or NewMailEx
) 事件ThisOutlookSession
来调用您自己的宏。
另一种方法是创建一个运行 VBA 脚本的规则,但是您的 VBA 子需要处理 MailItem 参数:
Option Explicit
Sub MyVBARule(poMail As MailItem)
End Sub
或者您可以添加“ItemAdd”事件处理程序,例如:
Option Explicit
Private Const ASIA_FOLDER_NAME As String = "M_M_Asia"
Private WithEvents m_outlookFolderItems As Outlook.Items
Private Sub Application_Startup()
Initialize_Handler
End Sub
Private Sub m_outlookFolderItems_ItemAdd(ByVal Item As Object)
' You have new mail in folder ASIA_FOLDER_NAME
RunExcelMacro
End Sub
Private Sub Initialize_Handler()
Dim outlookFolder As Outlook.MAPIFolder
Dim defaultInboxFolder As Outlook.MAPIFolder
Dim outlookNameSpace As Outlook.NameSpace
Set outlookNameSpace = Outlook.GetNamespace("MAPI")
Set defaultInboxFolder = outlookNameSpace.GetDefaultFolder(olFolderInbox)
Set outlookFolder = defaultInboxFolder.Folders(ASIA_FOLDER_NAME)
Set m_outlookFolderItems = outlookFolder.Items
End Sub
Public Sub RunExcelMacro()
On Error GoTo RunExcelMacro_Err
Const path As String = "C:\temp\Excel_VBA\"
Const fileName As String = "CallMeFromOutloouk.xlsm"
Const macroName As String = "CallMeFromOutlook"
Dim excelObject As Object
Dim workbookObject As Object
Set excelObject = CreateObject("Excel.Application")
On Error Resume Next
Set workbookObject = excelObject.Workbooks(fileName)
On Error GoTo RunExcelMacro_Err
If workbookObject Is Nothing Then
Set workbookObject = excelObject.Workbooks.Open(path & fileName)
End If
excelObject.Run fileName & "!" & macroName
Exit Sub
RunExcelMacro_Err:
MsgBox Err.Description
End Sub