Recently, I have been working on a project which requires the zipping and unzipping files and folders. Zip files are now a common method of compressing files and folders for sharing. As software becomes more complex, file sizes increase, however there is often a limit to the file size an e-mail provider will allow. For example, Google currently allows a maximum file size of 25MB to be sent. Putting all the attachments into a single zip file can help get around this issue, as the files are compressed to be smaller.
The code snippets below are based on a section from Excel 2016 Power Programming with VBA by Michael Alexander/Dick Kusleika and from Ron de Bruin’s site.
Whilst working with Zip files, I wanted to make a reusable procedure which I could call when ever required. The code below was created for that purpose. These code snippets do not create, delete or check for the existence of the files or folders which it uses. Check out the following code snippets to cover these areas:
UPDATE: As discussed in the comments section below. Do not declare a String variable to hold the file paths, this will not work with the Shell.Application. Declare a Variant variable to hold the file paths, this will ensure the code runs smoothly.
Create a zip file from a folder
This procedure has only a few steps:
- Create an empty zip file
- Copy the files from the folder into the zip file
- Wait for all the zip files to stop compressing
Sub CreateZipFile(folderToZipPath As Variant, zippedFileFullName As Variant) Dim ShellApp As Object 'Create an empty zip file Open zippedFileFullName For Output As #1 Print #1, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0) Close #1 'Copy the files & folders into the zip file Set ShellApp = CreateObject("Shell.Application") ShellApp.Namespace(zippedFileFullName).CopyHere ShellApp.Namespace(folderToZipPath).items 'Zipping the files may take a while, create loop to pause the macro until zipping has finished. On Error Resume Next Do Until ShellApp.Namespace(zippedFileFullName).items.Count = ShellApp.Namespace(folderToZipPath).items.Count Application.Wait (Now + TimeValue("0:00:01")) Loop On Error GoTo 0 End Sub
To call the procedure above the following code can be used within another procedure. Change the paths to be the folder you wish to zip and the name you want the zip folder to be called.
Call CreateZipFile("C:\Users\marks\Documents\ZipThisFolder\", "C:\Users\marks\Documents\NameOFZip.zip")
This procedure will overwrite any zip folder with the same name.
Do you know the fastest way to learn foreign languages? It is to read, write, speak, and think in that language as often as possible. Apart from speaking, programming languages are no different. The more you immerse yourself in that language, the faster you will pick it up.
Therefore, what most people like you need is lots of examples that you can practice. That is why the 100 Excel VBA Macros eBook exists. It’s the book for all Excel users who want to learn how to read and write Excel macros, save time, and stand out from their peers. The book contains:
- 100 example codes to practice reading and writing macros that will embed the language into your thinking.
- An introduction to macros in Excel to ensure you can implement the VBA code in the book even if you have no prior knowledge.
- Consistent code layout between examples to enable you to understand the structure and easily customize the code to meet your needs.
- Downloadable workbook containing all the source code, so the examples can be added to your project to give you the benefit of VBA straight away.
Unzip a zip file to a folder
Unzipping is a much easier process and only requires the files to be copied from the zip file into the folder.
Sub UnzipAFile(zippedFileFullName As Variant, unzipToPath As Variant) Dim ShellApp As Object 'Copy the files & folders from the zip into a folder Set ShellApp = CreateObject("Shell.Application") ShellApp.Namespace(unzipToPath).CopyHere ShellApp.Namespace(zippedFileFullName).items End Sub
To call the procedure above the following code can be used within another procedure. Change the paths to be the name of the zip file you wish to unzip and the folder you wish to put the unzipped files into.
Call UnzipAFile("C:\Users\marks\Documents\ZipHere.zip", "C:\Users\marks\Documents\UnzipHereFolder\")
How does this code actually work?
It is rarely explained how this code creates a zip file. Let me show you.
Create an empty zip file just using windows. Right-click in a folder and select New-> Compressed (zipped) folder.
Now open that file in Notepad. The section of code (highlighted in blue) informs windows this file is a zip file. The file is empty, so there is no code from other files in there.
The code below is the line which inserts that same character string at the start of the file. As a result, Windows believes this is a zip folder.
Print #1, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0)
Even if we’ve created a zip file, we still need to get the files into it. The native VBA code is not able to copy to/from a zip file, so the code uses the Shell.Application to copy the files. Using the Shell.Application is similar to using the Windows environment, which is able to copy and paste files into a zip folder.
It is these two things together which really drive the functionality of this code.
Get our FREE VBA eBook of the 30 most useful Excel VBA macros.
Automate Excel so that you can save time and stop doing the jobs a trained monkey could do.
By entering your email address you agree to receive emails from Excel Off The Grid. We’ll respect your privacy and you can unsubscribe at any time.
If you’ve found this post useful, or if you have a better approach, then please leave a comment below.
Do you need help adapting this to your needs?
I’m guessing the examples in this post didn’t exactly meet your situation. We all use Excel differently, so it’s impossible to write a post that will meet everybody’s needs. By taking the time to understand the techniques and principles in this post (and elsewhere on this site) you should be able to adapt it to your needs.
But, if you’re still struggling you should:
- Read other blogs, or watch YouTube videos on the same topic. You will benefit much more by discovering your own solutions.
- Ask the ‘Excel Ninja’ in your office. It’s amazing what things other people know.
- Ask a question in a forum like Mr Excel, or the Microsoft Answers Community. Remember, the people on these forums are generally giving their time for free. So take care to craft your question, make sure it’s clear and concise. List all the things you’ve tried, and provide screenshots, code segments and example workbooks.
- Use Excel Rescue, who are my consultancy partner. They help by providing solutions to smaller Excel problems.
Don’t go yet, there is plenty more to learn on Excel Off The Grid. Check out the latest posts: