【发布时间】:2019-04-24 14:56:46
【问题描述】:
我有这段代码可以浏览 Outlook 中“今天”文件夹中的所有电子邮件,然后将所有电子邮件 (.msg) 保存到名为发件人名称的文件夹中。
有时文件会以文件类型“文件”保存。
如何解决此问题以确保将电子邮件保存为 .msg 文件?
Sub SaveAttachments()
'https://www.fontstuff.com/outlook/oltut01.htm
'Declare Variables
Dim ns As NameSpace
Dim Inbox As Outlook.MAPIFolder
Dim Item As Object
Dim Atmt As Attachment
Dim Savefolder As String
Dim i As Integer
Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox).Folders("Today")
i = 0
'Stop script if there are no emails
If Inbox.Items.Count = 0 Then
MsgBox "There are no messages in the Inbox.", vbInformation, "Nothing Found"
Exit Sub
End If
'Display the number of emails
MsgBox Inbox.Items.Count, vbInformation, _
"Number of Emails?"
'Go through each email
For Each Item In Inbox.Items
'Create a path for the save folder
Savefolder = "C:\Users\work\Desktop\22_11_18\Test\" & Item.SenderName
'If the email has attachments, then create a folder
If Item.Attachments.Count > 0 Then
MkDir Savefolder
'If the folder already exists, skip to the next statement
On Error Resume Next
'Save the email as a .msg file
Item.SaveAs Savefolder & "\" & Item.Subject & ".msg"
End If
Next Item
End Sub
【问题讨论】:
-
如果您删除
On Error Resume Next,您会收到错误消息吗?我也没有看到任何迹象表明您正在删除路径中无效的字符。如“回复:”中的: -
有时我会收到一个错误,因为该文件夹已经存在并且脚本尝试创建一个具有相同名称的新文件夹(有时我会收到来自同一发件人的多封电子邮件);我发现使用 On Error 可以让我克服这个问题。我根本没想到!我会调整脚本来解决这个问题,希望这就是原因。
-
我把
.Subject改成了.ConversationID,问题已经解决了。非常感谢! -
On Error Resume Next 应该总是在可能导致良性或可恢复错误的行之后跟随一个后续 On Error Goto 0。