Hello,
I have a folder that at the root level contains both files as well as an images folder containing .jpg files. I was wondering if it were possible to use VBA to create a .zip file from that root folder. So far, I have only been able to create a new .zip file, and then add files to that .zip. I've had no luck be able to create the images subfolder.
Is this possible with VBA? I have searched many forums and have found the following code to be one of few that actually runs without throwing an error:
I have a folder that at the root level contains both files as well as an images folder containing .jpg files. I was wondering if it were possible to use VBA to create a .zip file from that root folder. So far, I have only been able to create a new .zip file, and then add files to that .zip. I've had no luck be able to create the images subfolder.
Is this possible with VBA? I have searched many forums and have found the following code to be one of few that actually runs without throwing an error:
Code:
Sub AddFolderToZip(ZipFile As String, FolderToAdd As String)
Dim objShell As Object
Dim lngFilesAdded As Long
Dim strFile As String
Dim varZipFile As Variant
Call InitializeZipFile(ZipFile) 'This Works
Set objShell = _
CreateObject("Shell.Application")
varZipFile = ZipFile
If Right$(FolderToAdd, 1) <> "\" Then
FolderToAdd = FolderToAdd & "\"
End If
strFile = Dir(FolderToAdd & "*.*")
Do While strFile <> ""
'This doesnt capture the subfolder inside the folder to add
objShell.Namespace(varZipFile).CopyHere (FolderToAdd & strFile)
lngFilesAdded = lngFilesAdded + 1
Do Until _
objShell.Namespace(varZipFile).Items.Count >= lngFilesAdded
Call Pause(1)
Loop
strFile = Dir()
Loop
End Sub
Last edited: