无法从Outlook打开Excel文件

时间:2015-10-30 06:43:13

标签: excel-vba outlook outlook-vba vba excel

如果我收到包含主题行的电子邮件,我可以打开Excel文件:运行CIRM信息中心。

当我收到主题行的电子邮件:“运行仪表板”时,Excel文件无法打开。没有错误。

我需要根据Outlook主题行打开更多文件。

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

        If InStr(Item.Subject, "Run CIRM Dashboard") Then

            Call CIRMDashboard
        If InStr(Item.Subject, "Run Dashboard") Then

        Call PendingForRefund

        End If

        End If

    End If   
End Sub


Public Function CIRMDashboard()
Dim xlapp As Object
Dim sourceWB
Dim sourceWS

Set xlapp = CreateObject("Excel.Application")
With xlapp
    .Visible = True
    .EnableEvents = True
End With

strFile = "C:\Users\Dharmendra\Desktop\CIRM Dashboard Auto_Runner.xlsm" '\\ my fist file path
Set sourceWB = xlapp.Workbooks.Open(strFile) ', , False, , , , , , , True)

End Function


Public Function PendingForRefund()
Dim xlapp As Object
Dim sourceWB
Dim sourceWS

Set xlapp = CreateObject("Excel.Application")
With xlapp
    .Visible = True
    .EnableEvents = True
End With

strFile = "C:\Users\Dharmendra\Desktop\CRM Dashboard Auto_Runner.xlsm" ' \\my second file path
Set sourceWB = xlapp.Workbooks.Open(strFile) ', , False, , , , , , , True)

End Function

1 个答案:

答案 0 :(得分:0)

看看你的If构造......正确的缩进将有助于可视化真正发生的事情......

Private Sub Items_ItemAdd(ByVal Item As Object)

    If TypeOf Item Is Outlook.MailItem Then

        If InStr(Item.Subject, "Run CIRM Dashboard") Then

            Call CIRMDashboard

            If InStr(Item.Subject, "Run Dashboard") Then

                Call PendingForRefund

            End If

        End If

    End If   

End Sub

&#34;运行仪表板&#34; If里面运行&#34;运行CIRM&#34;控制板&#34; If ...所以你永远不会遇到这种情况(除非主题为#34; 的邮件运行CIRM仪表板,然后运行仪表板&#34;或收到喜欢的邮件。< / p>

您可能希望将第二个If替换为Else If,以使其达到同一水平