Zip Files Using VBA

How to zip files using VBA in Microsoft Excel. Often we need to email files for sharing with others. If the files are large in size then it is better to zip or compress them for saving upload and download times. As you can see in one example a macro enabled file has been compressed from 2.6 MB to about 262 Kb in size.

Watch the video to learn how to compress files using VBA and the inbuilt zipping function in Windows:

Watch this video on Youtube.

Here’s the complete VBA code to perform a compression of files using the Windows inbuilt compression feature:

Sub makeZipFile(pathToZipFolder As Variant, zippedFileName As Variant)

Dim ShellApp As Object

‘First we create an empty zip file
Open zippedFileName For Output As #1
Print #1, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0)
Close #1

‘Next we copy the files & folders into the zip file
Set ShellApp = CreateObject(“Shell.Application”)
ShellApp.Namespace(zippedFileName).CopyHere ShellApp.Namespace(pathToZipFolder).items

‘We use loopng mechanism to process the zipping with a pause value since zipping the files will take some time
On Error Resume Next
Do Until ShellApp.Namespace(zippedFileName).items.Count = ShellApp.Namespace(pathToZipFolder).items.Count
Application.Wait (Now + TimeValue(“0:00:01”))
Loop
On Error GoTo 0

End Sub

Sub zipMyFiles()
Call makeZipFile(“C:\Files-To-Be-Zipped\”, “C:\My-Zipped-Files\my-zipped-files.zip”)
End Sub

Further reading:

Zip file(s) with the default Windows zip program (VBA)

 

Published by

Dinesh Kumar Takyar

Welcome to exceltrainingvideos.com! My aim is to help you learn MS Excel including VBA. I always felt that a good way to learn something was to find solutions to problems in that domain. That is why I share these Excel videos with you. Mostly these are questions asked during my corporate training sessions. I've been training individuals and companies in computers since 1991. I did my B. Sc. (Vordiplom), M. Sc. (Diplom) and Ph.D. (Dr. rer. nat.) from Hamburg, Germany. The best thing about solving some of my visitor's questions is that I also gain new insights. For more Excel VBA solutions you may like to visit my YouTube channel: http://youtube.com/familycomputerclub For a structured Excel VBA training course online you can visit: https://www.youtube.com/excelvbaonline

Leave a Reply

Your email address will not be published. Required fields are marked *