Writing to Zip file from VBA

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
20,330
Office Version
  1. 365
Platform
  1. Windows
I have seen many pages with content like the following: Zip file(s) with the default Windows zip program (VBA)

But no matter what I do it always gets stuck in the Do Until loop and doesn't write any files into the zip file.

My current version of this is as follows:
Code:
Sub ZipFile(strZipFilePath, file1 As String, file2 As String)
 
    Dim intloop         As Long
 
    Dim I               As Integer
 
    Dim objApp          As Object
 
    Dim vFileNameZip
    
    Dim arrFiles(0 To 1) As String
 
    

    vFileNameZip = strZipFilePath
    

    arrFiles(0) = file1
    arrFiles(1) = file2
 
 
 
 
'-------------------Create new empty Zip File-----------------

    If Len(Dir(vFileNameZip)) > 0 Then Kill vFileNameZip
 
    Open vFileNameZip For Output As #1
 
    Print #1, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0)
 
    Close #1
 
'=============================================================

    Set objApp = CreateObject("Shell.Application")

    I = 0

    For intloop = LBound(arrFiles) To UBound(arrFiles)

        'Copy file to Zip folder/file created above

        I = I + 1

        objApp.Namespace(vFileNameZip).CopyHere arrFiles(intloop)



        'Wait until Compressing is complete

        On Error Resume Next

        Do Until objApp.Namespace(vFileNameZip).items.Count = I

            Application.Wait (Now + TimeValue("0:00:01"))

        Loop

        On Error GoTo 0

    Next intloop

ExitH:

    Set objApp = Nothing
 
End Sub

I have verified that there are valid file paths/filenames for both elements of the array.
 
Last edited:

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
I had the same problem. I found if you modify the statement:

objApp.Namespace(vFileNameZip).CopyHere arrFiles(intLoop)

to

objApp.Namespace(vFileNameZip).CopyHere CStr(arrFiles(intLoop))

The code works.
 
Upvote 0

Forum statistics

Threads
1,214,632
Messages
6,120,649
Members
448,975
Latest member
sweeberry

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