Vba 尝试保存电子邮件附件时出错

Vba 尝试保存电子邮件附件时出错,vba,outlook,Vba,Outlook,我尝试编写一些VBA来将附件文件从某个电子邮件保存到一个文件夹中 但是我得到了错误 运行时错误“424” 所需对象 这就是我试图使用的代码 Sub test_extraer() Dim olApp As Outlook.Application Dim objNS As Outlook.NameSpace Set olApp = Outlook.Application Set objNS = olApp.GetNamespace("MAPI") Set Items = objNS.GetDefau

我尝试编写一些VBA来将附件文件从某个电子邮件保存到一个文件夹中 但是我得到了错误

运行时错误“424”

所需对象

这就是我试图使用的代码

Sub test_extraer()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set Items = objNS.GetDefaultFolder(olFolderInbox).Items

If (Msg.SenderName = "sender@email.com") And _
       (Msg.Subject = "subject of the email") And _
   (Msg.Attachments.Count >= 1) Then

    'Set folder to save in.
    Dim olDestFldr As Outlook.MAPIFolder
    Dim myAttachments As Outlook.Attachments
    Dim Att As String

    Const attPath As String = "C:\temp\"

   Set myAttachments = item.Attachments
    Att = myAttachments.item(1).DisplayName
    myAttachments.item(1).SaveAsFile attPath & Att
End If

End Sub
当脚本输入此if时,将触发该错误

If (Msg.SenderName = "sender@email.com") And _
       (Msg.Subject = "subject of the email") And _
       (Msg.Attachments.Count >= 1) Then
有什么建议吗

提前谢谢

好的。。。从哪里开始。 你肯定有一些基本问题需要解决。您有几个未声明的变量。第一个原因是你的头衔。上下文中的msg很可能是Outlook.MailItem。仅仅声明变量并不是问题的唯一来源。接下来,您将看到一个类似于上下文中的msg的项目,它应该是Outlook.MailItem。您缺少一个循环,该循环也可以浏览收件箱中的所有项目

所以你只是想在收件箱中导航,寻找一个特定的项目,对吗?仅仅添加循环就会产生另一个问题。收件箱中的某些项目不是邮件项目。为了解决这个问题,我们浏览收件箱中的每个对象,并检查我们遇到的每个邮件项目。如果符合发件人、主题和项目数的条件,我们将继续将.SaveAsFile保存到目标目录

Sub Test_ExtraER()

    Const strAttachmentPath As String = "C:\temp\"

    Dim olApp As Outlook.Application
    Dim objNS As Outlook.NameSpace
    Dim objFolder As Outlook.MAPIFolder
    Dim objItem As Object
    Dim strFileName As String

    Set olApp = Outlook.Application
    Set objNS = olApp.GetNamespace("MAPI")
    Set objFolder = objNS.GetDefaultFolder(olFolderInbox)

    For Each objItem In objFolder.Items
        If TypeName(objItem) = "MailItem" Then
            If (objItem.Attachments.Count >= 1) And (objItem.Subject = "Some Subject") And (objItem.SenderName = "sender@email.com") Then
                With objItem.Attachments.Item(1)
                    strFileName = strAttachmentPath & .DisplayName
                    Debug.Print strFileName
                    .SaveAsFile strFileName
                End With
            End If
        End If
    Next
End Sub 
这主要是首选项,但正如您所看到的,我做了一些其他编码更改。我重命名了一些其他变量,使其更能描述对象。还将所有DIM和Const移到一起以提高可读性


最后一件事。你似乎在浏览整个收件箱,寻找一小部分邮件。您可以创建一个规则,在这些邮件进入您的邮箱时对其进行处理。这方面的一个例子是:

为什么这个标签是visual studio?这里是什么?你似乎错过了一两步。这个代码的问题伤到了我的头。我的第一个建议是在这个模块中使用optionexplicit,这将有助于消除大多数错误。这将阻止您尝试使用未声明的变量,而这正是msg和item现在使用的变量
Sub test_extraer()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim MailItems As Outlook.MAPIFolder 'Add this one
Dim Msg As Outlook.MailItem 'Add this one
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set MailItems = objNS.GetDefaultFolder(olFolderInbox)

For Each Msg In MailItems.Items 'loop thru the inbox folder to match the exact sender name and subject
    If (Msg.SenderName = "Sender Name Here") And _
           (Msg.Subject = "Subject Here") And _
       (Msg.Attachments.Count >= 1) Then


        'Set folder to save in.
        Dim olDestFldr As Outlook.MAPIFolder
        Dim myAttachments As Outlook.Attachments
        Dim Att As String

        Const attPath As String = "C:\temp\"

       Set myAttachments = Msg.Attachments
        Att = myAttachments.Item(1).DisplayName
        myAttachments.Item(1).SaveAsFile attPath & Att
    End If
Next
End Sub