View Single Post
  #3   Report Post  
Posted to microsoft.public.vb.general.discussion,microsoft.public.excel.programming
Dave Peterson Dave Peterson is offline
external usenet poster
 
Posts: 35,218
Default Excel automation: Why my code is hijacked?

First, I don't see the purpose of checking in the first routine.

Public Sub XLDisconnectFromExcel()
Set moExcelWS = Nothing
Set moExcelApp = Nothing
End Sub

If either of those objects are already nothing, then it won't hurt.

But I don't have a guess why your program hangs up. Maybe you didn't release
all the objects??? Maybe you didn't close excel??? Maybe excel is waiting for
input from the user???

And I would expect an error if the getobject failed in the second routine.

I use this:

dim moExcelApp as Object
On Error Resume Next
Set moExcelApp = GetObject(, "Excel.Application")
If Err.Number = 429 Then
'Excel is not running
Set moExcelApp = CreateObject("Excel.Application")
err.clear
end if
On Error GoTo 0



Jack wrote:

Hello,
I have this routine to disconnect from Excel:

Public Sub XLDisconnectFromExcel()
If Not moExcelWS Is Nothing Then Set moExcelWS = Nothing
If Not moExcelApp Is Nothing Then Set moExcelApp = Nothing
End Sub

When the second line of that code is executed and moExcelApp is "Microsoft
Excel" then that line of code is executed properly (Excel.exe is process
terminated in Task Manager) but the code execution never returns after that
line. It just dissapears and subsequently not any other code is executed.
What can be wrong?

To connect to Excel I use this code:
Set moExcelApp = GetObject(, "Excel.Application")
If moExcelApp Is Nothing Then
Set moExcelApp = CreateObject("Excel.Application")
End If

Jack


--

Dave Peterson