【发布时间】: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 无论如何,您有时间快速绘制它吗?我不确定我会怎么做。理想情况下,每个附件的名称都是我想要的。