Save Email Attachments to a Network location

懵懂的女人 提交于 2019-12-11 01:03:32

问题


I’m trying to create a VBA macro that saves an email attachment to folder depending on the email address. For example if I receive and email with an attachment from joey@me.com I want to save that attachment to the directory \server\home\joey or if I receive it from steve@me.com the attachment should be saved in \server\home\steve .

And finally I want send a reply email with the name of the file that has been saved. I found some code that almost does what I want but I having a difficult time modifying it. This is all being done in Outlook 2010. This is what I have so far. Any help would be greatly appreciated

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

回答1:


Is this what you are trying? (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



回答2:


It will never work. As Outlook 2010 is not saving any msg file to a network drive only local drive is working !! As described in the documentation of M$ and tested by me. Simple test with fixed path and filename. Local c:\ works. Network drive in UNC or L: does not work !!!!



来源:https://stackoverflow.com/questions/10241659/save-email-attachments-to-a-network-location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!