• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Macro to attach Zip Files within sub-folders |VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / Macro to attach Zip Files within sub-folders |VBA & Macros|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosMacro to attach Zip Files within su…
sp_PrintTopic sp_TopicIcon
Macro to attach Zip Files within sub-folders
Avatar
Howard Cohen
Member
Members
Level 0
Forum Posts: 48
Member Since:
December 8, 2016
sp_UserOfflineSmall Offline
1
January 27, 2021 - 11:54 am
sp_Permalink sp_Print sp_EditHistory
  1. I have code to attach zip files within sub-folders with C:\Sales Reports in Microsoft outlook

    The code works well and attaches some of the zip files in Outlook as the maximum limit is 20MB

    I need my code amended to attach up to a max limit of 20MB and when this is reached to create a second to attach the balance of the zip files (the total off al the Zip files is less than 40MB so I only need two emails)

    there are 18 zip files in the sub-folders. an alternative would be to attach the first 10 to the first email and the balance to be attached on the second email

    Your assistance in this regard is most appreciated

    1. 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 Sales Reports" & 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 = "Sales Reports" .Body = strbody Path = "C:\Sales Reports\" '-------------------------------------------- '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
      I have also posted on https://www.excelforum.com/excel-programming-vba-macros/1339082-macro-to-attach-zip-files-within-sub-folders.html
Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1807
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
2
January 29, 2021 - 5:54 pm
sp_Permalink sp_Print sp_EditHistory

Hi Howard,

you should loop through that folder outside your CreateEmail procedure, collect files, when they reach your limit, create an email.

Should be like this:

Sub GetAllFiles()
Dim FSO As Object, FSize As Long, FilesCollection As Collection
Set FilesCollection = New Collection
Set FSO = CreateObject("Scripting.FileSystemObject")
ProcessFolderFiles FSO.GetFolder("C:\users\catalin\desktop"), FSize, FilesCollection
If FilesCollection.Count > 0 Then CreateEmail FilesCollection 'there may be remaining files, totalizing less than 18 mb
End Sub

Sub ProcessFolderFiles(objFolder As Object, ByRef FSize As Long, ByRef FilesCollection As Collection)
Dim SubFld As Object, aFile As Object
For Each SubFld In objFolder.SubFolders
ProcessFolderFiles SubFld, FSize, FilesCollection
Next

For Each aFile In objFolder.Files
FSize = FSize + aFile.Size
Debug.Print FSize
FilesCollection.Add aFile.Path
If FSize > 18000000 Then 'set to 18 mb, you can set to FilesCollection.count>9 for example
CreateEmail FilesCollection
Set FilesCollection = New Collection
FSize = 0 'reset file size
End If
Next
End Sub

 

Then, in your CreateEmail procedure, attach to email the files from FilesCollection collection received from outside procedure.

Of course, CreateEmail() should become:
Sub CreateEmail(byref FilesCollection as collection)
You should update the other forum, if the solution works, to inform them you have a solution, you don't want to waste their time.

Avatar
Howard Cohen
Member
Members
Level 0
Forum Posts: 48
Member Since:
December 8, 2016
sp_UserOfflineSmall Offline
3
January 29, 2021 - 11:25 pm
sp_Permalink sp_Print

Many Thanks for the help

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Ruth Savage, Andy Kirby, Roy Lutke, Jeff Krueger
Guest(s) 7
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 870
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 219
A.Maurizio: 202
Jessica Stewart: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
John Chisholm
vexokeb sdfg
John Jack
Malcolm Toy
Ray-Yu Yang
George Shihadeh
Naomi Rumble
Uwe von Gostomski
Jonathan Jones
drsven
Forum Stats:
Groups: 3
Forums: 24
Topics: 6212
Posts: 27236

 

Member Stats:
Guest Posters: 49
Members: 31889
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.