【问题标题】:Saving Outlook Emails as ".msg" not as "File"将 Outlook 电子邮件另存为“.msg”而不是“文件”
【发布时间】: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。

标签: vba outlook


【解决方案1】:

如果主题中的字符都有效,则可以使用主题。

Option Explicit

Private Sub SaveMail_ContainingAttachments_ValidSubject()

'Declare Variables
Dim ns As Namespace
Dim targetFolder As Folder
Dim itm As Object
Dim atmt As Attachment

Dim strSaveFolder As String
Dim validSubject As String

Set ns = GetNamespace("MAPI")
Set targetFolder = ns.GetDefaultFolder(olFolderInbox)
Set targetFolder = targetFolder.Folders("Today")

'Stop script if there are no emails
If targetFolder.Items.count = 0 Then
    MsgBox "There are no messages in " & targetFolder & ".", vbInformation, "Nothing Found"
    Exit Sub
End If

'Display the number of emails
MsgBox targetFolder.Items.count, vbInformation, "Number of Emails?"

'Go through each email
For Each itm In targetFolder.Items

    'If the email has attachments, then create a folder
    If itm.Attachments.count > 0 Then

        'Create a path for the save folder
        strSaveFolder = "C:\Users\work\Desktop\22_11_18\Test\" & itm.senderName

        ' Bypass error if the folder already exists
        On Error Resume Next
        MkDir strSaveFolder

        ' Discontinue error bypass as soon as the purpose is served
        ' Let unknown errors generate then fix them
        On Error GoTo 0

        ' Replace or remove invalid characters
        ' Possible options "_" or " " or "" ....
        validSubject = ReplaceIllegalChar(itm.subject, "_")
        If validSubject <> itm.subject Then
            Debug.Print itm.subject
            Debug.Print validSubject
        End If

        'Save the email as a .msg file
        itm.SaveAs strSaveFolder & "\" & validSubject & ".msg"

    End If

Next itm

End Sub

Private Function ReplaceIllegalChar(strInput, strReplace)

    Dim RegX As Object

    Set RegX = CreateObject("vbscript.regexp")

    RegX.Pattern = "[\" & Chr(34) & "\!\@\#\$\%\^\&\*\(\)\=\+\|\[\]\{\}\`\'\;\:\<\>\?\/\,]"
    RegX.IgnoreCase = True
    RegX.Global = True

    ' Replace with another string
    ReplaceIllegalChar = RegX.Replace(strInput, strReplace)

ExitFunction:

    Set RegX = Nothing

End Function

【讨论】:

    猜你喜欢
    • 2014-12-19
    • 2015-05-02
    • 1970-01-01
    • 2021-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-03
    • 2014-10-25
    相关资源
    最近更新 更多