View Single Post
  #15   Report Post  
Posted to microsoft.public.excel.programming
MikeZz MikeZz is offline
external usenet poster
 
Posts: 152
Default xlApp.Quit - Doesn't Close Instance.... Any ideas?

Hi Jim,
I tried your adding this to the .close statement but it still didn't work.
SaveChanges:=False
I think it has something to do with our network.

However, I did resolve the issue by using this trick I found:

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam
As Long) As Long

'Get Handle I created.
HwndImport = xlApp2.hwnd

'Close Handle after Closing file, Quitting xlApp2 and setting = nothing.
SendMessage HwndImport, WM_CLOSE, 0, 0

Thanks for your help,

"Jim Cone" wrote:

When I tried your code, It hung when trying to close the file unless I followed
the instructions I posted earlier:
"Close the Workbook and specify whether to save it or not."

The close line should read...
"xlApp2.Workbooks(i).Close SaveChanges:=False 'or True"
When I did the above, the new Excel instance closed without a problem.

Also, complications could arise, if your "S" drive is "elsewhere" or
the file is shared and in use.
--
Jim Cone
Portland, Oregon USA
(use object references when automating excel)




"MikeZz"
wrote in message
Hi Jim,
I did an even more basic test, just opening and closing an instance using
the following code.
It closed the instance as long as I didn't open and close a workbook - see
the 2 lines for opening and closing.

Sub CloseInstanceTest()

Dim xlApp2 As Excel.Application 'ADDED FOR MEMORY
Set xlApp2 = New Excel.Application 'ADDED FOR MEMORY
xlApp2.Visible = True
Dim wbCount, i

'If I don't do these lines of code, the Excel Instance closes.
'So as long as I don't open a file (which defeats the point), it works.
xlApp2.Workbooks.Open ("S:\C-0MNL0-0CB-001.xls")
wbCount = xlApp2.Workbooks.Count

For i = 1 To wbCount
xlApp2.Workbooks(i).Close
Next i
xlApp2.Quit
Set xlApp2 = Nothing
End Sub