Solved

Further Modifications to Outlook Macro to save Attachment to Network Folder

Posted on 2006-11-15
4
382 Views
Last Modified: 2010-04-08
Blue DevilFan provided me with the code below which saves the attachment of the current email message to a network drive.  It works great.  As I was using it I was thinking that it would also be fantastic if I could have a similar macro that when fired processed all emails in the current folder (instead of one at a time).   The folder in question is a public folder.

 Can it be done?


Sub SaveAttachment()
    Dim olkMessage As Outlook.MailItem, _
        olkAttachment As Outlook.Attachment, _
        objFSO As Object, _
        myOrt As String, _
        myPath As String
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    myOrt = "J:\Programming\"
    For Each olkMessage In Application.ActiveExplorer.Selection
        myPath = myOrt & olkMessage.SenderName
        If Not objFSO.FolderExists(myPath) Then
            objFSO.CreateFolder (myPath)
        End If
        myPath = myPath & "\" & Format(olkMessage.SentOn,"MM-DD-YYYY")
       If Not objFSO.FolderExists(myPath) Then
            objFSO.CreateFolder (myPath)
        End If
        myPath = myPath & "\"
        If olkMessage.Attachments.Count > 0 Then
            olkMessage.HTMLBody = olkMessage.HTMLBody & "<br><br><b>Saved Attachments</b><br>"
            For Each olkAttachment In olkMessage.Attachments
                With olkAttachment
                    .SaveAsFile myPath & olkAttachment.FileName
                    olkMessage.HTMLBody = olkMessage.HTMLBody & "<a href=""file://" & myPath & olkAttachment.FileName & """>" & olkAttachment.FileName & "</a><br>"
                    .Delete
                End With
            Next
            olkMessage.Save
        End If
    Next
    Set objFSO = Nothing
    Set olkAttachment = Nothing
    Set olkMessage = Nothing
End Sub
0
Comment
Question by:snyperj
  • 2
4 Comments
 
LVL 76

Accepted Solution

by:
David Lee earned 500 total points
Comment Utility
This macro will process all the messages selected, not just one message.  You could select whichever messages you want and it'd process them all.  If you want to save yourself the step of selecting messages, then change this line

    For Each olkMessage In Application.ActiveExplorer.Selection

to

    For Each olkMessage In Application.ActiveExplorer.CurrentFolder.Items

Now when you run the macro it will process all messages in whatever folder you have selected.
0
 
LVL 35

Expert Comment

by:mvidas
Comment Utility
Hi again,

'    For Each olkMessage In Application.ActiveExplorer.Selection
    For Each olkMessage In Application.ActiveExplorer.CurrentFolder.Items

I would recommend you not use the J drive for the network drive and instead use the UNC reference (\\server\folder\etc), in case a user doesn't have that drive mapped or it changes for whatever reason.

Matt
0
 

Author Comment

by:snyperj
Comment Utility
works awesome- thanks BDF!
0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
No problem.  Always happy to help out.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

My experience with Windows 10 over a one year period and suggestions for smooth operation
Email signatures have numerous marketing benefits. Here are 8 top reasons to turn your email signature into a marketing channel.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now