【问题标题】:Error trying to save email attachment尝试保存电子邮件附件时出错
【发布时间】:2020-06-26 06:41:26
【问题描述】:

我尝试编写一些 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

脚本进入this if时触发错误

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

任何建议

提前致谢

【问题讨论】:

  • 为什么这个标签是visual studio?
  • Msg 这是什么?你似乎少了一两步。
  • 这段代码的问题让我很头疼。我的第一个有助于消除大部分错误的建议是在这个模块中使用Option Explicit。这将阻止您尝试使用 msgitem 现在的未声明变量

标签: vba outlook


【解决方案1】:

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

所以您只是想在收件箱中导航以查找特定项目,对吗?只是添加循环会产生另一个问题。收件箱中的某些项目不是邮件项目。为了解决这个问题,我们浏览收件箱中的每个对象并检查我们遇到的每个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 

这主要是偏好,但如您所见,我进行了一些其他编码更改。我重命名了其他一些变量,以便对它的对象更具描述性。还将所有Dims 和Const 一起移动以提高可读性。

最后一件事。看起来您正在浏览您的整个收件箱以寻找一小部分邮件。您可以创建一个规则,在这些邮件进入您的邮箱时对其进行处理。例如:Save Outlook attachment to disk

【讨论】:

    【解决方案2】:
    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
    

    【讨论】:

      猜你喜欢
      • 2019-01-11
      • 1970-01-01
      • 2017-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-12
      • 2021-06-26
      相关资源
      最近更新 更多