• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2871
  • Last Modified:

Access VBA - programmatically add files to an existing zip

hello,

Does anyone know how to programmatically add files to an exisiting zip file? I'm using code to create the zip file with a spreadsheet in it. However, I need to add PDFs to that zip file. The reason it needs to be done with VBA code and not dragging/dropping into the zip is because the code will run overnight when no one is around.

btw, i'm trying to use 'FileCopy strDirFrom, strDirTo', to copy the PDF to the zip file but I keep getting Run-time error '76': Path not found. This is confusing me because I know the paths are right. Are you not able to programmatically copy to a compressed zip file?

any ideas will be greatly appreciated.

thanks!
aebea
0
Anthony Berenguel
Asked:
Anthony Berenguel
  • 2
  • 2
1 Solution
 
omgangIT ManagerCommented:
I posted this in your previous Q....must have been while you were requesting deletion.  Anyway, this code creates the zip file first and then adds files to it.  Simply comment out the part where the zip file is created.
OM Gang


Public Function AddFilesToZip()
On Error GoTo Err_AddFilesToZip

        'Declarations
    Dim objFSO As Object, objZip As Object, objShell As Object
    Dim objFolder As Object, objFile As Object
    Dim sngStart As Single
    Dim strPath As String, strZip As String
   
    strPath = "C:\test\"   'Path to read files from
    strZip = "C:\test2\omgang.zip"  'Output zip file
   
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objZip = objFSO.CreateTextFile(strZip)
    objZip.WriteLine Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, 0)
    objZip.Close
 
    Set objShell = CreateObject("Shell.Application")
    Set objFolder = objFSO.GetFolder(strPath)
 
        'loop through files - adding them to the zip
    For Each objFile In objFolder.Files
       
        objShell.NameSpace("" & strZip).CopyHere objFile.PATH
       
        sngStart = Timer
        Do While Timer < sngStart + 2
            DoEvents
        Loop

    Next

Exit_AddFilesToZip:
        'destroy object variables
    Set objFile = Nothing
    Set objFolder = Nothing
    Set objShell = Nothing
    Set objZip = Nothing
    Set objFSO = Nothing
    Exit Function

Err_AddFilesToZip:
    MsgBox Err.Number & ", " & Err.Description, , "Error in Function AddFilesToZip of Module Module7"
    Resume Exit_AddFilesToZip
   
End Function
0
 
Anthony BerenguelAuthor Commented:
thanks! This bit of code really helped me out!!
0
 
Anthony BerenguelAuthor Commented:
omgang, thanks again for your help. Btw, i'm not using this bit of code because I'm not sure what it does. Can you explain it? Thanks!

 
       sngStart = Timer
        Do While Timer < sngStart + 2
            DoEvents
        Loop

Open in new window

aebea
0
 
omgangIT ManagerCommented:
It's to provide a bit of extra time to allow the file to be added to the zip file.  The code I provided adds every file in the specified directory to the zip archive.  When dealing with relatively large files we need to allow for a bit of time for the file to be processed.  The loop pauses processing of the current procedure for the specified interval (2 seconds) before continuing.  The other processes continue to execute during this pause, e.g. the Shell CopyHere command that's adding the file to the zip.

If you were dealing with very large files you may need to increase the interval.  If you are only dealing with very small files, or only one file at a time instead of mulitiple files, you may not require the pause at all.
OM Gang
0
 
Justin BarryCommented:
OM Gang,

I am zipping some very large .txt files (1.4gb).  Is there a way to modify this code that uses the Sleep method, instead of adding to the interval?  IE: sngstart + 500?

If there isn't a way to use the Sleep method, do you see any issues with the process stalling when adding such a large interval?

Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now