Forum Discussion

Sameer_Kuppanath_Sultan's avatar
Sameer_Kuppanath_Sultan
Brass Contributor
May 04, 2022
Solved

Error show in VBA to create Zip files of a selected folder

Hi I have a code below for zipping a folder to a specified location to save. 

 

Sub Zipping_the_File()
Dim source, zipfile As String
source = "C:\Users\Documents\Client\"
zipfile = "C:\Work Area\Common Files\NameOFZip.zip"
CreateZipFile source, zipfile
End Sub

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

Do Until ShellApp.Namespace(zippedFileFullName).Items.Count = ShellApp.Namespace(folderToZipPath).Items.Count
Application.Wait (Now + TimeValue("0:00:01"))
Loop
End Sub

 

When I run the code, I am receiving below Pic (Error Pic 1), and when do debug it shows the below pic (Error Pic 2).

 

 

 

 

Resources