Results 1 to 13 of 13

Thread: Excel -VB - Opening Microsoft Outlook attachments from saved .msg files (new problem)

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    4

    Excel -VB - Opening Microsoft Outlook attachments from saved .msg files (new problem)

    Hello,

    I have a large number of emails all saved in individual files. Each of
    these emails contains 2 or more attachments (all excel spreadsheets). I am looking to write a macro in excel whereby the user can select a saved email
    and it will automatically open all of the attachments. Any help would
    be greatly appreciated.

    Thanks in advance,
    Rich
    Last edited by ShootMe; May 3rd, 2006 at 05:58 AM.

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Excel -VB - Opening Microsoft Outlook attachments from saved .msg files

    Welcome to the Forums

    A couple of questions.

    1/ What format are the file saved as?
    2/ Have you written any code so far? - if so, plase post it.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    4

    Re: Excel -VB - Opening Microsoft Outlook attachments from saved .msg files

    Thanx for the welcome

    The emails I am trying to get the attachments from are all .msg extensions

    The attachments themselves are all excel files .xls

    I have not written any code as of yet but I will probably start by using a GetFilname application to get the user to chose the email file.

    VB Code:
    1. fileToOpen = Application.GetOpenFilename("Outlook item (*.msg), *.msg")
    2.  
    3. If fileToOpen <> False Then
    4.     Workbooks.Open Filename:=fileToOpen
    5. End If

    My aim is to open all the attachments from the chosen email so I can further manipulate the individual spreadsheets.

    Thanxs for you interest,
    Rich

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel -VB - Opening Microsoft Outlook attachments from saved .msg files

    Here is one of my code examples on saving an email attachment to the file system using Outlook.

    http://www.vbforums.com/showthread.php?t=347353
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Excel -VB - Opening Microsoft Outlook attachments from saved .msg files

    Opening MailItems that have been saved as .msg files is really difficult. One easy work around it to create a copy of the file using the CreateItemFromTemplate method of the Outlook application. This makes a copy of the msg file that you can work with in subsequent code.
    Here's a procedure that should give you what you need. I've used the FileDialog object rather than the GetOpenFilename method, as it has move flexibility.
    Note: I wrote this in Excel, and you will need to add a reference to the Outlook library.

    VB Code:
    1. Option Explicit
    2.  
    3. Sub ShootMe()
    4. Dim sMessagePath As String
    5. Dim sSavePath As String
    6. Dim OLApp As Outlook.Application
    7. Dim oMessage As Outlook.MailItem
    8. Dim oMsgAttach As Outlook.Attachment
    9.  
    10.     '----------------------------------------------------------
    11.     'First get the path for the .msg file
    12.     '----------------------------------------------------------
    13.    
    14.     With Application.FileDialog(msoFileDialogFilePicker)
    15.        
    16.         'only allow a single file to be selected
    17.         .AllowMultiSelect = False
    18.        
    19.         .Title = "Select Message File"
    20.        
    21.         'Filter the file list to only
    22.         'include .msg files
    23.         With .Filters
    24.             .Clear
    25.             .Add "Message Files", "*.msg"
    26.         End With
    27.        
    28.         'If the user selects a file
    29.         'record the path,
    30.         'otherwise quit execution
    31.         If .Show = -1 Then
    32.             sMessagePath = .SelectedItems(1)
    33.         Else
    34.             Exit Sub
    35.         End If
    36.     End With
    37.    
    38.     '----------------------------------------------------------
    39.     'Next, get the save path
    40.     '----------------------------------------------------------
    41.    
    42.     With Application.FileDialog(msoFileDialogFolderPicker)
    43.         .AllowMultiSelect = False
    44.         .Title = "Select Save Folder"
    45.        
    46.         If .Show = -1 Then
    47.             sSavePath = .SelectedItems(1)
    48.         Else
    49.             Exit Sub
    50.         End If
    51.     End With
    52.    
    53.     '----------------------------------------------------------
    54.     'Now we need to open the message and export the attachments
    55.     '----------------------------------------------------------
    56.    
    57.     'Create Outlook objects
    58.     Set OLApp = New Outlook.Application
    59.     Set oMessage = OLApp.CreateItemFromTemplate(sMessagePath)
    60.    
    61.     'Loop through each attachment...
    62.     For Each oMsgAttach In oMessage.Attachments
    63.    
    64.         With oMsgAttach
    65.             '... saving it to the destination folder
    66.             .SaveAsFile Path:=sSavePath & "\" & .DisplayName
    67.         End With
    68.     Next oMsgAttach
    69.    
    70.     'Clear object variables
    71.     Set OLApp = Nothing
    72.     Set oMessage = Nothing
    73.     Set oMsgAttach = Nothing
    74. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel -VB - Opening Microsoft Outlook attachments from saved .msg files

    Isnt that basically the same as my code example. I used CreateItemFromTemplate.

    You also need to filter out the invalid characters in the SaveAsFile.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Excel -VB - Opening Microsoft Outlook attachments from saved .msg files

    Rob
    Yes its basically the same hack. To be honest, I didn't read all your referenced thread before I posted, as It didn't refer to a saved .msg file.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel -VB - Opening Microsoft Outlook attachments from saved .msg files

    I know it takes time to read all a thread or code example as I am guilty of the same thing from time to time. My link had attached email messages with attachments in them so that code example was specific.

    I know about the FileDialog in other Office apps but never had the need for it in Outlook.

    Nice touch as always.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    4

    Re: Excel -VB - Opening Microsoft Outlook attachments from saved .msg files

    Thank guys you have been a massive help.

    I think I’ll stick around these forums - I'm relearning Visual Basic after a long break and I'm loving the standard to code posted in these forums.

    Thanx again guys and seeya around

  10. #10

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    4

    Re: [RESOLVED]Excel -VB - Opening Microsoft Outlook attachments from saved .msg files

    Hi again, I've been using the code that DKenny wrote and it's been working perfectly However recently I discovered that it does not detect .zip files. I included oMessage.Attachments.Count to test an email with a zip attachment and it came up as 0. Any ideas as to why this is and how to fix it? Thanks once again.

  11. #11
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Excel -VB - Opening Microsoft Outlook attachments from saved .msg files (new problem)

    I assume you are using XP, correct? In XP BG & Co decided to make zip files act like folders. I imagine this is why you are seeing this behaviour. Not sure, yet, how to fix it...
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  12. #12
    Registered User
    Join Date
    Mar 2015
    Posts
    1

    Re: Excel -VB - Opening Microsoft Outlook attachments from saved .msg files

    Hi DKenny,

    I am referring to this after a long-long time.
    I tried this code of yours - it worked great for picking up one .msg file from a folder.
    But the user has to run the macro multiple times to extract attachments from all the .msg files in a given folder.
    Can you help pls with a modified version of this code, to open all the .msg files from a given folder and extract all the attachments (from each .msg) into another specified folder?
    This was perhaps the original requirement of Shootme too!
    May be Shootme could manage with the piece of code you shared.

    Would be greatfull if you can enhance this code of yours to accomplish the task of extracting the attachments from each and every .msg files stored in a folder in one go (macro to be executed only once).

    Regards

    Rajesh


    Quote Originally Posted by DKenny View Post
    Opening MailItems that have been saved as .msg files is really difficult. One easy work around it to create a copy of the file using the CreateItemFromTemplate method of the Outlook application. This makes a copy of the msg file that you can work with in subsequent code.
    Here's a procedure that should give you what you need. I've used the FileDialog object rather than the GetOpenFilename method, as it has move flexibility.
    Note: I wrote this in Excel, and you will need to add a reference to the Outlook library.

    VB Code:
    1. Option Explicit
    2.  
    3. Sub ShootMe()
    4. Dim sMessagePath As String
    5. Dim sSavePath As String
    6. Dim OLApp As Outlook.Application
    7. Dim oMessage As Outlook.MailItem
    8. Dim oMsgAttach As Outlook.Attachment
    9.  
    10.     '----------------------------------------------------------
    11.     'First get the path for the .msg file
    12.     '----------------------------------------------------------
    13.    
    14.     With Application.FileDialog(msoFileDialogFilePicker)
    15.        
    16.         'only allow a single file to be selected
    17.         .AllowMultiSelect = False
    18.        
    19.         .Title = "Select Message File"
    20.        
    21.         'Filter the file list to only
    22.         'include .msg files
    23.         With .Filters
    24.             .Clear
    25.             .Add "Message Files", "*.msg"
    26.         End With
    27.        
    28.         'If the user selects a file
    29.         'record the path,
    30.         'otherwise quit execution
    31.         If .Show = -1 Then
    32.             sMessagePath = .SelectedItems(1)
    33.         Else
    34.             Exit Sub
    35.         End If
    36.     End With
    37.    
    38.     '----------------------------------------------------------
    39.     'Next, get the save path
    40.     '----------------------------------------------------------
    41.    
    42.     With Application.FileDialog(msoFileDialogFolderPicker)
    43.         .AllowMultiSelect = False
    44.         .Title = "Select Save Folder"
    45.        
    46.         If .Show = -1 Then
    47.             sSavePath = .SelectedItems(1)
    48.         Else
    49.             Exit Sub
    50.         End If
    51.     End With
    52.    
    53.     '----------------------------------------------------------
    54.     'Now we need to open the message and export the attachments
    55.     '----------------------------------------------------------
    56.    
    57.     'Create Outlook objects
    58.     Set OLApp = New Outlook.Application
    59.     Set oMessage = OLApp.CreateItemFromTemplate(sMessagePath)
    60.    
    61.     'Loop through each attachment...
    62.     For Each oMsgAttach In oMessage.Attachments
    63.    
    64.         With oMsgAttach
    65.             '... saving it to the destination folder
    66.             .SaveAsFile Path:=sSavePath & "\" & .DisplayName
    67.         End With
    68.     Next oMsgAttach
    69.    
    70.     'Clear object variables
    71.     Set OLApp = Nothing
    72.     Set oMessage = Nothing
    73.     Set oMsgAttach = Nothing
    74. End Sub

  13. #13
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel -VB - Opening Microsoft Outlook attachments from saved .msg files (new prob

    try like
    Code:
    mypath = "c:\test\messages\"     ' change to suit
    ssavepath = "c:\test\attachments"
    msg = dir(mypath & "*.msg")    ' assumes messages have .msg extention
    
    do while len(msg) > 0   ' exit when no more messages
        Set OLApp = New Outlook.Application
    Set oMessage = OLApp.CreateItemFromTemplate(mypath & msg)
       
        'Loop through each attachment...
        For Each oMsgAttach In oMessage.Attachments
       
            With oMsgAttach
                '... saving it to the destination folder
                .SaveAsFile Path:=sSavePath & "\" & .DisplayName
            End With
        Next oMsgAttach
    msg = dir     ' get next message
    loop
    you can either hardcode the folder for messages and attachments or use the filedialog as in the original code, add or remove trailing \ as required
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Click Here to Expand Forum to Full Width