Link to home
Start Free TrialLog in
Avatar of Howard Katz
Howard KatzFlag for United States of America

asked on

Problem printing .pdf from Foxpro

Hello Experts,
I need to simply print a .pdf file on a user-selected printer from a VFP9 app. The user doesnt want to view the file, and it gets erased after its printed.  Here is the code I wrote.  It works when I step through the debugger, but otherwise it doesnt.  Even putting in delays doesnt help.  Without the debugger the printer queue says Spooling after the PrintAll() call for the entire time of the Sleep() delay.  Then it briefly flashes Printing, and then the document is deleted from the queue.  It only prints part of the first page and stops.  Also, the program hangs.    In the debugger everything works OK.  

Here is the code:

PROCEDURE PrintPDF
LPARAMETERS tcFileName, tcPrinter
LOCAL loform1, lcOldPrinter

DECLARE INTEGER SetDefaultPrinter IN winspool.drv;
     STRING pszPrinter
DECLARE Sleep IN Win32API INTEGER nMilliseconds

** If no file, get out
IF !FILE(tcFileName)
      RETURN
ENDIF
WAIT WINDOW 'printing' NOWAIT
lcOldPrinter = SET("PRINTER",2)

** Set the default windows printer
SetDefaultPrinter(tcPrinter)
 
loform1=Newobject("form")
WAIT WINDOW 'adding AcroPDF object ...' nowait
loform1.AddObject("olecontrol2","AcroPDF1")
*=Sleep(3000)
WAIT WINDOW 'loading file ...' nowait
loform1.olecontrol2.loadfile(tcFileName)
=Sleep(3000)
WAIT WINDOW 'printing ...' nowait
loform1.olecontrol2.printall()
=Sleep(20000)

loform1.RemoveObject("olecontrol2")
loform1.Release()
ERASE (tcFileName)

** Now reset the printer
SetDefaultPrinter(lcOldPrinter)

RETURN

Define Class AcroPDF1 As OleControl
    OleClass = "AcroPDF.PDF.1"
    Height = 0
    Width = 0
ENDDEFINE
Avatar of CarlWarner
CarlWarner
Flag of United States of America image

Simpler way to print:

declare integer ShellExecute ;
      in shell32.dll;
      long hwnd, string cOperation, string cFile, string cParam, string cDir, integer nShow

? shellexecute(0, "print", "d:\temp\onepage.pdf","",fullpath(""), 0)
Avatar of Howard Katz

ASKER

This works, but the Adobe Reader pops up on the Task bar (I have Adobe 7.0).  Can you suggest code to find it and kill it?  What if the client has another version of the Reader?
That ShellExecute procedure uses the default PDF reader on a PC.  I only suggested it because most users don't have the commercial version of Abobe (nor do I) to call up and/or print a PDF.  

And yes there is a way to kill the Adobe Reader window.  I'll get it for you shortly-- I have it on another PC inaccessible to the network this PC is on.  The issue as you have found out is utilizing the Sleep function to wait for the process to finish its print job before you actually kill it.

ASKER CERTIFIED SOLUTION
Avatar of CarlWarner
CarlWarner
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks.  Haven't had a chance to try it but thanks for your effort and quick response.  If any other issues appear when I do try, I will re post.
You're welcome.

I just know it all works for me in one old VFP6 application.