VBA_Save Compressed "windows" zip

cgmojoco

Well-known Member
Joined
Jan 15, 2005
Messages
699
Does windows have a method of zipping files? (I think windows has Zip or Compressed folders method?)

I cannot use winzip (not allowed to install any programs like that on this PC).

If windows has a built in method, can that be accessed via VBA for a compressed .xls save?
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
I can't figure out how to:

-Check if a windows compressed file/folder named _Backup exists in the current workbook's directory.

-If windows compressed file/folder exists open it and put a copy of the active workbook in it.
-If compressed file/folder does not exist (I think I can figure out how to create a new one and add a copy of the file to it)...

I'm trying to create a macro that will upon execution save a copy of the workbook into a zipped folder named _Backup with a date and version # (I have the date and version # part worked out).

Opening an existing windows zipped folder/file isn't an option listed on the site the last poster listed (at least not as far as I was able to discern).

Thanks in advance to anyone that may provide a solution. When working with files 100megs+ compression becomes a serious issue...
 
Upvote 0
Thanks Dave!

I'm having trouble understanding how to merge your code with Ron's.
Here is Ron's "vanilla" code with psuedo code comments in orange where added functionality is meant to be inserted:

Rich (BB code):
'From Ron de Bruin: http://www.rondebruin.nl/windowsxpzip.htm
'This sub will make a copy of the Activeworkbook and zip it
'in "C:\Backup" with a date-time stamp.
'Change this folder or use your default path Application.DefaultFilePath
 
Sub Zip_ActiveWorkbook()
    Dim strDate As String, DefPath As String
    Dim FileNameZip, FileNameXls
    Dim oApp As Object
'##########CHANGE THIS TO ACTIVEWORKBOOK DIRECTORY
    DefPath = "C:\Backup" '<< Change
    If Right(DefPath, 1) <> "\" Then
        DefPath = DefPath & "\"
    End If
'##########REMOVE THIS, WANT THE FILE TO HAVE A DATESTAMP,
'##########NOT THE ZIP FOLDER
    'Create date/time string and the temporary xls and zip file name
    strDate = Format(Now, " dd-mmm-yy h-mm-ss")
    FileNameZip = DefPath & Left(ActiveWorkbook.Name, _
 Len(ActiveWorkbook.Name) - 4) & strDate & ".zip"
    FileNameXls = DefPath & Left(ActiveWorkbook.Name, _
 Len(ActiveWorkbook.Name) - 4) & strDate & ".xls"
 
    If Dir(FileNameZip) = "" And Dir(FileNameXls) = "" Then
'##########ADD A DATESTAMP HERE
        'Make copy of the activeworkbook
        ActiveWorkbook.SaveCopyAs FileNameXls
'##########ONLY CREATE A ZIP FILE IF ONE NAMED _BACKUP.zip
'##########DOESN'T ALREADY EXIST
        'Create empty Zip File
        NewZip (FileNameZip)
'##########ADD FILE TO EXISTING _BACKUP.zip folder
        'Copy the file in the compressed folder
        Set oApp = CreateObject("Shell.Application")
        oApp.NameSpace(FileNameZip).CopyHere FileNameXls
 
        'Keep script waiting until Compressing is done
        On Error Resume Next
        Do Until oApp.NameSpace(FileNameZip).items.Count = 1
            Application.Wait (Now + TimeValue("0:00:01"))
        Loop
        On Error GoTo 0
 
        'Delete the temporary xls file
        Kill FileNameXls
 
        MsgBox "Your Backup is saved here: " & FileNameZip
 
    Else
        MsgBox "FileNameZip or/and FileNameXls exist"
    End If
End Sub
Sub NewZip(sPath)
'Create empty Zip File
'Changed by keepITcool Dec-12-2005
    If Len(Dir(sPath)) > 0 Then Kill sPath
    Open sPath For Output As #1
    Print #1, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0)
    Close #1
End Sub
 
 
Function bIsBookOpen(ByRef szBookName As String) As Boolean
' Rob Bovey
    On Error Resume Next
    bIsBookOpen = Not (Application.Workbooks(szBookName) Is Nothing)
End Function
 
Function Split97(sStr As Variant, sdelim As String) As Variant
'Tom Ogilvy
    Split97 = Evaluate("{""" & _
                       Application.Substitute(sStr, sdelim, """,""") & """}")
End Function
 
Upvote 0
If you're still interested, Here's 2 functions I have for zipping and unzipping.

Used like this in your macro

Call Zipper(FullPathFilename of zip file,FullPathFilename of file to zip)
Call Unzip(FullPathFilename to Unzip,Fullpath to Unzip To)

In the Zipper, if the zip file does not already exist, it is created. If it DOES already exist, the file is added to it.

The only problem I've been unable to figure out is...
If the Zip File already Exists, AND the file you're adding to the zip file already exists in the zip file, you are prompted with "It already exists, do you want to replace"
IT still works, you just have to click yes or no. I've been unable to figure out how to automatically choose an answer to the question...

It can even zip a file that is currently open. Make sure to put in a Save or SaveAs command before the zip....

But anyway, here's the code.

Code:
Public Function Unzip(DefPath, Fname)
'Unzips A File
'Fname must be FULL Path\Filename.zip
'DefPath must be valid Path\ you want to Unzip file TO
Dim fso As Object
Dim oApp As Object
Set oApp = CreateObject("Shell.Application")
oApp.Namespace(DefPath).CopyHere oApp.Namespace(Fname).items
On Error Resume Next
Set fso = CreateObject("scripting.filesystemobject")
fso.deletefolder Environ("Temp") & "\Temporary Directory*", True
Set oApp = Nothing
Set fso = Nothing
End Function

Code:
Public Function Zipper(ZipName, FileToZip)
'Zips A File
'ZipName must be FULL Path\Filename.zip - name Zip File to Create OR ADD To
'FileToZip must be Full Path\Filename.xls  -  Name of file you want to zip
Dim fso As Object
Dim oApp As Object
If Dir(ZipName) = "" Then
    Open ZipName For Output As #1
    Print #1, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0)
    Close #1
End If
Set oApp = CreateObject("Shell.Application")
oApp.Namespace(ZipName).CopyHere (FileToZip)
On Error Resume Next
Set fso = CreateObject("scripting.filesystemobject")
fso.deletefolder Environ("Temp") & "\Temporary Directory*", True
Set oApp = Nothing
Set fso = Nothing
End Function

Hope this helps...
 
Upvote 0
At last, with your explanation there I think it is beginning to sink in
Appreciate that will give it a go thanks!
 
Upvote 0
Got it working...sort of...
Creates a .zip folder naming it the filename of the workbook you have open ( _bkpyourfilename.zip)

If the .zip already exists adds a date/time stamped xls copy of your activeworkbook to that .zip

"Sometimes"
The code get's stuck in the waiting loop (seems to happen when copying to an existing .zip). If I remove the waiting loop I get a windows message saying FileNameXls file is empty.

Any advise to terminate the waiting loop after a given amount of time or something else that will get this working correctly?

Rich (BB code):
'Help from:
'Ron de Bruin: http://www.rondebruin.nl/windowsxpzip.htm
'NdNoviceHlp @ mr.excel.com
'jonmo1 @ mr.excel.com
'This sub will make a copy of the Activeworkbook and zip it
'in "C:\Backup" with a date-time stamp.
'Change this folder or use your default path Application.DefaultFilePath
'Option Explicit
    Dim strDate As String, DefPath As String
    Dim FileNameZip, FileNameXls
    Dim FullPathFilename As String
    Dim oApp As Object
    Dim fso As Object
Sub Zip_ActiveWorkbook()
    DefPath = ActiveWorkbook.Path '<< Change
    If Right(DefPath, 1) <> "\" Then
        DefPath = DefPath & "\"
    End If
 
'Create date/time string and the temporary xls and zip file name
    strDate = Format(Now, " yy-mmm-dd h-mm-ss")
 
    FileNameZip = DefPath & Left(ActiveWorkbook.Name, _
    Len(ActiveWorkbook.Name) - 4) & "_bkp" & ".zip"
 
    FileNameXls = DefPath & Left(ActiveWorkbook.Name, _
    Len(ActiveWorkbook.Name) - 4) & "_" & strDate & ".xls"
 
'Make copy of the activeworkbook
    ActiveWorkbook.SaveCopyAs FileNameXls
    If Dir(FileNameZip) = "" Then
        Open FileNameZip For Output As #1
        Print #1, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0)
        Close #1
    End If
 
    Set oApp = CreateObject("Shell.Application")
    oApp.Namespace(FileNameZip).CopyHere (FileNameXls)
 
'Keep script waiting until Compressing is done!!!!!!!!!Errors out here!!!!!11
    On Error Resume Next
        Do Until oApp.Namespace(FileNameZip).items.Count = 1
            Application.Wait (Now + TimeValue("0:00:01"))
        Loop
    err.Clear
 
'Delete any temp files from compressing
On Error Resume Next
    Set fso = CreateObject("scripting.filesystemobject")
    fso.deletefolder Environ("Temp") & "\Temporary Directory*", True
    Set oApp = Nothing
    Set fso = Nothing
err.Clear
'Delete the temporary xls file
    Kill FileNameXls
 
    MsgBox "Your Backup is saved here: " & FileNameZip
 
FileNameZip = ""
FileNameXls = ""
 
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,126
Messages
6,129,004
Members
449,480
Latest member
yesitisasport

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