Ident Open Workbooks in Multiple Excel Instances
I'm working through 'Access 2003 VBA', which is proving very useful.
The section on Working with Excel is where I am at present.
A test piece I've built uses:
- comdlg32.dll to pick a file name,
- If a file of that name already exists, and is open, it must be closed before it is deleted (so that it can be overwritten); the following is used to refer to any open instance of Excel:
xlApp = GetObject(, "Excel.Application")
- Originally, code then checked each open file in xlApp, and closed it if it had the same name as that returned by the file picker.
However, if there was more than one instance of Excel open, that was inadequate. Alternative code to quit each instance of Excel was written:
Set xlApp = Nothing
On Error GoTo en429
Set xlApp = GetObject(, "Excel.Application")
Do
xlApp.Quit
Set xlApp = Nothing
Set xlApp = GetObject(, "Excel.Application")
Loop
en429:
Select Case Err.Number
Case 429 'xlAPP cannot be created, because there are no open instances of Excel.
Case Is <> 0
Debug.Print Err.Number
Debug.Print Err.Description
Stop 'Unexpected error that should be recorded and trapped.
End Select
Err.Clear
On Error GoTo 0
This worked when it was stepped through, but when run at normal speed, if crashed, typically with the following:
Error 462: The remote server machine does not exist or is unavailable.
Please help - Ideally, I want all instances of Excel checked and only the file with the same name as that picked with comdlg32.dll being closed.
Any thoughts / experience you have with identifying open files in multiple instances of Excel would gratefully be received.
|