Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Problem with closing Excel opened by automation
Message
 
 
To
All
General information
Forum:
Visual FoxPro
Category:
Forms & Form designer
Title:
Problem with closing Excel opened by automation
Environment versions
Visual FoxPro:
VFP 8 SP1
OS:
Windows XP
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01232734
Message ID:
01232734
Views:
53
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
Next
Reply
Map
View

Click here to load this message in the networking platform