The script has become a bit unwieldy--especially since it's VBScript. There's nothing really wrong with it but it would be much better to break things down much more. There aren't all that many options for doing that with VBScript--it is not really your friend when you have such a long procedure--but there are some. One example: there are 20 instances of .Copy and 20 instances of .Paste. These sections of code could all be moved to one or two standard functions. Getting one instance failsafe would get them all failsafe. A simple thing that's (sort of) wrong throughout is that although you are waiting for a modal dialog, you aren't explicitly waiting for the right modal dialog, i.e. a dialog with the expected caption. In practice it's often essential to do both, i.e. wait twice, to prevent random timing errors. I've written a fairly bulletproof function to do that, which you can use each time instead of a simple in-line loop. It has a timeout. Sub WaitForUIDialog(caption, timeout) ' Wait for a modal dialog with the specified caption to show ' subject to an overall timeout in seconds ' (-> log and throw error) Dim ui Dim start Set ui = Application.UserInterface start = Now ' Wait for some modal dialog to show Do If DateDiff("s", start, Now) >= timeout Then Application.History.Log "Timeout waiting for modal dialog (" _ & CStr(DateDiff("s", start, Now)) & " sec)" & vbCrLf, _ True Err.Raise 17 ' Can't perform requested operation 'Exit Do ' not required End If Loop Until ui.DisplaysModalDialog ' Wait for the current modal dialog to show the expected caption Set dlg = ui.ModalDialog Do If DateDiff("s", start, Now) >= timeout Then Application.History.Log "Timeout waiting for " & Chr(34) & caption & Chr(34) & " dialog (" _ & CStr(DateDiff("s", start, Now)) & " sec)" & vbCrLf, _ True Err.Raise 17 ' Can't perform requested operation 'Exit Do ' not required End If Loop Until ui.ModalDialog.Caption = caption End Sub But obviosuly there is a lot more to look at here.
|