Zipping a group of files using generic zipping code

KenCriss

Active Member
Joined
Jun 6, 2005
Messages
326
The following code works great for zipping all files in a folder. But I have a need to feed a list of files into the macro instead of zipping the entire folder. Is there a way to revise the code to do this? Not sure it matters, but all the files will have an extension of .DAT. Thanks for any advice.

VBA Code:
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"))
    Exit Do
Loop
On Error GoTo 0

End Sub

Sub MakeZip()
Call CreateZipFile("U:\WS_Users\KWC\Temp\TEST", "U:\WS_Users\KWC\Temp\MyCompanyZip.zip")
MsgBox "Done!"
End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
See if you can adapt this code for a similar request:


Your main proc should probably be something like (copy the variable declarations) :

VBA Code:
Sub MakeZip()
    sourceFolder = "U:\WS_Users\KWC\Temp\TEST\"
    file = Dir(sourceFolder & "*.dat")
    While file <> vbNullString
        Zip_File sourceFolder & file, "U:\WS_Users\KWC\Temp\MyCompanyZip.zip"
        file = Dir
    Wend
    MsgBox "Done!"
End Sub
 
Upvote 0
I saw that post and thought about that....the problem is I only need to zip some of the .DAT files in a folder, not all of them. I'll play around with it. Thanks for the reply.
 
Upvote 0

Forum statistics

Threads
1,214,980
Messages
6,122,563
Members
449,088
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top