It would be appreciated if someone can assist me in witing code to attach all zip files in a folder C:downloads as well as the sub-folders in this folder
I will save the code in a template
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » MS Outlook and email programs » Macro to attach all zip files
Howard,
Here is sample code to create an email and attach all the zip files in a folder. Replace the blue lines with values that pertain to your situation.
HTH,
Maud
Sub CreateEmail() [COLOR=”#008000″]’————————————————— ‘DECLARE AND SET VARIABLES[/COLOR] Dim outApp As Object Dim OutMail As Object Dim strbody As String Dim Filename As String, strbody As String Set outApp = CreateObject(“Outlook.Application”) Set OutMail = outApp.CreateItem(0) [COLOR=”#008000″]’————————————————— ‘CREATE EMAIL BODY[/COLOR] strbody = “[COLOR=”#0000FF”]EMAIL BODY GOES HERE[/COLOR]” [COLOR=”#008000″]’————————————————— ‘BUILD EMAIL[/COLOR] On Error Resume Next With OutMail .To = “[COLOR=”#0000FF”]JohnDoe@gmail.com[/COLOR]” .CC = “” .BCC = “” .Subject = “[COLOR=”#0000FF”]SUBJECT GOES HERE[/COLOR]” .Body = strbody Path = “[COLOR=”#0000FF”]C:UsersMaudibeDesktop[/COLOR]” [COLOR=”#008000″]’——————————————– ‘GET FILENAMES[/COLOR] Filename = Dir(Path & “*.zip”) Do While Len(Filename) > 0 .Attachments.Add Filename Filename = Dir Loop .Display End With [COLOR=”#008000″]’————————————————— ‘CLEANUP[/COLOR] On Error GoTo 0 Set OutMail = Nothing Set outApp = Nothing End Sub
Hi Maud
I certainly forgot about this post. Thanks very much for providing me with the code. Must this be set up in an Outlook Template or can I set this up rather in Excel ?
Where there is a PM, I am receiving these via email, but not replies to other posts. How do I set this up so I receive emails where someone has replied to one of my posts?
Howard,
I think you zipped and uploaded to wrong file.
I have tested the code and it attaches the zipped files flawlessly. Make sure you have the correct path and it ends with a “”. Also make sure you have zipped files in the directory with the extension .zip
Maud
Hi Maud
I think that although my file is zipped and has a ,zip extension , it looks like it was created in notepad
This may be the reason, it may not be attaching
See screen print attached of what the zip file looks like
I only have an evaluation copy of winzip
Do you have any idea how I can resolve this?
It would be appreciated if you can please shed some light on this
Howard,
It should not matter on the format. As long as the extension is .zip (all small), it should attach it. I am suspecting that the file is actually named Book6.zip.txt and not a zipped file at all.
HTH,
Maud
Exactly what is the file Book6.xlsx in the uploaded zipped file of post #6 supposed to be?
Hi Maud
I have amended the code from
.Attachments.Add Filename
to
.Attachments.Add Path & Filename
The .zip files in folder “C:pull now attach in Outlook.
However the .zip files in the sub-directory of C:pull for eg C:pullBR1TB C:pullCPETB etc are not attaching
It would be appreciated if you would kindly amend the code to attach the .zip files in the sub-folders as well
Hi Maud
You recently helped me to email zip files. It would be appreciated if you would kindly amend the code to exclude zip files containing “backup” in the name of the zip file
Sub CreateEmail() '--------------------------------------------------- 'DECLARE AND SET VARIABLES Dim outApp As Object Dim OutMail As Object Dim strbody As String Dim Filename As String Set outApp = CreateObject("Outlook.Application") Set OutMail = outApp.CreateItem(0) '--------------------------------------------------- 'CREATE EMAIL BODY strbody = "Hi " & Join(Application.Transpose(Range("D1:D5").Value)) & vbNewLine & vbNewLine strbody = strbody & "Attached Please find latest Management Account" & vbNewLine & vbNewLine strbody = strbody & "Regards" & vbNewLine & vbNewLine '--------------------------------------------------- 'BUILD EMAIL On Error Resume Next With OutMail .to = Join(Application.Transpose(Range("E1:E5").Value), ";") .CC = "" .BCC = "" .Subject = "Accounts" .Body = strbody Path = "C:test1l" '-------------------------------------------- 'GET FILENAMES ' Filename = Dir(Path & "*.zip") ' Do While Len(Filename) > 0 ' .Attachments.Add Filename ' Filename = Dir ' Loop Dim fso, oFolder, oSubfolder, oFile, col As Collection Set fso = CreateObject("Scripting.FileSystemObject") Set col = New Collection col.Add fso.GetFolder(Path) Do While col.Count > 0 Set oFolder = col(1) col.Remove 1 For Each oSubfolder In oFolder.SubFolders col.Add oSubfolder Next oSubfolder For Each oFile In oFolder.Files If CStr(oFile) Like "*.zip" Then .Attachments.Add CStr(oFile) End If Next oFile Loop .Display End With '--------------------------------------------------- 'CLEANUP On Error GoTo 0 Set OutMail = Nothing Set outApp = Nothing Set fso = Nothing End Sub
Howard,
It should not matter on the format. As long as the extension is .zip (all small), it should attach it. I am suspecting that the file is actually named Book6.zip.txt and not a zipped file at all.
I think Maudibe is correct as the icon pictured with the file book6.zip shows it as a text file.
Hi Maud
You provided me with code to attach zip files in a folder. Is it possible to attach 10 files per email as the file size is too large as some companies limit the total file size to 10gb. If so kindly amend the code so that the first 10 zip files are attach to email 1 , the second 10 to email 2 etc until all the zip files have been attached
It would be appreciated if you would kindly assist
Sub CreateEmail() '--------------------------------------------------- 'DECLARE AND SET VARIABLES Dim outApp As Object Dim OutMail As Object Dim strbody As String Dim Filename As String Set outApp = CreateObject("Outlook.Application") Set OutMail = outApp.CreateItem(0) '--------------------------------------------------- 'CREATE EMAIL BODY strbody = "Hi " & Join(Application.Transpose(Range("D1:D5").Value)) & vbNewLine & vbNewLine strbody = strbody & "Attached Please find latest Management Account" & vbNewLine & vbNewLine strbody = strbody & "Regards" & vbNewLine & vbNewLine '--------------------------------------------------- 'BUILD EMAIL On Error Resume Next With OutMail .to = Join(Application.Transpose(Range("E1:E5").Value), ";") .CC = "" .BCC = "" .Subject = "Management Accounts" .Body = strbody Path = "C:pull" '-------------------------------------------- 'GET FILENAMES ' Filename = Dir(Path & "*.zip") ' Do While Len(Filename) > 0 ' .Attachments.Add Filename ' Filename = Dir ' Loop Dim fso, oFolder, oSubfolder, oFile, col As Collection Set fso = CreateObject("Scripting.FileSystemObject") Set col = New Collection col.Add fso.GetFolder(Path) Do While col.Count > 0 Set oFolder = col(1) col.Remove 1 For Each oSubfolder In oFolder.SubFolders col.Add oSubfolder Next oSubfolder For Each oFile In oFolder.Files If InStr(1, oFile, "Backup", 1) > 0 Then Else If CStr(oFile) Like "*.zip" Then .Attachments.Add CStr(oFile) End If End If Next oFile Loop .Display End With '--------------------------------------------------- 'CLEANUP On Error GoTo 0 Set OutMail = Nothing Set outApp = Nothing Set fso = Nothing End Sub
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.