将电子邮件附件保存到网络位置

时间:2012-04-20 07:00:06

标签: vba outlook outlook-vba

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

最后,我想发送一封回复电子邮件,其中包含已保存文件的名称。我发现一些代码几乎可以完成我想要的但是我很难修改它。这一切都在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

2 个答案:

答案 0 :(得分:1)

这是你在尝试什么? (的 UNTESTED

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

答案 1 :(得分:0)

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