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.
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.
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: