VBA FileCopy "freezes" form

303 views Asked by At

Actually I have coded the following macro based on FileCopy method.

'MyObject copy function:
Public Function OutputFile(ByVal SrcPath As String, ByVal TrgPath As String) As Boolean

    If pDoesFileExist(SrcPath) Then   '<= Internal private function to check if file exists
        FileCopy SrcPath, TrgPath
        OutputFile = True
    Else
        OutputFile = False
    End If

End Function

Invoked from:

'Called subroutine from main program:
Sub OutputFiles(ByRef MyCollection As Collection, SourcePath As String, TargetPath As String)

    Dim Obj As MyObject

    With MyForm
        .Show "Modeless"
        For Each Obj In MyCollection
            If Obj.OutputFile(SourcePath, TargetPath) Then
                .TextBoxResult.Text = .TextBoxResult.Text & "File copied." & vbNewLine
            Else
                .TextBoxResult.Text = .TextBoxResult.Text & "File not copied!" & vbNewLine
            End if
        Next Obj
    End With

End Sub

The macro works perfectly when I make it run from/to my local computer folders, regardless of the file size (from a few KB to 20MB more or less).

But when I make it run using a work-domain source path (which is obviously slower than my computer), the instruction line FileCopy "freezes" on large files. The program is still running in background and the files are getting copied succesfully, however MyForm will go stucked [No Response] until the end of the execution.

Debugging step-by-step works "fine", I just have to wait that FileCopy instruction returns (10 seconds aprox), then keep moving forward.

I would like to know if there is a possible way to force this "wait until FileCopy returns", or to grant an immunity to the rest of my code against these mini freezes?

0

There are 0 answers