检查 Outlook 收到的电子邮件中是否存在 Excel 附件

检查 Outlook 收到的电子邮件中是否存在 Excel 附件

我试图使用下面的代码创建一个宏,检查 Outlook 文件夹中带有 excel 附件的所有未读电子邮件。之后,我希望它运行我在 excel 中创建的另一个宏。我遇到的问题是,使用下面的代码,我不断收到运行时错误

‘424’:需要对象。

我将非常感激任何对此提供帮助的帮助。

Sub attach()
Dim myNameSpace As NameSpace
Dim myFolder As Folder
Dim myAttachment As Outlook.Attachment
Dim xlApp As Object
Dim xlWB As Object
Dim xlWB2 As Object
Const File_Path As String = ""

Set myNameSpace = Application.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox).Folders("Reports")

On Error Resume Next
    Set xlApp = GetObject(, "Excel.Application")
    If xlApp Is Nothing Then Set xlApp = CreateObject("Excel.Application")
On Error GoTo 0
xlApp.Visible = True

For Each myAttachment In myItem.Attachments
    If InStr(myAttachment.DisplayName, "Detail") > 0 Then
        myAttachment.SaveAsFile File_Path & myAttachment.FileName
        Set xlWB = xlApp.Workbooks.Open(File_Path & myAttachment.FileName)
    End If
Next myAttachment

xlWB2.Application.Run "ExportToPDF"

End Sub

当我到达以下部分时收到错误消息:

对于 myItem.Attachment 中的每个 myAttachment

答案1

myItem对象不存在。

Set myUnreadItems = myFolder.items.Restrict("[UnRead] = True")

If myUnreadItems.count = 0 Then
    MsgBox "No unread mail in: " & myFolder
    Exit Sub
End If

For i = myUnreadItems.count To 1 Step -1     
    Set myItem = myUnreadItems(i)

相关内容