VB app needs to wait for Word document to close

Posted on 1999-07-13
Medium Priority
Last Modified: 2008-03-17
I am using VB6 and Word 97 and am launching Word from my VB app using CreateObject.  I programmatically open a document for the user to view.  I then want to wait for the user to close the doc before I let him continue to use my VB app, i.e. I want to suspend VB while the doc is open.

I have tried a "ping" of sorts by running a loop that gets the FullName property of the document and as long as something other than null is returned, I wait.  But when the user opens a dialog in Word (e.g. Tools/Options) the OLE conversation taking place in my loop gets locked out and VB freezes so bad I have to kill it with a Ctrl-Alt-Del.

DDE is not an option from what I can tell because from past experience we know that a similar event occurs when a dialog is open, and you must communicate over channel 0 with Word temporarily until the dialog is dismissed.  The LinkMode property (VB's DDE method) doesn't let you specify the DDE channel and so it will hang as well.  

Is there a robust, foolproof way to wait for a Word document to close from VB?
Question by:fenske

Expert Comment

ID: 1524352
Have you tried

   Set wrdApp = CreateObject("Word.Application")
   ' Set to false when done
   wrdApp.Visible = True
   wrdapp.maximized = true

'This will put your program behind the scenes until they close the word app.  


Author Comment

ID: 1524353
This doesn't prevent them from Alt-Tab'ing to my app or minimizing Word to get to it.  I can't allow them to use my app again until the document is closed.

Expert Comment

ID: 1524354
Have you tried summoning Word from a VBModal form in your app...

Pseudo code here would be

Your app loads a VBmodal form (this will lock the rest of your app...)

Create the word object... get the HWND handle id

Check status of Word... when HWND handle is terminated, close VBModal form, and release control of your app back to user.


Accepted Solution

tyy8 earned 600 total points
ID: 1524355
Okay here ya go...

1. Open Word and grab the window handle (very important)
2. Now use the FindWindow API in a DO...LOOP

I'd use the classname parameter in the API and leave the window caption alone (they could save it as any name they want and still have Word open and FindWindow would be fooled into thinking it's closed. Then just loop through until FindWindow returns 0 (no Word doc is open) or is not equal to your handle (another Word doc isd open but is not the one you launched). That way your program's logic is lost in this loop and cannot go forward until that Word doc is closed. Then the flow returns to your program. Also, don't forget to throw in a DoEvents into your loop (Very important). So the Word doc can have access to the processor - if you don't then you've essentially are locking up the program.

Author Comment

ID: 1524356
Thanks tyy8.  I actually just finished implementing a solution similar to this just before I was notified of your answer.  I used IsWindow to check to see if the handle still exists.  But since this is my first time on the Expert Exchange I'll give you the points.  Thanks again.

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

588 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question