Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Problem with closing Excel opened by automation
Message
 
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Gestionnaire d'écran & Écrans
Versions des environnements
Visual FoxPro:
VFP 8 SP1
OS:
Windows XP
Database:
Visual FoxPro
Divers
Thread ID:
01232734
Message ID:
01233535
Vues:
11
Here are some additional observations related to the problem.

If I create a CSV file, open it with Excel automation, then I'm able to create CSV again and a new instance of Excel could be opened too (just shows read-only).

For XLS file, if I create a file, open Excel, minimize it, then try to create the same file again, it gives an error.

The following code works for me (e.g. after the Excel is brought to the front, I can close it, and there is no image remaining), but couple of points remain unsolved:

1) Why oSheel.AppActivate doesn't work?
2) Why I need to use ShowWindow after BringWindowToTop?
3) Why sleep doesn't work and is there a way to don't use DO WHILE loop (I suspect it's ineffiecient)?

Thanks a lot in advance for your suggestions.
***************************************************************************
* Class Method/Function: Frm_rpt_payment.fileclosed
* Class Library........: s:\vfpapps\ecs\source\forms.vcx
* Base Class...........: Form
* Include File ........: s:\vfpapps\ecs\source\include.h
* Description..........: Frm_Rpt_Payment.FileClosed
* Returns..............: Logical
***************************************************************************
* Author...............: Nadya Nosonovsky
* Date Created.........: 06/13/2007
* Date Modified........: 06/13/2007
* Compiler.............: Visual FoxPro 08.00.0000.3117 for Windows
***************************************************************************
LPARAMETERS tcFile
LOCAL llReturn, lnHandle, lnHwnd, loShell
llReturn = .T.
IF NOT EMPTY(SYS(2000,m.tcFile)) && File does exist
	lnHandle = FOPEN(m.tcFile, 12)
	IF m.lnHandle < 0
		=MESSAGEBOX('This file is already opened. Please close Excel...', 0, "File is opened")
		DECLARE Sleep IN kernel32 INTEGER dwMilliseconds
		IF VARTYPE(THISFORM.oExcel) = 'O'

			DECLARE INTEGER FindWindow IN user32 ;
				STRING lpClassName, STRING lpWindowName
				
			lnHwnd = FindWindow(NULL, THISFORM.oExcel.CAPTION)
			
			IF m.lnHwnd > 0
				DECLARE INTEGER ShowWindow IN user32 AS ShowWindowA ;
					INTEGER hWindow, INTEGER nCmdShow
					
				DECLARE SHORT BringWindowToTop IN WIN32API INTEGER HWND
*!*	                DECLARE SHORT SetForegroundWindow IN WIN32API ;
*!*	                INTEGER hWnd
*   =SetForegroundWindow(m.lnHwnd)
				=BringWindowToTop(m.lnHwnd)
				ShowWindowA(m.lnHwnd, 1)
			ENDIF

* This code doesn't work for Excel
*!*				loShell=CREATEOBJECT("WScript.Shell")
*!*				loShell.AppActivate(THISFORM.oExcel.CAPTION)
*!*				loShell.SendKeys("")
*           This works and Sleep doesn't work for some reason
			DOEVENTS
			lnI = 0
			DO WHILE VARTYPE(THISFORM.oExcel) = 'O' AND m.lnI < 1000000
				lnI = m.lnI + 1
			ENDDO
		ENDIF
*		Sleep(1000*10) && Hopefully 10 sec. would be enough to save and close Excel
		lnHandle = FOPEN(m.tcFile, 12)
		IF m.lnHandle < 0 && Still opened
			=MESSAGEBOX("Please try again...",0,"File is opened")
			llReturn = .F.
		ELSE
			FCLOSE(m.lnHandle)
		ENDIF
	ELSE
		FCLOSE(m.lnHandle)
	ENDIF
ENDIF

RETURN m.llReturn
>Hi everybody,
>
>I've added an opening Excel file (making Excel visible by automation) in one of my form.
>
>In the form I may press Save to Excel button multiple times before actually closing Excel.
>
>So, I've added this simple code to check, if I can produce an Excel file
>
>
>LPARAMETERS tcFile
>LOCAL llReturn, lnHandle, lnHwnd
>llReturn = .T.
>IF NOT EMPTY(SYS(2000,m.tcFile)) && File does exist
>	lnHandle = FOPEN(m.tcFile, 12)
>	IF m.lnHandle < 0
>		=MESSAGEBOX('This file is already opened. Please close Excel...', 0, "File is opened")
>		DECLARE Sleep IN kernel32 INTEGER dwMilliseconds
>		DECLARE INTEGER FindWindow IN user32;
>        STRING lpClassName, STRING lpWindowName
>        lnHwnd = FindWindow(Null, "Microsoft Excel - " + JUSTFNAME(m.tcFile))
>        IF m.lnHwnd > 0
>	           DECLARE INTEGER ShowWindow IN user32 AS ShowWindowA ;
>	           INTEGER hWindow, INTEGER nCmdShow
>					DECLARE SHORT BringWindowToTop IN WIN32API ;
>					INTEGER hWnd
>                DECLARE SHORT SetForegroundWindow IN WIN32API ;
>                INTEGER hWnd
>                =BringWindowToTop(m.lnHwnd)
>                =ShowWindow(m.lnHwnd,1)
>        ENDIF
>        * I close Excel and see this bad effect
>		Sleep(1000*10) && Hopefully 10 sec. would be enough to save and close Excel
>		lnHandle = FOPEN(m.tcFile, 12)
>		IF m.lnHandle < 0 && Still opened
>			=MESSAGEBOX("Please try again...",0,"File is opened")
>			llReturn = .F.
>		ELSE
>			FCLOSE(m.lnHandle)
>		ENDIF
>	ELSE
>		FCLOSE(m.lnHandle)
>	ENDIF
>ENDIF
>RETURN m.llReturn
>
>It works almost fine except that after I press Close button on Excel, the image of it is still keeping around for some time.
>
>Do you see what is wrong in this code?
>
>Appreciate your advice.
>
>Thanks a lot in advance.
If it's not broken, fix it until it is.


My Blog
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform