2

大家早上好,

我希望有人可以在这里帮助我编写一段代码。

我希望将选定的电子邮件保存到特定目录,并带有电子邮件的名称,当然还有 .msg 文件。这就是我今天所拥有的,但它不起作用。它保存一个文件,但名称只有前 2 个字符(在分号文件名后看起来像错误,例如:FW 或 RE)...文件内容为空白且文件类型尚未应用。

'code to save selected email
Dim selectedEmail As MailItem
Set selectedEmail = ActiveExplorer.Selection.Item(1)
Dim emailsub As String
emailsub = ActiveExplorer.Selection.Item(1).Subject
    With selectedEmail
        .SaveAs "C:\direcotry\folder\" & emailsub & ".msg", olMSG
    End With

谢谢你的期待。多姆

4

1 回答 1

2

原因很简单。您的电子邮件主题包含无效字符。例如:,这通常发生在电子邮件是RE:FWD:

试试这个

Sub Sample()
    Dim selectedEmail As MailItem
    Dim emailsub As String

    Set selectedEmail = ActiveExplorer.Selection.Item(1)

    emailsub = GetValidName(selectedEmail.subject)

    'Debug.Print emailsub

    With selectedEmail
        .SaveAs "C:\direcotry\folder\" & emailsub & ".msg", OlSaveAsType.olMSG
    End With
End Sub

Function GetValidName(sSub As String) As String
    '~~> File Name cannot have these \ / : * ? " < > |
    Dim sTemp As String

    sTemp = sSub

    sTemp = Replace(sTemp, "\", "")
    sTemp = Replace(sTemp, "/", "")
    sTemp = Replace(sTemp, ":", "")
    sTemp = Replace(sTemp, "*", "")
    sTemp = Replace(sTemp, """", "")
    sTemp = Replace(sTemp, "<", "")
    sTemp = Replace(sTemp, ">", "")
    sTemp = Replace(sTemp, "|", "")

    GetValidName = sTemp
End Function
于 2013-10-10T09:54:12.363 回答