2017-03-10 148 views
1

我想在带有“主题”的电子邮件发送到我的收件箱时运行Excel宏。我设置了在Outlook中的管理规则&警报中运行脚本。当我收到带有“主题”的邮件时,宏观上没有任何反应。如何在Outlook中收到新邮件后运行Excel宏?

Sub Test(mail As MailItem) 

    Dim ExApp As Excel.Application 
    On Error Resume Next 
    Set ExApp = GetObject(, "Excel.Application") 
    If Not ExApp Is Nothing Then 
     ExApp.Run "'C:\Users\Desktop\Production v2.7.1.xlsm'!Main_function_Auto" 
    End If 
End Sub 
+0

上的错误继续下一步绕过错误,也有人认为这意味着没有任何错误。只要绕过错误的原因结束,就必须有一个On Error GoTo 0。将它放在Set ExApp = GetObject(,“Excel.Application”)后面。如果有错误,你可以看到它们。 – niton

回答

0

当从Outlook的调用Excel子过程中,一定要包含模块的名字 -

Option Explicit 
Public Sub Example(Item As Outlook.MailItem) 
    Dim xlApp As Excel.Application 
    Dim xlBook As Workbook 

    Set xlApp = New Excel.Application 
    Set xlBook = xlApp.Workbooks.Open(Environ(_ 
         "USERPROFILE") & "\Desktop\Production.xlsm") 
    xlApp.Visible = True 

' // Run Macro in file 
    xlBook.Application.Run "Module1.Main_function_Auto" 

    Set xlApp = Nothing 
    Set xlBook = Nothing 
End Sub 
+0

我确实取代了这段代码。但宏不跑也。 “USERPROFILE”)&“\ Desktop \ Production.xlsm”) userprofile在这里做了什么?我需要给完整的文件路径的Excel位置? –

+0

@NadalMir你有没有重命名模块? – 0m3r

+0

是的,我确实重命名了模块。 –