How to terminate Office Viewers with VFP

I have a cool application that displays documents using the Word Viewer and the Excel Viewer. The application works great except I can't figure out how to close the external viewer without actually clicking the application's File>Exit. I want Visual Foxpro to open and close the external applications.
mikejcurtisAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
tusharkanvindeConnect With a Mentor Commented:
Instead of C:\program files\Microsoft Office\Office12\xlview.exe maybe you should send m.pathname+"charts\"+(m.docname)+".XLS" as the cfilename
0
 
CaptainCyrilFounder, Software Engineer, Data ScientistCommented:
0
 
jrbbldrCommented:
" I can't figure out how to close the external viewer"

Since you say that you are using an Outlook "Viewer" can we assume that it is not Outlook itself?

Obviously you are launching the 'viewer' in some manner - either using an ActiveX to control it or using Windows' ShellExecute.

If you are using an ActiveX, then it should have the necessary controls to close it.

If you used ShellExecute to launch the 'viewer', you can use other Win32API functions to close it.

Good Luck

0
Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

 
mikejcurtisAuthor Commented:
Well, I'm actually not doing anything with Outlook, it's the Office file viewers for Word and Excel I'm using. know that I can use VFP's commands  to close it, and that's what I want to do, but I don't know how to determine the viewer's handle so that I can send commands to the app using FCLOSE() FOPEN() FWRITE() etc. I think that's the bit I'm missing, how to find out the application's addressable identity from VFP.  
0
 
CaptainCyrilFounder, Software Engineer, Data ScientistCommented:
FOPEN and FCLOSE are for reading and writing files in low level.

You need to handle the object of the viewer itself. Please show us how you invoke this user? Do you use ShellExecute? If that is the case then you don't have access to it directly, You have to SENDKEYs to it to control it.

If you use CREATEOBJECT or NEWOBJECT then you have a reference to it and you can close it by calling QUIT or EXIT.
0
 
CaptainCyrilFounder, Software Engineer, Data ScientistCommented:
xlapp = CREATEOBJECT("Excel.Application")
xlapp.Visible = .T.
WAIT WINDOW "Hello" TIMEOUT 5
xlapp.Quit
RELEASE xlapp
0
 
CaptainCyrilFounder, Software Engineer, Data ScientistCommented:
What you can also do is open the file inside a Visual FoxPro form but slapping an OLE Object on the form and then you have full control of the form and the object inside.
0
 
tusharkanvindeCommented:
I got this from Yousfi Benameur

LOCAL loWMI, lcQuery, loResult, loProcess
loWMI = GetObject("winmgmts://")
lcQuery = "select * from win32_process"
loResult = loWMI.ExecQuery(m.lcQuery)

FOR EACH loProcess IN loResult
      ?loProcess.Name
      if loProcess.name="yourexe"
         loProcess.Terminate(0)
      endi      
NEXT
0
 
mikejcurtisAuthor Commented:
I think I can be clearer on this now that I've banged my head on it repeatedly. Attached is the code I'm using to call either the Word Viewer app or the ExcelViewer and display the document, and the code I'm using to close the Word or Excel viewer.

What I'm not figuring out is how to make these two applications print their document when the Print button is pressed on the 'Documents' form.

To give some background, this application is written for touch screen computers set out in a factory environment. The Foxpro controls are all made extra large so the machine operators can easily make selections. That's why I don't want to use the application's native controls to print or exit, the users get too frustrated working through the small menu controls under time constraints.

&&Open the document in the viewer, this code runs on Load of the form 'Documents' and it works correctly
DO CASE
	CASE FILE((m.pathname)+"charts\"+(m.docname)+".DOC")
		DECLARE INTEGER ShellExecute IN shell32.dll ;
		INTEGER hndWin, ;
			STRING cAction, ;
			STRING cFileName, ;
			STRING cParameters, ;
			STRING cStartDir, ;
			INTEGER nShowWindow
		m.hndWin = 0 && Open a new window
		m.cAction = "open"
		m.cFilename = "C:\program files\Microsoft Office\Office11\wordview.exe"
		m.cParameters = (m.docname)+".DOC"
		m.cStartDir = (m.pathname)+"CHARTS\"  && passing this file with the path
		m.nShowWindow = 1 &&Normal
		ShellExecute(m.hndWin, m.cAction, m.cFileName, ;
		m.cParameters, m.cStartDir, m.nShowWindow)


	CASE FILE((m.pathname)+"charts\"+(m.docname)+".XLS")
		DECLARE INTEGER ShellExecute IN shell32.dll ;
		INTEGER hndWin, ;
			STRING cAction, ;
			STRING cFileName, ;
			STRING cParameters, ;
			STRING cStartDir, ;
			INTEGER nShowWindow
		m.hndWin = 0 && Open a new window
		m.cAction = "open"
		m.cFilename = "C:\program files\Microsoft Office\Office12\xlview.exe"
		m.cParameters = (m.docname)+".XLS"
		m.cStartDir = (m.pathname)+"CHARTS\"  && passing this file with the path
		m.nShowWindow = 1 &&Normal
		ShellExecute(m.hndWin, m.cAction, m.cFileName, ;
		m.cParameters, m.cStartDir, m.nShowWindow)

ENDCASE
SHOW WINDOW documents

&&Close the viewer, this code runs when the Exit button is pressed on the 'Documents' form. This works correctly too.

DECLARE LONG GetDesktopWindow IN WIN32API
lnDesktopWind = GetDesktopWindow()

DECLARE LONG GetWindow IN WIN32API LONG hWnd, LONG wCmd

#DEFINE GW_child 5
#DEFINE GW_HWNDNEXT 2
lnHWnd = GetWindow( lnDesktopWind, GW_CHILD )

DO WHILE lnHWnd <> 0

	DECLARE LONG GetClassName IN WIN32API ;
		LONG hWnd, STRING lpClassName, LONG nMaxCount
	
	lcClass = SPACE(256)
	lnLen = GetClassName( lnHWnd, @lcClass, LEN(lcClass) )
	lcClass = LEFT( lcClass, lnLen )

	 &&? "Next child is ", lnHWnd, lcClass
	DO CASE
		CASE ALLTRIM(lcClass)="XLMAIN"
			DECLARE LONG PostMessage IN WIN32API ;
				LONG hWnd, LONG wMsg, LONG wParm, LONG lparam
	
			#DEFINE WM_CLOSE 0x10
			PostMessage( lnHWnd, WM_CLOSE, 0, 0)

		CASE ALLTRIM(lcClass)="OpusApp"
			DECLARE LONG PostMessage IN WIN32API ;
				LONG hWnd, LONG wMsg, LONG wParm, LONG lparam
	
			#DEFINE WM_CLOSE 0x10
			PostMessage( lnHWnd, WM_CLOSE, 0, 0)
	ENDCASE
	lnHWnd = getWindow(lnHWnd, GW_HWNDNEXT )


ENDDO	
m.docname=""
CLEAR windows
DO FORM (m.callingform)

Open in new window

0
 
tusharkanvindeCommented:
You can call shellexecute with the print command insteadof the open command. Make

cAction='print'

instead of open

And you should put that in another procedure so you don't have to repeat the code.
0
 
mikejcurtisAuthor Commented:
I tried that. It didn't work. I tried sending the Alt keys to the application too. No errors either way, but no output either.
DO CASE
	CASE FILE((m.pathname)+"charts\"+(m.docname)+".DOC")
		DECLARE INTEGER ShellExecute IN shell32.dll ;
		INTEGER hndWin, ;
			STRING cAction, ;
			STRING cFileName, ;
			STRING cParameters, ;
			STRING cStartDir, ;
			INTEGER nShowWindow
		m.hndWin = 0 && Open a new window
		m.cAction = "print"
		m.cFilename = "C:\program files\Microsoft Office\Office11\wordview.exe"
		m.cParameters = (m.docname)+".DOC"
		m.cStartDir = (m.pathname)+"CHARTS\"  && passing this file with the path
		m.nShowWindow = 1 &&Normal
		ShellExecute(m.hndWin, m.cAction, m.cFileName, ;
		m.cParameters, m.cStartDir, m.nShowWindow)


	CASE FILE((m.pathname)+"charts\"+(m.docname)+".XLS")
		DECLARE INTEGER ShellExecute IN shell32.dll ;
		INTEGER hndWin, ;
			STRING cAction, ;
			STRING cFileName, ;
			STRING cParameters, ;
			STRING cStartDir, ;
			INTEGER nShowWindow
		m.hndWin = 0 && Open a new window
		m.cAction = "print"
		m.cFilename = "C:\program files\Microsoft Office\Office12\xlview.exe"
		m.cParameters = (m.docname)+".XLS"
		m.cStartDir = (m.pathname)+"CHARTS\"  && passing this file with the path
		m.nShowWindow = 1 &&Normal
		ShellExecute(m.hndWin, m.cAction, m.cFileName, ;
		m.cParameters, m.cStartDir, m.nShowWindow)

ENDCASE

Open in new window

0
 
mikejcurtisAuthor Commented:
Thanks tusharkanvinde ! Funny how the obvious can escape you when you work alone. It works great now!
0
All Courses

From novice to tech pro — start learning today.