More Outlook Resource Sites

Microsoft Developer Network (MSDN)

FAQs and other general resources

forum 24-Feb-2017 05:43

Looking for help with Outlook programming projects — VSTO, add-ins, VBA, custom Outlook forms, etc.? You′ve come to the right place!

NEW! >> Subscribe to this site via RSS. For more RSS options, see the complete list of feeds on our main news page.

    Page [ 1 ]  
 Outlook and .NET Printer Friendly Version
Writing code with C# and VB.NET to create Outlook add-ins and other projects
VB Marco - save PDF attachments to specific network folder outlook 2010
Good Morning Gentlemen,

I am trying to create a macro that will take incoming emails with attachments and save them to a new network folder according to the attachment.

For example, i have an email coming from Joey Lawrence with the attachment name Whoa123.pdf. I want the macro to take that file and put it into a folder \\server\emailattachments\whoa123\whoa123.pdf

Then another email from Martin Lawrence with the attachment hi123.pdf, have the macro put that file into a folder \\server\emailattachments\hi123\hi123.pdf

Please advise, thank you!

  18-Feb-2013  11:15
  18-Feb-2013  22:14   
  21-Feb-2013  17:31   
' Set up a rule on incoming mail to "run a script"

Sub SaveAttachments_VariableFolder(MyMail As MailItem)

Dim Atmt As attachment

Dim FileName As String
Dim lenName As Long
Dim strPathAdd As String

Const strPath As String = "C:\test\" ' set as desired

On Error Resume Next
MkDir strPath
On Error GoTo 0

For Each Atmt In MyMail.Attachments

    If (Right(Atmt, Len(Atmt) - InStrRev(Atmt, "."))) = "pdf" Then
        lenName = InStrRev(Atmt, ".") - 1
        ' Trim possible spaces before the extension.
        ' A space at the end of the name created a problem with deleting the folder
        strPathAdd = strPath & Trim(Left(Atmt, lenName)) & "\"
        On Error Resume Next
        MkDir strPathAdd
        On Error GoTo 0

        FileName = strPathAdd & Atmt.FileName
        Atmt.SaveAsFile FileName
    End If
Next Atmt

Set Atmt = Nothing
End Sub
  22-Feb-2013  08:24   
an unexpected error has occurred.
Please note and export the following information.
Macro name: SaveEmailAttachments ToFolder
Error Number: -2147221233
Error Description: The attempted operation failed. An object could not be found.
  22-Feb-2013  08:40   
nevermind it worked, thank you so much!!!!!!!!!!!!!
  22-Feb-2013  14:37   
so this script is great but is there a way that we can have it create a folder with the name email address of the sender?

So if the attached file is coming from, create a folder called and put the attached file in there?

please let me know, thank you very much.
  27-Feb-2013  17:22   
You will want the SMTP address.
    Page [ 1 ]