* Program...........: PRINTPDF.PRG * Author............: John St. Andria thread #033100 * Project...........: Visual Collections * Created...........: 04/14/2005 12:55:23 * Copyright.........: (c) Jzanus LTD, 2005 *) Description.......: this function prints PDF passed as parameter and kills Adobe afterwards * Calling Samples...: * Parameter List....: tcFileName * Major change list.: lparameters tcPDF if vartype(m.tcPDF)<>"C" or adir(laFile,m.tcPDF) = 0 tcPDF = getfile("PDF","Select PDF file to print...") endif local llReturn llReturn = .f. if not empty(m.tcPDF) LOCAL lcWindowTitle, lnWindowTitleLen, lnVFPhWnd, lnStart, lnReaderHwnd *== API Declarations DECLARE INTEGER ShellExecute IN "Shell32.dll" ; INTEGER hwnd, ; STRING lpVerb, ; STRING lpFile, ; STRING lpParameters, ; STRING lpDirectory, ; LONG nShowCmd DECLARE INTEGER GetForegroundWindow IN win32API DECLARE INTEGER Sleep IN WIN32API INTEGER nMSecs && 1000 ms = 1 sec DECLARE SHORT SetForegroundWindow IN USER32.DLL INTEGER hWnd DECLARE INTEGER SetActiveWindow IN USER32 INTEGER hWnd DECLARE INTEGER FindWindow IN Win32API STRING @lpClassName, STRING @lpWindowName DECLARE INTEGER GetWindowText IN WIN32API INTEGER hwnd, STRING @lptstr, INTEGER cbmax lcWindowTitle = SPACE(50) lnWindowTitleLen = LEN(lcWindowTitle) *== Get HWND of current VFP session IF VERSION(5) >= 800 && VFP8 and later carry hWnd property lnVFPhWnd = _vfp.hWnd ELSE lnVFPhWnd = GetForegroundWindow() ENDIF *== Print The PDF = ShellExecute(0, "open", "acrord32.exe", " /p /h " + tcPDF, "", 0) *== Wait For Acrobat TO Become Active lnStart = DATETIME() DO WHILE .t. lnReaderHwnd = FindWindow(0, "Acrobat Reader") IF lnReaderHwnd = 0 lnReaderHwnd = FindWindow(0, "Adobe Reader") ENDIF IF lnReaderHwnd # 0 OR DATETIME() - lnStart > 30 && 30 seconds max EXIT ENDIF Sleep(100) ENDDO IF lnReaderHwnd = 0 ** Reader Did Not Start in 30 seconds. ** Just Set Focus And Bail SetForegroundWindow(lnVFPhWnd) SetActiveWindow(lnVFPhWnd) RETURN ENDIF *== Printing has begun when PDF name appears in title of Reader window DO WHILE .t. getwindowtext(lnReaderHwnd, @lcWindowTitle, lnWindowTitleLen) IF UPPER(JUSTFNAME(tcPDF)) $ UPPER(lcWindowTitle) EXIT ENDIF ENDDO *== Printing has finished when PDF name no longer in title DO WHILE .t. lcWindowTitle = SPACE(50) getwindowtext(lnReaderHwnd, @lcWindowTitle, lnWindowTitleLen) IF ! UPPER(JUSTFNAME(tcPDF)) $ UPPER(lcWindowTitle) EXIT ENDIF ENDDO *== Kill Reader Process IF SUBSTR(OS(1),9,1) >= "5" && 4 = Win 98, 5= 2K & XP LOCAL loWMI, loProcess loWMI = GetObject("winmgmts://./root/cimv2") loProcesses = loWMI.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'AcroRd32.exe'") For Each loProcess in loProcesses loProcess.Terminate(0) Next loProcesses = loWMI.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'Acrobat.exe'") For Each loProcess in loProcesses loProcess.Terminate(0) Next ENDIF *== Set Focus to VFP SetForegroundWindow(lnVFPhWnd) SetActiveWindow(lnVFPhWnd) llReturn = .t. endif return m.llReturn>
>>>IF ShellExecute(0,"print", FullPath('MyFile.pdf'),"","",1)>32 >>> >>> oShell = CreateObject("WScript.Shell") >>> *wait for Acrobat reader >>> lntimeout=60 >>> lnstarttime=SECONDS() >>> DO WHILE not oShell.AppActivate("Acrobat Reader") AND ; >>> SECONDS()-lnstarttime<lntimeout >>> INKEY(2) >>> ENDDO >>> >>> IF oShell.AppActivate("Acrobat Reader") >>> *While printing Acrobat reader cannot be closed; so repeat >>> * attempts to close it. >>> lnstarttime=SECONDS() >>> DO WHILE oShell.AppActivate("Acrobat Reader") AND ; >>> SECONDS()-lnstarttime<lntimeout >>> oShell.SendKeys("^q") && try to close Acrobat reader >>> INKEY(2) >>> enddo >>> endif >>> >>> oShell=.null. >>>ELSE >>> ?"file not found " >>>ENDIF >>> >>>>>>