|
-
Apr 20th, 2006, 04:11 AM
#1
Thread Starter
New Member
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.
-
Apr 20th, 2006, 09:19 AM
#2
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 
-
Apr 20th, 2006, 10:53 AM
#3
Thread Starter
New Member
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:
fileToOpen = Application.GetOpenFilename("Outlook item (*.msg), *.msg")
If fileToOpen <> False Then
Workbooks.Open Filename:=fileToOpen
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
-
Apr 20th, 2006, 11:26 AM
#4
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Apr 20th, 2006, 12:37 PM
#5
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:
Option Explicit
Sub ShootMe()
Dim sMessagePath As String
Dim sSavePath As String
Dim OLApp As Outlook.Application
Dim oMessage As Outlook.MailItem
Dim oMsgAttach As Outlook.Attachment
'----------------------------------------------------------
'First get the path for the .msg file
'----------------------------------------------------------
With Application.FileDialog(msoFileDialogFilePicker)
'only allow a single file to be selected
.AllowMultiSelect = False
.Title = "Select Message File"
'Filter the file list to only
'include .msg files
With .Filters
.Clear
.Add "Message Files", "*.msg"
End With
'If the user selects a file
'record the path,
'otherwise quit execution
If .Show = -1 Then
sMessagePath = .SelectedItems(1)
Else
Exit Sub
End If
End With
'----------------------------------------------------------
'Next, get the save path
'----------------------------------------------------------
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
.Title = "Select Save Folder"
If .Show = -1 Then
sSavePath = .SelectedItems(1)
Else
Exit Sub
End If
End With
'----------------------------------------------------------
'Now we need to open the message and export the attachments
'----------------------------------------------------------
'Create Outlook objects
Set OLApp = New Outlook.Application
Set oMessage = OLApp.CreateItemFromTemplate(sMessagePath)
'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
'Clear object variables
Set OLApp = Nothing
Set oMessage = Nothing
Set oMsgAttach = Nothing
End Sub
Declan
Don't forget to mark your Thread as resolved.
Take a moment to rate posts that you think are helpful 
-
Apr 20th, 2006, 12:47 PM
#6
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Apr 20th, 2006, 12:51 PM
#7
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 
-
Apr 20th, 2006, 12:55 PM
#8
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Apr 21st, 2006, 03:39 AM
#9
Thread Starter
New Member
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
-
May 3rd, 2006, 05:56 AM
#10
Thread Starter
New Member
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.
-
May 3rd, 2006, 07:16 AM
#11
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 
-
Mar 28th, 2015, 09:21 AM
#12
Registered User
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
 Originally Posted by DKenny
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:
Option Explicit
Sub ShootMe()
Dim sMessagePath As String
Dim sSavePath As String
Dim OLApp As Outlook.Application
Dim oMessage As Outlook.MailItem
Dim oMsgAttach As Outlook.Attachment
'----------------------------------------------------------
'First get the path for the .msg file
'----------------------------------------------------------
With Application.FileDialog(msoFileDialogFilePicker)
'only allow a single file to be selected
.AllowMultiSelect = False
.Title = "Select Message File"
'Filter the file list to only
'include .msg files
With .Filters
.Clear
.Add "Message Files", "*.msg"
End With
'If the user selects a file
'record the path,
'otherwise quit execution
If .Show = -1 Then
sMessagePath = .SelectedItems(1)
Else
Exit Sub
End If
End With
'----------------------------------------------------------
'Next, get the save path
'----------------------------------------------------------
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
.Title = "Select Save Folder"
If .Show = -1 Then
sSavePath = .SelectedItems(1)
Else
Exit Sub
End If
End With
'----------------------------------------------------------
'Now we need to open the message and export the attachments
'----------------------------------------------------------
'Create Outlook objects
Set OLApp = New Outlook.Application
Set oMessage = OLApp.CreateItemFromTemplate(sMessagePath)
'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
'Clear object variables
Set OLApp = Nothing
Set oMessage = Nothing
Set oMsgAttach = Nothing
End Sub
-
Mar 28th, 2015, 05:06 PM
#13
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|