borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (run code when closing document?)Next Topic (HELP!!!!) New Topic New Poll Post Reply
AndreaVB Forum : VBA (Access, Excel, Word, ...) : destroying instances of Excel
Poster Message
TheDog
Level: Protégé

Registered: 27-09-2005
Posts: 4

icon destroying instances of Excel

I currently use:

myHwnd = FindWIndow("XLMAIN", 0)

To find the handle of the window containing an instance of excel for my use after opening an instance of Excel.

Then I use:

xlapp.Quit
tmpBoolean = xlapp.DestroyWindow(myHwnd)

To close the instance.
It works great the task bar shows the instance does in fact disappear.

My problem is that when I use:

myHwnd = FindWIndow("XLMAIN", 0)
agein to dectect if any further nstances are open, I get the handle of the supposedly destroyed instance again and not "0" which I would expect if the instance were truely destroyed.

Is there some "refresh" type function I need to run after closing an unused instance and destroying its window, so the FindwIndow function will stop returning the destroyed windows handle, making me think it still is in existance when it is not.

05-10-2005 at 02:18 PM
View Profile Send Email to User Show All Posts | Quote Reply
GeoffS
Level: VB Lord


Registered: 29-09-2004
Posts: 536
icon Re: destroying instances of Excel

Hi,
You need to send a message to the Window to physically close it.

Add the following declaration :

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


Then add the following to your procedure :


Const NILL = 0&
Const WM_SYSCOMMAND = &H112
Const SC_CLOSE = &HF060&

'get the window handle for the app
myHwnd = FindWIndow("XLMAIN", 0)

'now send message to close it
myHwnd = SendMessage(myHwnd, WM_SYSCOMMAND, SC_CLOSE, NILL)



____________________________
multi-tasking - the ability to hang more than one app. at the same time.

06-10-2005 at 08:58 AM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : VBA (Access, Excel, Word, ...) : destroying instances of Excel
Previous Topic (run code when closing document?)Next Topic (HELP!!!!) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder