【问题标题】:Saving E-Mails with Meeting Invitation From Outlook从 Outlook 保存带有会议邀请的电子邮件
【发布时间】:2020-05-28 19:01:18
【问题描述】:

我有以下 VBA 代码宏以将所有选定的电子邮件以 .msg 格式保存到任何文件夹,但它不保存带有会议邀请的电子邮件。如何保存带有会议邀请的邮件?我必须包含任何特殊对象吗?下面是我用来以 .msg 格式保存电子邮件的代码:

Option Explicit

Public Sub SaveMessageAsMsg()

Dim xShell As Object
Dim xFolder As Object
Dim strStartingFolder As String
Dim xFolderItem As Object

Dim xMail As MailItem
Dim xObjItem As Object

Dim xPath As String
Dim xFileName As String
Dim xName As String
Dim xDtDate As Date

Set xShell = CreateObject("Shell.Application")

On Error Resume Next
' Bypass error when xFolder is nothing on Cancel
Set xFolder = xShell.BrowseForFolder(0, "Select a folder:", 0, strStartingFolder)
' Remove error bypass as soon as the purpose is served
On Error GoTo 0

If Not TypeName(xFolder) = "Nothing" Then
    Set xFolderItem = xFolder.Self
    xFileName = xFolderItem.Path
    ' missing path separator
    If Right(xFileName, 1) <> "\" Then xFileName = xFileName & "\"
Else
    xFileName = ""
    Exit Sub
End If

For Each xObjItem In ActiveExplorer.Selection

    If xObjItem.Class = olMail Then

        Set xMail = xObjItem

        xName = Left(CleanFileName(xMail.Subject), 100)
        Debug.Print xName

        xDtDate = xMail.ReceivedTime

        xName = Format(xDtDate, "yyyymmdd", vbUseSystemDayOfWeek, _
          vbUseSystem) & "-" & xName & ".msg"

        xPath = xFileName & xName

        xMail.SaveAs xPath, olMSG
    End If
Next

End Sub

Public Function CleanFileName(strFileName As String) As String

Dim Invalids
Dim e
Dim strTemp As String

Invalids = Array("?", "*", ":", "|", "<", ">", "[", "]", """", "/", "\")

strTemp = strFileName

For Each e In Invalids
    strTemp = Replace(strTemp, e, " ")
    'strTemp = Replace(strTemp, e, "")
Next

CleanFileName = strTemp

End Function

【问题讨论】:

    标签: vba outlook


    【解决方案1】:

    在上面发布的代码中,只处理邮件:

     If xObjItem.Class = olMail Then
    

    Outlook 中有多种对象类型,有关详细信息,请参阅OlObjectClass enumeration。

    基本上,您还需要处理会议项目:

    Dim xMail As Object
    If xObjItem.Class = olMail Or xObjItem.Class = olMeetingRequest Then
    
    

    【讨论】:

    • 我试过了,但我得到类型不匹配(错误 13)错误
    • 没关系,现在可以了。我没有将 xMail 澄清为对象。非常感谢。
    • 很高兴我的帖子有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-18
    相关资源
    最近更新 更多