I have a macro to zip xlsm workbooks in C:\Sales directory as well as sub-folders
The macro zips the files but A get a zip error message-see link below
www.dropbox.com
It would be appreciated if someone could amend my code to prevent this error
The macro zips the files but A get a zip error message-see link below
Dropbox
It would be appreciated if someone could amend my code to prevent this error
Code:
Dim x As Integer
Dim fso As Object
Dim result As Boolean
Sub SubFolderInfo()
Application.ScreenUpdating = False
'------------------------------------
'DECLARE AND SET VARIABLES
Dim strPath As String
strPath = "C:\sales\"
x = 0
Set fso = CreateObject("Scripting.FileSystemObject")
'------------------------------------
'CHECK FOLDERS AND SUBFOLDERS
result = ExtractFileInfo(strPath)
'------------------------------------
'CLEANUP
Set fso = Nothing
MsgBox x & " files have been zipped."
Application.ScreenUpdating = True
End Sub
Private Function ExtractFileInfo(fspec)
On Error GoTo ErrHandler
'------------------------------------
'DECLARE AND SET VARIABLES
Dim fldr As Object, fi As Object, sfldr As Object, oApp As Object
Dim Filename, fname As String
Set fldr = fso.GetFolder(fspec)
'------------------------------------
'CHECK FILES IN TOP FOLDER
If fldr.Files.Count <> 0 Then
For Each fi In fldr.Files
s = Split(fi, ".")
If InStr(1, fi, "(P).xls", 1) > 0 Then
'And UCase(Left(s(1), 2)) = "XL" Then
s = Split(fi, ".")
Filename = s(0) & ".zip"
NewZip (Filename)
fname = fi
Set oApp = CreateObject("Shell.Application")
oApp.Namespace(Filename).CopyHere s(0) & "." & s(1) 'FName(iCtr)
x = x + 1
End If
accessnotallowed:
Next
End If
'------------------------------------
'CHECK SUBFOLDERS
If fldr.SubFolders.Count > 0 Then
For Each sfldr In fldr.SubFolders
ExtractFileInfo (sfldr) 'RECURSIVE CHECK
Next
End If
'------------------------------------
'CLEANUP
permissiondenied:
ExtractFileInfo = True
Set fldr = Nothing
ExitHandler:
Application.ScreenUpdating = True
Exit Function
'------------------------------------
'HANDLE RETURNED ERROR
ErrHandler:
If Err.Number = 70 Then 'permission denied
Err.Clear
MsgBox fspec & Chr(13) & "Permission Denied"
Resume permissiondenied
Else
MsgBox Err.Number & ": " & Err.Description
Resume ExitHandler
End If
End Function
Sub NewZip(sPath)
'Create empty Zip File
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