在Outlook主题行上触发Excelmacros文件触发

如果我收到只有主题行"Run Dashboard"电子邮件,请提供一个代码,可以打开excelmacros文件。

将以下代码粘贴到"ThisOutlookSession"

 Private WithEvents Items As Outlook.Items Private Sub Application_Startup() Dim olNameSpace As Outlook.NameSpace Dim Folder As Outlook.MAPIFolder Set olNameSpace = Application.GetNamespace("MAPI") Set Folder = olNameSpace.GetDefaultFolder(olFolderInbox) Set Items = Folder.Items End Sub Private Sub Items_ItemAdd(ByVal Item As Object) If TypeOf Item Is Outlook.MailItem Then '// Subject line here If InStr(Item.Subject, "Run Dashboard") Then OpenExcel Item End If End If End Sub Private Sub OpenExcel(olItem As Outlook.MailItem) '// Declare variables Dim xlApp As Excel.Application Dim xlWb As Excel.Workbook Dim xlSheet As Excel.Worksheet Dim FilePath As String '// File Path to Open FilePath = "C:\temp\Temp.xlsm" '// Start Excel Application On Error Resume Next Set xlApp = GetObject(, "Excel.Application") If Err <> 0 Then Application.StatusBar = "Please wait while Excel is opened ... " Set xlApp = CreateObject("Excel.Application") xlStarted = True End If With xlApp .Visible = True .EnableEvents = False End With Set xlWb = xlApp.Workbooks.Open(FilePath) Set xlSheet = xlWb.Sheets("Sheet1").Activate '// Clean up Set xlApp = Nothing Set xlWb = Nothing Set xlSheet = Nothing Set olItem = Nothing End Sub 

您可以在Outlook中创build一个规则,当您收到主题中指定文本的电子邮件时触发。 然后,你可以分配一个VBAmacros应该看起来像下面的子:

  Public Sub Test(mail as MailItem) ' do whatever you need End Sub 

mail对象代表到达的项目。

从Outlookmacros中,您可以自动执行Excel并以编程方式运行VBAmacros,或者只需将您的Excelmacros移动到Outlook即可直接从规则运行。 请参阅如何从Visual Basic中自动化Microsoft Excel以获取更多信息。

  1. 将Excel VBAfunction移入Outlook VBA或从Outlook运行 。
  2. 创build可以从Outlook规则启动的 VBA Sub
    (在这篇文章中你可以find代码)。
  3. 使用此脚本操作创buildOutlook规则。

完成。

关于第2点:

 Sub CustomMailMessageRule(Item As Outlook.MailItem) MsgBox "Mail message arrived: " & Item.Subject End Sub