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.
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 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 Excel news, tips & tricks straight to you inbox. Helping you to save time and achieve more with Excel.
You will also receive the Tab Hopper Add-in for FREE.