*************************************************************************** * 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,
>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>