Microsoft KB Archive/246021

= How To Close an Instance of Word from Visual FoxPro Using APIs =

Article ID: 246021

Article Last Modified on 6/29/2004

-

APPLIES TO


 * Microsoft Visual FoxPro 5.0a
 * Microsoft Visual FoxPro 6.0 Professional Edition

-



This article was previously published under Q246021



SUMMARY
This article describes how you can close a running instance of Microsoft Word using the SendMessage Application Program Interface (API).



MORE INFORMATION
The following code terminates an instance of Word 97 or Word 2000, even if the current document has been changed. Note that you will not be prompted to save changes. Terminating a running Windows application in this manner is considered bad programming style, however, this method can be effective if you need to quickly close Word.

This code can be used with either an instance of Word that is already running or if Word is instantiated using OLE Automation, this example uses OLE Automation to start Word.

Create a program and enter the following code: DECLARE LONG FindWindowA IN WIN32API STRING class, STRING title DECLARE INTEGER SendMessage IN Win32API INTEGER HWND,INTEGER Msg, Short WParam, INTEGER LPARAM)

oWord = Createobject('Word.Application') oWordHandle = FindWindowA('OpusApp',NULL) WITH oWord .Documents.Add .Visible=.T.   .Selection.InsertAfter ("This is a test.") .Application.Activate  && Activate Word. ENDWITH =INKEY(3)                  && Wait a couple seconds then close Word. =SendMessage(oWordHandle,16,0,0)
 * Pass the class name for Word - "OpusApp" to FindWindowA

