Extracting Attachments from *.msg files stored in many subfolders within main Shared Folder











up vote
0
down vote

favorite












I'm seeking your kind support to revise the below code to extract Attachments from *.msg files stored in many subfolders within main Shared Folder.



The below code is working fine for *.msg files if stored in one folder, but can't work for *.msg files stored in the subfolders.



The path for the main Folder is:



U:XXXXXXXXXXMain Folder



The paths for the subfolders are:



U:XXXXXXXXXXMain FolderFolder1



U:XXXXXXXXXXMain FolderFolder2



U:XXXXXXXXXXMain FolderFolder3
..etc.



Below is the code:



Sub SaveOlAttachments()

Dim msg As Outlook.MailItem
Dim att As Outlook.Attachment
Dim strFilePath As String
Dim strAttPath As String

'path for msgs
strFilePath = "U:XXXXXXXXXXMain Folder"
'path for saving attachments
strAttPath = "DAttachments"

strFile = Dir(strFilePath & "*.msg")
Do While Len(strFile) > 0
Set msg = Application.CreateItemFromTemplate(strFilePath & strFile)
If msg.Attachments.Count > 0 Then
For Each att In msg.Attachments
att.SaveAsFile strAttPath & att.FileName
Next
End If
strFile = Dir
Loop

End Sub









share|improve this question
























  • stackoverflow.com/questions/20687810/…
    – Tim Williams
    Jan 11 at 6:39










  • Thanks a lot Tim for your prompt response. Much appreciated. As my VBA knowledge is limited, can you do me a favor & combine the two code together. Thank you again Tim :)
    – Obada Talal
    Jan 11 at 6:54












  • Possible duplicate of VBA macro that search for file in multiple subfolders
    – F0XS
    Jan 11 at 6:55










  • Thank you FoxCy. I would be grateful if the codes can be combined together to extract all the attachments from the resulted *.msg files. Appreciated.
    – Obada Talal
    Jan 11 at 7:08















up vote
0
down vote

favorite












I'm seeking your kind support to revise the below code to extract Attachments from *.msg files stored in many subfolders within main Shared Folder.



The below code is working fine for *.msg files if stored in one folder, but can't work for *.msg files stored in the subfolders.



The path for the main Folder is:



U:XXXXXXXXXXMain Folder



The paths for the subfolders are:



U:XXXXXXXXXXMain FolderFolder1



U:XXXXXXXXXXMain FolderFolder2



U:XXXXXXXXXXMain FolderFolder3
..etc.



Below is the code:



Sub SaveOlAttachments()

Dim msg As Outlook.MailItem
Dim att As Outlook.Attachment
Dim strFilePath As String
Dim strAttPath As String

'path for msgs
strFilePath = "U:XXXXXXXXXXMain Folder"
'path for saving attachments
strAttPath = "DAttachments"

strFile = Dir(strFilePath & "*.msg")
Do While Len(strFile) > 0
Set msg = Application.CreateItemFromTemplate(strFilePath & strFile)
If msg.Attachments.Count > 0 Then
For Each att In msg.Attachments
att.SaveAsFile strAttPath & att.FileName
Next
End If
strFile = Dir
Loop

End Sub









share|improve this question
























  • stackoverflow.com/questions/20687810/…
    – Tim Williams
    Jan 11 at 6:39










  • Thanks a lot Tim for your prompt response. Much appreciated. As my VBA knowledge is limited, can you do me a favor & combine the two code together. Thank you again Tim :)
    – Obada Talal
    Jan 11 at 6:54












  • Possible duplicate of VBA macro that search for file in multiple subfolders
    – F0XS
    Jan 11 at 6:55










  • Thank you FoxCy. I would be grateful if the codes can be combined together to extract all the attachments from the resulted *.msg files. Appreciated.
    – Obada Talal
    Jan 11 at 7:08













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm seeking your kind support to revise the below code to extract Attachments from *.msg files stored in many subfolders within main Shared Folder.



The below code is working fine for *.msg files if stored in one folder, but can't work for *.msg files stored in the subfolders.



The path for the main Folder is:



U:XXXXXXXXXXMain Folder



The paths for the subfolders are:



U:XXXXXXXXXXMain FolderFolder1



U:XXXXXXXXXXMain FolderFolder2



U:XXXXXXXXXXMain FolderFolder3
..etc.



Below is the code:



Sub SaveOlAttachments()

Dim msg As Outlook.MailItem
Dim att As Outlook.Attachment
Dim strFilePath As String
Dim strAttPath As String

'path for msgs
strFilePath = "U:XXXXXXXXXXMain Folder"
'path for saving attachments
strAttPath = "DAttachments"

strFile = Dir(strFilePath & "*.msg")
Do While Len(strFile) > 0
Set msg = Application.CreateItemFromTemplate(strFilePath & strFile)
If msg.Attachments.Count > 0 Then
For Each att In msg.Attachments
att.SaveAsFile strAttPath & att.FileName
Next
End If
strFile = Dir
Loop

End Sub









share|improve this question















I'm seeking your kind support to revise the below code to extract Attachments from *.msg files stored in many subfolders within main Shared Folder.



The below code is working fine for *.msg files if stored in one folder, but can't work for *.msg files stored in the subfolders.



The path for the main Folder is:



U:XXXXXXXXXXMain Folder



The paths for the subfolders are:



U:XXXXXXXXXXMain FolderFolder1



U:XXXXXXXXXXMain FolderFolder2



U:XXXXXXXXXXMain FolderFolder3
..etc.



Below is the code:



Sub SaveOlAttachments()

Dim msg As Outlook.MailItem
Dim att As Outlook.Attachment
Dim strFilePath As String
Dim strAttPath As String

'path for msgs
strFilePath = "U:XXXXXXXXXXMain Folder"
'path for saving attachments
strAttPath = "DAttachments"

strFile = Dir(strFilePath & "*.msg")
Do While Len(strFile) > 0
Set msg = Application.CreateItemFromTemplate(strFilePath & strFile)
If msg.Attachments.Count > 0 Then
For Each att In msg.Attachments
att.SaveAsFile strAttPath & att.FileName
Next
End If
strFile = Dir
Loop

End Sub






vba outlook-vba email-attachments subfolder msg






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 11 at 9:07









0m3r

7,52392353




7,52392353










asked Jan 11 at 6:29









Obada Talal

32




32












  • stackoverflow.com/questions/20687810/…
    – Tim Williams
    Jan 11 at 6:39










  • Thanks a lot Tim for your prompt response. Much appreciated. As my VBA knowledge is limited, can you do me a favor & combine the two code together. Thank you again Tim :)
    – Obada Talal
    Jan 11 at 6:54












  • Possible duplicate of VBA macro that search for file in multiple subfolders
    – F0XS
    Jan 11 at 6:55










  • Thank you FoxCy. I would be grateful if the codes can be combined together to extract all the attachments from the resulted *.msg files. Appreciated.
    – Obada Talal
    Jan 11 at 7:08


















  • stackoverflow.com/questions/20687810/…
    – Tim Williams
    Jan 11 at 6:39










  • Thanks a lot Tim for your prompt response. Much appreciated. As my VBA knowledge is limited, can you do me a favor & combine the two code together. Thank you again Tim :)
    – Obada Talal
    Jan 11 at 6:54












  • Possible duplicate of VBA macro that search for file in multiple subfolders
    – F0XS
    Jan 11 at 6:55










  • Thank you FoxCy. I would be grateful if the codes can be combined together to extract all the attachments from the resulted *.msg files. Appreciated.
    – Obada Talal
    Jan 11 at 7:08
















stackoverflow.com/questions/20687810/…
– Tim Williams
Jan 11 at 6:39




stackoverflow.com/questions/20687810/…
– Tim Williams
Jan 11 at 6:39












Thanks a lot Tim for your prompt response. Much appreciated. As my VBA knowledge is limited, can you do me a favor & combine the two code together. Thank you again Tim :)
– Obada Talal
Jan 11 at 6:54






Thanks a lot Tim for your prompt response. Much appreciated. As my VBA knowledge is limited, can you do me a favor & combine the two code together. Thank you again Tim :)
– Obada Talal
Jan 11 at 6:54














Possible duplicate of VBA macro that search for file in multiple subfolders
– F0XS
Jan 11 at 6:55




Possible duplicate of VBA macro that search for file in multiple subfolders
– F0XS
Jan 11 at 6:55












Thank you FoxCy. I would be grateful if the codes can be combined together to extract all the attachments from the resulted *.msg files. Appreciated.
– Obada Talal
Jan 11 at 7:08




Thank you FoxCy. I would be grateful if the codes can be combined together to extract all the attachments from the resulted *.msg files. Appreciated.
– Obada Talal
Jan 11 at 7:08












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










Using my answer from VBA macro that search for file in multiple subfolders



Sub SaveOlAttachments()

Dim msg As Outlook.MailItem
Dim att As Outlook.Attachment
Dim strFilePath As String
Dim strAttPath As String
Dim colFiles As New Collection, f

'path for msgs
strFilePath = "U:XXXXXXXXXXMain Folder"

GetFiles strFilePath , "*.msg", True, colFiles

'path for saving attachments
strAttPath = "DAttachments"

For Each f in colFiles
Set msg = Application.CreateItemFromTemplate(f)
If msg.Attachments.Count > 0 Then
For Each att In msg.Attachments
att.SaveAsFile strAttPath & att.FileName
Next
End If
Next

End Sub


Sub to perform the search:



Sub GetFiles(StartFolder As String, Pattern As String, _
DoSubfolders As Boolean, ByRef colFiles As Collection)

Dim f As String, sf As String, subF As New Collection, s

If Right(StartFolder, 1) <> "" Then StartFolder = StartFolder & ""

f = Dir(StartFolder & Pattern)
Do While Len(f) > 0
colFiles.Add StartFolder & f
f = Dir()
Loop

sf = Dir(StartFolder, vbDirectory)
Do While Len(sf) > 0
If sf <> "." And sf <> ".." Then
If (GetAttr(StartFolder & sf) And vbDirectory) <> 0 Then
subF.Add StartFolder & sf
End If
End If
sf = Dir()
Loop

For Each s In subF
GetFiles CStr(s), Pattern, True, colFiles
Next s

End Sub





share|improve this answer























  • Thanks a lot Tim for your efforts. I tried the code but I'm getting Compile error, Sub or Function not defined. Any thoughts from your side?
    – Obada Talal
    Jan 12 at 5:59










  • Did you copy the GetFiles sub from the linked question? I've added it above.
    – Tim Williams
    Jan 12 at 6:23












  • It worked amazingly! Thank you so much Tim. Wish you all the best in your life & career. Have a great day ahead!
    – Obada Talal
    Jan 16 at 5:03











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48201007%2fextracting-attachments-from-msg-files-stored-in-many-subfolders-within-main-sh%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










Using my answer from VBA macro that search for file in multiple subfolders



Sub SaveOlAttachments()

Dim msg As Outlook.MailItem
Dim att As Outlook.Attachment
Dim strFilePath As String
Dim strAttPath As String
Dim colFiles As New Collection, f

'path for msgs
strFilePath = "U:XXXXXXXXXXMain Folder"

GetFiles strFilePath , "*.msg", True, colFiles

'path for saving attachments
strAttPath = "DAttachments"

For Each f in colFiles
Set msg = Application.CreateItemFromTemplate(f)
If msg.Attachments.Count > 0 Then
For Each att In msg.Attachments
att.SaveAsFile strAttPath & att.FileName
Next
End If
Next

End Sub


Sub to perform the search:



Sub GetFiles(StartFolder As String, Pattern As String, _
DoSubfolders As Boolean, ByRef colFiles As Collection)

Dim f As String, sf As String, subF As New Collection, s

If Right(StartFolder, 1) <> "" Then StartFolder = StartFolder & ""

f = Dir(StartFolder & Pattern)
Do While Len(f) > 0
colFiles.Add StartFolder & f
f = Dir()
Loop

sf = Dir(StartFolder, vbDirectory)
Do While Len(sf) > 0
If sf <> "." And sf <> ".." Then
If (GetAttr(StartFolder & sf) And vbDirectory) <> 0 Then
subF.Add StartFolder & sf
End If
End If
sf = Dir()
Loop

For Each s In subF
GetFiles CStr(s), Pattern, True, colFiles
Next s

End Sub





share|improve this answer























  • Thanks a lot Tim for your efforts. I tried the code but I'm getting Compile error, Sub or Function not defined. Any thoughts from your side?
    – Obada Talal
    Jan 12 at 5:59










  • Did you copy the GetFiles sub from the linked question? I've added it above.
    – Tim Williams
    Jan 12 at 6:23












  • It worked amazingly! Thank you so much Tim. Wish you all the best in your life & career. Have a great day ahead!
    – Obada Talal
    Jan 16 at 5:03















up vote
0
down vote



accepted










Using my answer from VBA macro that search for file in multiple subfolders



Sub SaveOlAttachments()

Dim msg As Outlook.MailItem
Dim att As Outlook.Attachment
Dim strFilePath As String
Dim strAttPath As String
Dim colFiles As New Collection, f

'path for msgs
strFilePath = "U:XXXXXXXXXXMain Folder"

GetFiles strFilePath , "*.msg", True, colFiles

'path for saving attachments
strAttPath = "DAttachments"

For Each f in colFiles
Set msg = Application.CreateItemFromTemplate(f)
If msg.Attachments.Count > 0 Then
For Each att In msg.Attachments
att.SaveAsFile strAttPath & att.FileName
Next
End If
Next

End Sub


Sub to perform the search:



Sub GetFiles(StartFolder As String, Pattern As String, _
DoSubfolders As Boolean, ByRef colFiles As Collection)

Dim f As String, sf As String, subF As New Collection, s

If Right(StartFolder, 1) <> "" Then StartFolder = StartFolder & ""

f = Dir(StartFolder & Pattern)
Do While Len(f) > 0
colFiles.Add StartFolder & f
f = Dir()
Loop

sf = Dir(StartFolder, vbDirectory)
Do While Len(sf) > 0
If sf <> "." And sf <> ".." Then
If (GetAttr(StartFolder & sf) And vbDirectory) <> 0 Then
subF.Add StartFolder & sf
End If
End If
sf = Dir()
Loop

For Each s In subF
GetFiles CStr(s), Pattern, True, colFiles
Next s

End Sub





share|improve this answer























  • Thanks a lot Tim for your efforts. I tried the code but I'm getting Compile error, Sub or Function not defined. Any thoughts from your side?
    – Obada Talal
    Jan 12 at 5:59










  • Did you copy the GetFiles sub from the linked question? I've added it above.
    – Tim Williams
    Jan 12 at 6:23












  • It worked amazingly! Thank you so much Tim. Wish you all the best in your life & career. Have a great day ahead!
    – Obada Talal
    Jan 16 at 5:03













up vote
0
down vote



accepted







up vote
0
down vote



accepted






Using my answer from VBA macro that search for file in multiple subfolders



Sub SaveOlAttachments()

Dim msg As Outlook.MailItem
Dim att As Outlook.Attachment
Dim strFilePath As String
Dim strAttPath As String
Dim colFiles As New Collection, f

'path for msgs
strFilePath = "U:XXXXXXXXXXMain Folder"

GetFiles strFilePath , "*.msg", True, colFiles

'path for saving attachments
strAttPath = "DAttachments"

For Each f in colFiles
Set msg = Application.CreateItemFromTemplate(f)
If msg.Attachments.Count > 0 Then
For Each att In msg.Attachments
att.SaveAsFile strAttPath & att.FileName
Next
End If
Next

End Sub


Sub to perform the search:



Sub GetFiles(StartFolder As String, Pattern As String, _
DoSubfolders As Boolean, ByRef colFiles As Collection)

Dim f As String, sf As String, subF As New Collection, s

If Right(StartFolder, 1) <> "" Then StartFolder = StartFolder & ""

f = Dir(StartFolder & Pattern)
Do While Len(f) > 0
colFiles.Add StartFolder & f
f = Dir()
Loop

sf = Dir(StartFolder, vbDirectory)
Do While Len(sf) > 0
If sf <> "." And sf <> ".." Then
If (GetAttr(StartFolder & sf) And vbDirectory) <> 0 Then
subF.Add StartFolder & sf
End If
End If
sf = Dir()
Loop

For Each s In subF
GetFiles CStr(s), Pattern, True, colFiles
Next s

End Sub





share|improve this answer














Using my answer from VBA macro that search for file in multiple subfolders



Sub SaveOlAttachments()

Dim msg As Outlook.MailItem
Dim att As Outlook.Attachment
Dim strFilePath As String
Dim strAttPath As String
Dim colFiles As New Collection, f

'path for msgs
strFilePath = "U:XXXXXXXXXXMain Folder"

GetFiles strFilePath , "*.msg", True, colFiles

'path for saving attachments
strAttPath = "DAttachments"

For Each f in colFiles
Set msg = Application.CreateItemFromTemplate(f)
If msg.Attachments.Count > 0 Then
For Each att In msg.Attachments
att.SaveAsFile strAttPath & att.FileName
Next
End If
Next

End Sub


Sub to perform the search:



Sub GetFiles(StartFolder As String, Pattern As String, _
DoSubfolders As Boolean, ByRef colFiles As Collection)

Dim f As String, sf As String, subF As New Collection, s

If Right(StartFolder, 1) <> "" Then StartFolder = StartFolder & ""

f = Dir(StartFolder & Pattern)
Do While Len(f) > 0
colFiles.Add StartFolder & f
f = Dir()
Loop

sf = Dir(StartFolder, vbDirectory)
Do While Len(sf) > 0
If sf <> "." And sf <> ".." Then
If (GetAttr(StartFolder & sf) And vbDirectory) <> 0 Then
subF.Add StartFolder & sf
End If
End If
sf = Dir()
Loop

For Each s In subF
GetFiles CStr(s), Pattern, True, colFiles
Next s

End Sub






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 12 at 6:24

























answered Jan 11 at 18:04









Tim Williams

83.7k96481




83.7k96481












  • Thanks a lot Tim for your efforts. I tried the code but I'm getting Compile error, Sub or Function not defined. Any thoughts from your side?
    – Obada Talal
    Jan 12 at 5:59










  • Did you copy the GetFiles sub from the linked question? I've added it above.
    – Tim Williams
    Jan 12 at 6:23












  • It worked amazingly! Thank you so much Tim. Wish you all the best in your life & career. Have a great day ahead!
    – Obada Talal
    Jan 16 at 5:03


















  • Thanks a lot Tim for your efforts. I tried the code but I'm getting Compile error, Sub or Function not defined. Any thoughts from your side?
    – Obada Talal
    Jan 12 at 5:59










  • Did you copy the GetFiles sub from the linked question? I've added it above.
    – Tim Williams
    Jan 12 at 6:23












  • It worked amazingly! Thank you so much Tim. Wish you all the best in your life & career. Have a great day ahead!
    – Obada Talal
    Jan 16 at 5:03
















Thanks a lot Tim for your efforts. I tried the code but I'm getting Compile error, Sub or Function not defined. Any thoughts from your side?
– Obada Talal
Jan 12 at 5:59




Thanks a lot Tim for your efforts. I tried the code but I'm getting Compile error, Sub or Function not defined. Any thoughts from your side?
– Obada Talal
Jan 12 at 5:59












Did you copy the GetFiles sub from the linked question? I've added it above.
– Tim Williams
Jan 12 at 6:23






Did you copy the GetFiles sub from the linked question? I've added it above.
– Tim Williams
Jan 12 at 6:23














It worked amazingly! Thank you so much Tim. Wish you all the best in your life & career. Have a great day ahead!
– Obada Talal
Jan 16 at 5:03




It worked amazingly! Thank you so much Tim. Wish you all the best in your life & career. Have a great day ahead!
– Obada Talal
Jan 16 at 5:03


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48201007%2fextracting-attachments-from-msg-files-stored-in-many-subfolders-within-main-sh%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Cypress Hill

what are some tips for doing well in the interview? [on hold]

How does a super-power salesman not get shut down for legal reasons?