The code below adds a folder called "Images" into my zip file. I don't want Images folder as a subfolder of the zip - how can I just add the contents of the Images folder to the root of the zip file? And FolderToAdd & "*.*"
doesn't work.
Sub testing()
Dim ZipFile As String
Dim FolderToAdd As String
Dim objShell As Object
Dim varZipFile As Variant
ZipFile = "C:\ZipFile_Images\images.zip"
FolderToAdd = "C:\Images"
Set objShell = CreateObject("Shell.Application")
varZipFile = ZipFile
If Right$(FolderToAdd, 1) <> "\" Then
FolderToAdd = FolderToAdd & "\"
End If
objShell.NameSpace(varZipFile).CopyHere (FolderToAdd)
End Sub
BACKGROUND: I pulled this code from a function that would add files one at a time to the given zip file, but when adding 100 small JPEG files, this would take a lot of time. Adding the whole folder at once is about 50x quicker.
Ultimately, I just want to be able to add multiple files at once natively so I'm open to other code snippets as well.
From Ron de Bruin's page here: http://www.rondebruin.nl/win/s7/win001.htm
You should be able to adapt this.
Key part is:
oApp.Namespace(FileNameZip).CopyHere oApp.Namespace(FolderName).items
Listing:
Sub Zip_All_Files_in_Folder_Browse()
Dim FileNameZip, FolderName, oFolder
Dim strDate As String, DefPath As String
Dim oApp As Object
DefPath = Application.DefaultFilePath
If Right(DefPath, 1) <> "\" Then
DefPath = DefPath & "\"
End If
strDate = Format(Now, " dd-mmm-yy h-mm-ss")
FileNameZip = DefPath & "MyFilesZip " & strDate & ".zip"
Set oApp = CreateObject("Shell.Application")
'Browse to the folder
Set oFolder = oApp.BrowseForFolder(0, "Select folder to Zip", 512)
If Not oFolder Is Nothing Then
'Create empty Zip File
NewZip (FileNameZip)
FolderName = oFolder.Self.Path
If Right(FolderName, 1) <> "\" Then
FolderName = FolderName & "\"
End If
'Copy the files to the compressed folder
oApp.Namespace(FileNameZip).CopyHere oApp.Namespace(FolderName).items
'Keep script waiting until Compressing is done
On Error Resume Next
Do Until oApp.Namespace(FileNameZip).items.Count = _
oApp.Namespace(FolderName).items.Count
Application.Wait (Now + TimeValue("0:00:01"))
Loop
On Error GoTo 0
MsgBox "You find the zipfile here: " & FileNameZip
End If
End Sub