使用下面的代码,当我尝试将附件文件名添加到报告中时,出现错误。也许这是语法?
错误发生在 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
不同的变量名称对于每个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
关于vba - 将电子邮件附件名称添加到我的报告 Outlook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52672539/