J
Jack
My code checks whether Excel app is open by using this line of code:
Set moExcel = GetObject(, "Excel.Application")
If there is an error then my code opens Excel app, waits until Excel window
appears and executes the same line of code
and there is again the same error despite the fact that Excel was already
opened.
That problem is NOT a timing problem (I inserted 30 secs delay to the same
effect).
Interestingly, setting a breakpoint at this line and stepping through it
does not create any error.
On the other hand, when Excel is running prior to running this code there is
NOT any error.
Any help appreciated,
Jack
Public Sub CheckExcelApp()
Set moExcel = Nothing
Set moExcelWS = Nothing
On Error GoTo Open_Excel_App
Set moExcel = GetObject(, "Excel.Application")
Debug.Print "Excel app found!"
Exit Sub
Open_Excel_App:
Debug.Print "Excel app NOT found!"
On Error GoTo 0
rtn = ShellExecute(Me.hwnd, "Open", "excel.exe", vbNullString, App.Path,
vbNormalFocus)
If rtn > 32 Then 'no error from ShellExecute
Start = Timer
Do Until FindWindow(vbNullString, "Microsoft Excel") > 0
If Timer - Start > 10 Then ExcelApp = 1: Exit Sub
Loop
Debug.Print "Excel app opened"
Else
ExcelApp = 1: Exit Sub
End If
'''' DelayTimer (30)
Resume Try_Open_Again
Try_Open_Again:
On Error GoTo Error_again
Set moExcel = GetObject(, "Excel.Application")
Debug.Print "Excel app found!"
Exit Sub
Error_again:
Debug.Print "Excel app again NOT found!"
End Sub
..
Set moExcel = GetObject(, "Excel.Application")
If there is an error then my code opens Excel app, waits until Excel window
appears and executes the same line of code
and there is again the same error despite the fact that Excel was already
opened.
That problem is NOT a timing problem (I inserted 30 secs delay to the same
effect).
Interestingly, setting a breakpoint at this line and stepping through it
does not create any error.
On the other hand, when Excel is running prior to running this code there is
NOT any error.
Any help appreciated,
Jack
Public Sub CheckExcelApp()
Set moExcel = Nothing
Set moExcelWS = Nothing
On Error GoTo Open_Excel_App
Set moExcel = GetObject(, "Excel.Application")
Debug.Print "Excel app found!"
Exit Sub
Open_Excel_App:
Debug.Print "Excel app NOT found!"
On Error GoTo 0
rtn = ShellExecute(Me.hwnd, "Open", "excel.exe", vbNullString, App.Path,
vbNormalFocus)
If rtn > 32 Then 'no error from ShellExecute
Start = Timer
Do Until FindWindow(vbNullString, "Microsoft Excel") > 0
If Timer - Start > 10 Then ExcelApp = 1: Exit Sub
Loop
Debug.Print "Excel app opened"
Else
ExcelApp = 1: Exit Sub
End If
'''' DelayTimer (30)
Resume Try_Open_Again
Try_Open_Again:
On Error GoTo Error_again
Set moExcel = GetObject(, "Excel.Application")
Debug.Print "Excel app found!"
Exit Sub
Error_again:
Debug.Print "Excel app again NOT found!"
End Sub
..