Sub Zip_ThisWorkbook()
Dim strDate As String, StrDateBUpFolder As String, DefPath As String, DefPathBackup As String
Dim FileNameOriginZip, FileNameXls, FileNameDestinationZip, FileNameBUp
Dim oApp As Object
Application.DisplayStatusBar = True 'turns on the status bar
Application.StatusBar = "Please wait while File is Saved and Zipped" ' displays a message in Status Bar
Application.ScreenUpdating = False ' Hides changes to screen until Macro finished
'Define This workbook Path and Backup Folder date to use
DefPath = ThisWorkbook.Path & "\"
StrDateBUpFolder = Format(Now, "mmmyyyy")
'Checks to See If A Directory Exists, If Not, Creates It
DefPathBackup = ThisWorkbook.Path & "\" & "Enrol Backup" & StrDateBUpFolder & "\"
DirTest = Dir$(DefPathBackup, vbDirectory)
If DirTest = "" Then
MkDir DefPathBackup
DoEvents 'just to make sure it is there
End If
'Create date/time string and the temporary xls and zip file name
strDate = Format(Now, " (ddd dmmmyy hmmam/pmss") & "sec)"
FileNameBUp = Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 4) & strDate & ".zip"
FileNameDestinationZip = DefPathBackup & FileNameBUp 'Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 4) & strDate & ".zip"
FileNameOriginZip = DefPath & FileNameBUp 'Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 4) & strDate & ".zip"
FileNameXls = DefPath & ThisWorkbook.Name
If Dir(FileNameOriginZip) = "" Then ' And Dir(FileNameXls) = "" Then
'Make copy of the Thisworkbook
ThisWorkbook.Save
'Create empty Zip File
Application.ScreenUpdating = False ' Hides changes to screen until Macro finished
NewZip (FileNameOriginZip)
'Copy the file in the compressed folder
Set oApp = CreateObject("Shell.Application")
oApp.Namespace(FileNameOriginZip).CopyHere FileNameXls
'Keep script waiting until Compressing is done
On Error Resume Next
Do Until oApp.Namespace(FileNameOriginZip).items.Count = 1
Application.Wait (Now + TimeValue("0:00:01"))
Loop
On Error GoTo 0
' Move Zipped file to Backup Directory
Name FileNameOriginZip As FileNameDestinationZip
Else
End If
MsgBox "This file has been Saved and Zipped - Om Tat Sat"
Application.StatusBar = False 'turns off Message
Application.ScreenUpdating = True ' Turns back on screen updating
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
|