+ Reply to Thread
Results 1 to 6 of 6

VBA code to zip file not working

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-14-2014
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    186

    VBA code to zip file not working

    Hi,

    So I found a piece of VBA code online which should zip a file.
    This is the code:

     Sub ZipFile(strZipFilePath As String, strZipFileName As String, ParamArray arrFiles() As Variant)
     
        Dim intLoop         As Long
     
        Dim i               As Integer
     
        Dim objApp          As Object
     
        Dim vFileNameZip
     
     
     
        If Right(strZipFilePath, 1) <> Application.PathSeparator Then
     
            strZipFilePath = strZipFilePath & Application.PathSeparator
     
        End If
     
        vFileNameZip = strZipFilePath & strZipFileName & ".zip"
     
     
     
        If IsArray(arrFiles) = False Then GoTo ExitH
     
     
     
    '-------------------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

    The parameters of the function are set to

    Capture.PNG


    But when I run the code, it is keep looping over this part

    Do Until objApp.Namespace(vFileNameZip).Items.Count = i
     
         Application.Wait (Now + TimeValue("0:00:01"))
     
    Loop

    Anyone know why?

  2. #2
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: VBA code to zip file not working

    It could be related to how you're passing the array of files to zip. ParamArray expects multiple values separated with a comma so you need to call it like this:

    ZipFile "C:\Test", "Test", "[path to file 1]", "[path to file 2]", "[path to file 3]"
    Do not try and pass it as an array. If you want to pass an array, change the final parameter in the function:

    Sub ZipFile(strZipFilePath As String, strZipFileName As String, arrFiles As Variant)
    Then you can call it like this:

    ZipFile "C:\Test", "Test", Array("[path to file 1]", "[path to file 2]", "[path to file 3]")
    WBD
    Office 365 on Windows 11, looking for rep!

  3. #3
    Forum Contributor
    Join Date
    06-14-2014
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    186

    Re: VBA code to zip file not working

    Hi WBD,

    I only have one file to zip, so I am essentially just passing one file to the function.

    I tried what you said in your reply but still no luck

  4. #4
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: VBA code to zip file not working

    Ah. OK. I tried a little test locally on my PC and it worked. I also tried passing a UNC path (as in your example) and that also worked. I'm not sure why it wouldn't work for you.

    WBD

  5. #5
    Forum Contributor
    Join Date
    06-14-2014
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    186

    Re: VBA code to zip file not working

    could it be because the file that I want to zip is saved in a shared network drive that I only have read access to

  6. #6
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: VBA code to zip file not working

    Shouldn't be but you could try zipping up other files or changing the permissions on the file to see if that makes a difference.

    WBD

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. VBA Code with Listbox quit working after file was saved and reopened.
    By nate.oyen in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-07-2020, 08:23 AM
  2. [SOLVED] How to use edit (code) if the file is from the list box? i used tag but is not working..
    By Chandria in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-27-2019, 09:38 AM
  3. [SOLVED] Convert some working VBA code to work in VBScript [Test File State]
    By dlow in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 11-28-2015, 07:52 PM
  4. [SOLVED] Working code to import txt file to sheet.
    By abjac in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-24-2014, 04:35 PM
  5. [SOLVED] VBA Find Code not working with XML file like it did in XLS file - Please Help
    By SEOT in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-22-2014, 12:25 PM
  6. Problems working with a shared file contaning VBA Code.-question for the advanced
    By yossiperets in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-06-2013, 04:49 AM
  7. File Size Code not working
    By briank in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 03-02-2005, 01:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1