vba - 将电子邮件附件名称添加到我的报告 Outlook

标签 vba outlook

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

错误发生在 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 集合没有Filename属性(property),但每个个人 Attachment做。通过 Attachments 添加一个额外的循环收藏。
  • GetAllEmailsInFolder应该是Function返回 String 。一个Sub做某事;一个Function 返回一些东西。
  • GetAllEmailsInFolder假设 CurrentFolder 内的所有项目是 MailItem s,情况可能并非
  • 使用与 attachment 不同的变量名称对于每个 AttachmentFolder 也是如此, 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

关于vba - 将电子邮件附件名称添加到我的报告 Outlook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52672539/

相关文章:

html - css max-width 属性和外观

html - 为正文中的联系人创建带有@Mention 的 Outlook 电子邮件

vba - Worksheet_BeforeDoubleClick 进行选择

vba - 在自定义数字格式 VBA 中使用变量

excel - 如何在数字到文本转换中保留尾随零?

vba - Excel 粘贴到可见、转置为链接组合功能

performance - 为什么 Outlook 关闭时 Excel VBA 的运行速度明显加快?

javascript - 无法从 Outlook 功能区打开链接是 javascript API 而不是弹出窗口

regex - VBA RegExp 在 vbscript.regexp 工作时导致编译错误

vba - 在 Word 中更改自定义文档属性