Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking if Word is running

Status
Not open for further replies.

StewartUK

Programmer
Feb 19, 2001
860
GB
My VFP program checks to see if Word is running by looking through all the active programs and checking their title bar captions. It looks for a caption that starts "Microsoft Word". It does this using GetActiveWindow & GetWindowText.

Unfortunately, when Word 2000 has a document open, the caption becomes "Document Name - Microsoft Word"! Why did they change it????

I've found that I could use the version number and change my test accordingly, but is there another way of checking what programs are running, other than looking at the title bar?

Thanks for any help,

Stewart
 
Hi!

The Word main window have the "OpusApp" class name. So you can search for that class name using something like following:

Code:
#DEFINE C_ClassName "OpusApp"

m.nWHandle = GetActiveWindow() && get active window of current process.

DO WHILE m.nWHandle>0
	m.lcClassTemp = SPACE(1000)
	IF GetClassName(m.nWHandle, @m.lcClassTemp, 1000) > 0
		IF C_ClassName $ m.lcClassTemp
			m.aaaWordsCount = m.aaaWordsCount + 1
			DIMENSION m.aaaWords(m.aaaWordsCount)
			m.aaaWords[m.aaaWordsCount] = m.nWHandle
		ENDIF
	ENDIF
	m.nWHandle = GetWindow(m.nWHandle, GW_HWNDNEXT)
ENDDO
m.nWHandle = GetActiveWindow()
DO WHILE m.nWHandle>0
	m.lcClassTemp = SPACE(1000)
	IF GetClassName(m.nWHandle, @m.lcClassTemp, 1000) > 0
		IF C_ClassName $ m.lcClassTemp
			m.aaaWordsCount = m.aaaWordsCount + 1
			DIMENSION m.aaaWords(m.aaaWordsCount)
			m.aaaWords[m.aaaWordsCount] = m.nWHandle
		ENDIF
	ENDIF
	m.nWHandle = GetWindow(m.nWHandle, GW_HWNDPREV)
ENDDO

I have also a complete set of the functions that I use for reliable working with Word instansed for OLE Automation. We used them in the VFP automation service that runs in background and works with Word documents and prints them. They're quite reliable.

Vlad Grynchyshyn
vgryn@softserve.lviv.ua
The professional level of programmer could be determined by level of stupidity of his/her bugs
 
Other suggestions I've heard include:

lcBuffer=SPACE(1000)
GetWindowText(hWnd,lcBuffer,LEN(lcBuffer))
If 'MICROSOFT WORD' $ UPPER(lcBuffer)
? 'Please close Word'
End If

A more radical approach includes attempting to rename the Word executable. If it fails, either the user has word open or it's being automated.

oWord=CREATEOBJECT('Word.Application')
lcWord=oWord.Path
oWord.quit
ON ERROR llError = .T.
RENAME (lcWord+'\winword.exe') to (lcWord+'\word.exe')
IF llError
? 'Word is in use'
ELSE
RENAME (lcWord+'\word.exe') to (lcWord+'\winword.exe')
END IF Jon Hawkins
 
Maybe simply,


GetObject(, "Word.Application")

If Err.Number <> 0 Then

'code here.

End If

enjoy.
sn5
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top