>=PrintPDF("x:\dssvfp\xfrx\test.pdf") > >FUNCTION PrintPDF > LPARAMETERS tcPDFFile > ** ToDo: Add Error Checking for Parameter > > 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 " + tcPDFFile, "", 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(tcPDFFile)) $ 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(tcPDFFile)) $ 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) > >ENDFUNC >