+ Reply to Thread
Results 1 to 7 of 7

Extract msg attachments from outlook email

  1. #1
    Registered User
    Join Date
    06-09-2011
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    2

    Extract msg attachments from outlook email

    I have the following vba script that saves attachments in an email.

    This works fine for .docx, .jpg etc but I need to use it to extract multiple .msg attachments which doesn't work.

    The code is

    Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
    Dim objAtt As Outlook.Attachment
    Dim saveFolder As String

    saveFolder = "C:\Test\"
    For Each objAtt In itm.Attachments
    stFileName = saveFolder & "\" & objAtt.DisplayName
    i = 0
    JumpHere:
    If Dir(stFileName) = "" Then
    objAtt.SaveAsFile stFileName
    Else
    i = i + 1
    stFileName = saveFolder & "\" & i & " - " & objAtt.DisplayName
    GoTo JumpHere
    End If
    Set objAtt = Nothing
    Next
    End Sub

    The error concerns the line - If Dir(stFileName) = "" Then

  2. #2
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Extract msg attachments from outlook email

    I've adapted some code I use for deleting attachments which seems to work:

    Please Login or Register  to view this content.

    Please remember to use code tags when posting code to the forum per the forum rules.
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  3. #3
    Registered User
    Join Date
    06-09-2011
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Extract msg attachments from outlook email

    This is excellent thank you. Please excuse a silly question but how would I run this automatically as a script and not have to choose the folder?

  4. #4
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Extract msg attachments from outlook email

    No worries. What would you want to run it on?

    Dom

  5. #5
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Extract msg attachments from outlook email

    This has the same construct as the code you posted so maybe fits the bill:

    Please Login or Register  to view this content.

    I've also changed the position of the first check to see if the file already exists so it's not repeating it.

    Dom

  6. #6
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Extract msg attachments from outlook email

    I don't see any difference in functionality in those codes.

    To save the attachments of all the PostIN items into folder E:\ this suffices:

    Please Login or Register  to view this content.
    I you want to save the emails as 'msg' use:
    Please Login or Register  to view this content.
    Last edited by snb; 06-30-2011 at 11:59 AM.



  7. #7
    Registered User
    Join Date
    06-01-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: Extract msg attachments from outlook email

    I have edited the code slightly to only save .MSG attachments. Is there a way to open the .MSG attachments and email them to a different address instead of saving them?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1