Quick Question: Why should this loop have special error conditions?

Old Man Devin

Consul Of Code
Local time
Today, 00:12
Joined
Jan 10, 2014
Messages
183
Some of you may have encountered this piece of code before from Ron De Bruin's website that lets you zip up some files.

Code:
Sub Zip_All_Files_in_Folder()
    Dim FileNameZip, FolderName
    Dim strDate As String, DefPath As String
    Dim oApp As Object

    DefPath = Application.DefaultFilePath
    If Right(DefPath, 1) <> "\" Then
        DefPath = DefPath & "\"
    End If

    FolderName = "C:\Users\Ron\test\"    '<< Change

    strDate = Format(Now, " dd-mmm-yy h-mm-ss")
    FileNameZip = DefPath & "MyFilesZip " & strDate & ".zip"

    'Create empty Zip File
    NewZip (FileNameZip)

    Set oApp = CreateObject("Shell.Application")
    'Copy the files to the compressed folder
    oApp.Namespace(FileNameZip).CopyHere oApp.Namespace(FolderName).items

    'Keep script waiting until Compressing is done
    On Error Resume Next
    Do Until oApp.Namespace(FileNameZip).items.Count = _
       oApp.Namespace(FolderName).items.Count
        Application.Wait (Now + TimeValue("0:00:01"))
    Loop
    On Error GoTo 0

    MsgBox "You'll find the zipfile here: " & FileNameZip
End Sub

My question regarding this is: when it begins the script waiting loop, why does it set On Error Resume Next? Can counting the items give an error? My concern is that if something went wrong with the counting of files and the file count in the zip never reaches the number in the original folder, this is going to loop forever and not tell you that the count is failing.

Hopefully that makes sense. I just wonder if anyone has used similar waiting loops and counts in a namespace and might shed a little light on those error handler choices before I dive in and start changing things.

Many thanks.
 
If the target is locked or does not exist the ocunt returns an error I believe.
 
If the target is locked or does not exist the ocunt returns an error I believe.

Cool, thanks. So in that case ignoring all errors in the process may indeed cause an infinite loop.
 
Should be easy to test if the count does or does not return an error if the file does not exist.
 

Users who are viewing this thread

Back
Top Bottom