HOWTO: Close an Instance of Word from Visual FoxPro Using APIs

ID: Q246021


The information in this article applies to:
  • Microsoft Visual FoxPro for Windows, versions 5.0a, 6.0
  • Microsoft Word 2000


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')<BR/>
* Pass the class name for Word - "OpusApp" to FindWindowA
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) 


REFERENCES

(c) Microsoft Corporation 1999, All Rights Reserved. Contributions by Dean Christopher, Microsoft Corporation.

Additional query words: kbDSE

Keywords : kbAPI kbAutomation kbVFp500a kbVFp600 kbVS600sp3 kbGrpFox kbCodeSnippet
Version : WINDOWS:2000,5.0a,6.0
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: December 7, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.