将电子邮件附件名称添加到我的报表Outlook

时间:2018-10-05 19:56:45

标签: vba outlook outlook-vba

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

错误发生在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

2 个答案:

答案 0 :(得分:3)

  • Attachments集合没有Filename属性,但是每个个体 Attachment都有。通过Attachments集合添加一个附加循环。
  • GetAllEmailsInFolder应该是返回Function的{​​{1}}。 String做某事; Sub 返回一些东西。
  • Function假设GetAllEmailsInFolder中的所有项目都是CurrentFolder,可能不是
  • 为每个MailItem使用不同于attachment的变量名。 AttachmentFolder ...
  • 也是如此

未经测试,但类似这样:

Session

答案 1 :(得分:1)

这里是一个简单的示例,您可能需要对其在电子邮件上的显示方式进行一些调整。

    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

more info on pruning the original XGB slides from 2014