2

我正在尝试创建一个 VBA 宏,根据电子邮件地址将电子邮件附件保存到文件夹中。例如,如果我从 joey@me.com 收到附件并通过电子邮件发送,我想将该附件保存到目录 \server\home\joey 或者如果我从 steve@me.com 收到附件,则附件应保存在 \server \家\史蒂夫。

最后,我想发送一封回复电子邮件,其中包含已保存文件的名称。我发现一些代码几乎可以满足我的要求,但我很难修改它。这一切都在 Outlook 2010 中完成。这就是我目前所拥有的。任何帮助将不胜感激

Const mypath = "\\server\Home\joe\"
Sub save_to_v()

    Dim objItem As Outlook.MailItem
    Dim strPrompt As String, strname As String
    Dim sreplace As String, mychar As Variant, strdate As String
    Set objItem = Outlook.ActiveExplorer.Selection.item(1)
    If objItem.Class = olMail Then

        If objItem.Subject <> vbNullString Then
            strname = objItem.Subject
        Else
            strname = "No_Subject"
        End If
        strdate = objItem.ReceivedTime

        sreplace = "_"

        For Each mychar In Array("/", "\", ":", "?", Chr(34), "<", ">", "|")

            strname = Replace(strname, mychar, sreplace)
            strdate = Replace(strdate, mychar, sreplace)
        Next mychar

        strPrompt = "Are you sure you want to save the item?"
        If MsgBox(strPrompt, vbYesNo + vbQuestion) = vbYes Then
            objItem.SaveAs mypath & strname & "--" & strdate & ".msg", olMSG
        Else
            MsgBox "You chose not to save."
        End If
    End If
End Sub
4

2 回答 2

1

这是你正在尝试的吗?(未经测试

Option Explicit

Const mypath = "\\server\Home\"

Sub save_to_v()

    Dim objItem As Outlook.MailItem
    Dim strPrompt As String, strname As String, strSubj As String, strdate As String
    Dim SaveAsName As String, sreplace As String
    Dim mychar As Variant

    Set objItem = Outlook.ActiveExplorer.Selection.Item(1)

    If objItem.Class = olMail Then

        If objItem.Subject <> vbNullString Then
            strSubj = objItem.Subject
        Else
            strSubj = "No_Subject"
        End If

        strdate = objItem.ReceivedTime

        sreplace = "_"

        For Each mychar In Array("/", "\", ":", "?", Chr(34), "<", ">", "|")
            strSubj = Replace(strSubj, mychar, sreplace)
            strdate = Replace(strdate, mychar, sreplace)
        Next mychar

        strname = objItem.SenderEmailAddress

        strPrompt = "Are you sure you want to save the item?"

        If MsgBox(strPrompt, vbYesNo + vbQuestion) = vbYes Then
            Select Case strname
            Case "joey@me.com"
                SaveAsName = mypath & "joey\" & strSubj & "--" & strdate & ".msg"
            Case "steve@me.com"
                SaveAsName = mypath & "steve\" & strSubj & "--" & strdate & ".msg"
            End Select

            objItem.SaveAs SaveAsName, olMSG
        Else
            MsgBox "You chose not to save."
        End If
    End If
End Sub
于 2012-04-20T09:01:08.617 回答
0

它永远不会起作用。由于 Outlook 2010 没有将任何 msg 文件保存到网络驱动器,因此只有本地驱动器正在工作!如 M$ 的文档中所述并由我测试。具有固定路径和文件名的简单测试。本地 c:\ 有效。UNC 或 L 中的网络驱动器:不起作用!!!!

于 2013-07-22T12:04:10.423 回答