使用以下代码,当我尝试向报告中添加附件文件名时出现错误。也许是语法?
错误发生在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
答案 0 :(得分:3)
Attachments
集合没有Filename
属性,但是每个个体 Attachment
都有。通过Attachments
集合添加一个附加循环。GetAllEmailsInFolder
应该是返回Function
的{{1}}。 String
做某事; Sub
返回一些东西。Function
假设GetAllEmailsInFolder
中的所有项目都是CurrentFolder
,可能不是 。 MailItem
使用不同于attachment
的变量名。 Attachment
,Folder
... 未经测试,但类似这样:
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