Solved

Further Modifications to Outlook Macro to save Attachment to Network Folder

Posted on 2006-11-15
4
387 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
ID: 17947377
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
ID: 17947399
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
ID: 17950545
works awesome- thanks BDF!
0
 
LVL 76

Expert Comment

by:David Lee
ID: 17951016
No problem.  Always happy to help out.
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Are you unable to connect or configure Hotmail email account in Microsoft Outlook 2010, 2007? Or Outlook.com emails are not downloading to Outlook? Lets’ see the problem and resolve Outlook Connector error syncing folder hierarchy (0x8004102A).
This article lists the top 5 free OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their exchange server is no longer available or some other critical issue with exchange server or impor…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

791 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