【问题标题】:Adding email attachment name to my report Outlook将电子邮件附件名称添加到我的报表 Outlook
【发布时间】:2020-06-21 15:15:50
【问题描述】:

使用下面的代码,当我尝试将附件文件名添加到报告中时出现错误。也许是语法?

错误发生在Report = Report & currentItem.Attachments.FileName

错误是“对象不支持此属性或方法。

有什么想法吗?

我正在 Outlook 中运行此代码,

Private Sub GetAllEmailsInFolder(CurrentFolder As Outlook.Folder, Report As String)
    Dim currentItem
    Dim attachment As attachment
    Dim currentMail As MailItem

    Report = Report & "Folder Name: " & CurrentFolder.Name & " (Store: " & CurrentFolder.Store.DisplayName & ")" & vbCrLf

    For Each currentItem In CurrentFolder.Items
        Report = Report & currentItem.Subject
        Report = Report & vbCrLf
        Report = Report & "----------------------------------------------------------------------------------------"
        Report = Report & vbCrLf
        Report = Report & currentItem.Attachments.FileName

    Next

End Sub

另外,我首先运行一个获取电子邮件列表的子程序:

Public Sub GetListOfEmails()
    'On Error GoTo On_Error

    Dim Session As Outlook.NameSpace
    Dim Report As String
    Dim Folder As Outlook.Folder

    Set Session = Application.Session

    Set Folder = Application.ActiveExplorer.CurrentFolder

    Call GetAllEmailsInFolder(Folder, Report)

    Dim retValue As Boolean
    retValue = CreateReportAsEmail("List of Emails", Report)

Exiting:
        Set Session = Nothing
        Exit Sub
On_Error:
    MsgBox "error=" & Err.Number & " " & Err.Description
    Resume Exiting

End Sub

然后这是我用来以电子邮件的形式创建报告的子,我想复制到 excel 中。

Public Function CreateReportAsEmail(Title As String, Report As String)
    'On Error GoTo On_Error

    Dim Session As Outlook.NameSpace
    Dim mail As MailItem
    Dim MyAddress As AddressEntry
    Dim Inbox As Outlook.Folder

    CreateReportAsEmail = True

    Set Session = Application.Session
    Set Inbox = Session.GetDefaultFolder(olFolderInbox)
    Set mail = Inbox.Items.Add("IPM.Mail")

    Set MyAddress = Session.CurrentUser.AddressEntry
    mail.Recipients.Add (MyAddress.Address)
    mail.Recipients.ResolveAll

    mail.Subject = Title
    mail.Body = Report

    mail.Save
    mail.Display


Exiting:
        Set Session = Nothing
        Exit Function
On_Error:
    CreateReportAsEmail = False
    MsgBox "error=" & Err.Number & " " & Err.Description
    Resume Exiting

End Function

【问题讨论】:

  • Attachments 集合没有文件名属性 - 每个 Attachment 都有
  • @BigBen 哦,我明白了,所以也许我需要创建一个函数来提取附件文件名?我将使用报告作为指标。这个收件箱收到的附件实际上只有几种不同的类型。我想知道他们按月细分
  • 您可以遍历Attachments 集合并从每个单独的附件中提取文件名。
  • @BigBen 无论如何,您有时间快速绘制它吗?我不确定我会怎么做。理想情况下,每个附件的名称都是我想要的。

标签: vba outlook


【解决方案1】:
  • Attachments 集合没有Filename 属性,但每个个人 Attachment 都有。通过 Attachments 集合添加一个额外的循环。
  • GetAllEmailsInFolder 应该是 Function 返回 StringSub 做某事; Function 返回一些东西。
  • GetAllEmailsInFolder 假设 CurrentFolder 中的所有项目都是 MailItems,但可能不是
  • 为每个Attachment 使用与attachment 不同的变量名称。 Folder, Session...

未经测试,但类似这样:

Private Function GetAllEmailsInFolder(CurrentFolder As Outlook.Folder) As String
    Dim currentItem As Object
    Dim myAttachment As Attachment
    Dim Report as String

    Report = Report & "Folder Name: " & CurrentFolder.Name & " (Store: " & CurrentFolder.Store.DisplayName & ")" & vbCrLf

    For Each currentItem In CurrentFolder.Items
        If TypeOf currentItem Is Outlook.MailItem Then
            Report = Report & currentItem.Subject
            Report = Report & vbCrLf
            Report = Report & "----------------------------------------------------------------------------------------"
            Report = Report & vbCrLf
            For Each myAttachment in currentItem.Attachments
                Report = Report & myAttachment.Filename ' and add formatting inbetween as needed
            Next myAttachment
        End If
    Next currentItem

    GetAllEmailsInFolder = Report
End Sub

【讨论】:

    【解决方案2】:

    这是一个简单的示例,您可能需要对它在电子邮件中的显示方式稍作调整。

        For Each currentItem In CurrentFolder.Items
            Report = Report & currentItem.Subject
            Report = Report & vbCrLf
            Report = Report & "--------------------------------------------------------"
            Report = Report & vbCrLf
    '        Report = Report & currentItem.Attachments.FileName
    
            For Each attachment In currentItem.Attachments
                Debug.Print attachment.FileName
                Report = Report & attachment.FileName
            Next
    
        Next
    

    MSDN Attachment Object (Outlook)

    【讨论】:

    • 很抱歉在其核心添加了本质上相同的答案,但我不得不指出其他问题并且正在起草中:)
    • @BigBen 很酷的+1v,很好answer
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-21
    • 1970-01-01
    • 2017-09-08
    • 2016-05-15
    • 1970-01-01
    相关资源
    最近更新 更多