Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

programmatically changing printer settings 1

Status
Not open for further replies.

ids4ids

Technical User
Jun 17, 2004
26
0
0
RO
I try to convert in vfp the code from to change paper size from my application but I get the error "5 Access is denied" at GetPrinterA function.
Can anybody help me ?

This is vfp code:

FUNCTION SetMyPrinter
PARAMETERS pPrinterName,dmOrientationPaper,dmFormatPaper
*
LOCAL rezultat,eroare
LOCAL lcPrintDefaults,HandlePrinter,SizeOfPrinterInfo,InfoPrinter
*
DECLARE Integer GetLastError IN kernel32.dll
DECLARE Integer SetLastError IN kernel32.dll
DECLARE Integer FormatMessage IN kernel32.dll ;
Integer dwFlags, String @lpSource, Integer dwMessageId, ;
Integer dwLanguageId, String @lpBuffer, Integer nSize, Integer Arguments
DECLARE Integer OpenPrinter IN WinSpool.Drv ;
String pPrinterName, Integer@ phPrinter, String pDefault
DECLARE Integer ClosePrinter IN WinSpool.Drv ;
Integer hPrinter
DECLARE Integer GetPrinter IN winspool.drv AS GetPrinterA ;
Integer hPrinter, Integer LvL, String @pPrinter, ;
Integer cbBuf, Integer @pcbNeeded
DECLARE Integer SetPrinter IN WinSpool.Drv ;
Integer hPrinter, Integer LvL, ;
Integer pPrinter, Integer Cmd
*
#DEFINE PRINTER_ACCESS_ADMINISTER 4
#DEFINE PRINTER_ALL_ACCESS 0xf000c
#DEFINE PRINTER_INFO 2
#DEFINE ERROR_INSUFFICIENT_BUFFER 122
*
STORE 0 TO HandlePrinter,rezultat,SizeOfPrinterInfo
STORE '' TO eroare,lcPrintDefaults
*
pPrinterName=GETPRINTER()
*
lcPrintDefaults=REPLICATE(CHR(0),8)+CHR(PRINTER_ACCESS_ADMINISTER)+REPLICATE(CHR(0),3)
rezultat=OpenPrinter(pPrinterName,@HandlePrinter,lcPrintDefaults)
IF rezultat=0
eroare=ApiError()
MESSAGEBOX("Cannot open printer "+pPrinterName+CHR(13)+eroare,0+16+0,'ATENTIE !')
RETURN -1
ENDIF
*
* SetLastError(0)
*
InfoPrinter=''
rezultat=GetPrinterA(HandlePrinter,PRINTER_INFO,@InfoPrinter,SizeOfPrinterInfo,@SizeOfPrinterInfo)
IF rezultat=0
eroare=ApiError()
MESSAGEBOX("Cannot get information for printer "+pPrinterName+CHR(13)+eroare,0+16+0,'ATENTIE !')
ClosePrinter(HandlePrinter)
RETURN -1
ENDIF
*

? InfoPrinter


*
ClosePrinter(HandlePrinter)
******
FUNCTION ApiError
LOCAL lcErrBuffer,lcErrorMess,liNewErr,lnErrorCode
lcErrBuffer=REPLICATE(CHR(0),1000)
lnErrorCode=GetLastError()
liNewErr=FormatMessage(0x1000,.NULL.,lnErrorCode,0,@lcErrBuffer,500,0)
lcErrorMess = TRANSFORM(lnErrorCode)+ " " +LEFT(lcErrBuffer,AT(CHR(0),lcErrBuffer)-1)
RETURN lcErrorMess
ENDFUNC
*
ENDFUNC
 
thanks jokey2...

Can anybody help me how can I parse cDevMode buffer from example (from link) to obtain and modify paper size, paper lenght, paper width, scale,color, duplex, print quality ?
 
I have not access to news2news members area...

I found how to parse cDevMode and how to modify
dmPaperSize,dmPaperLength and dmPaperWidth :

dmPaperSize=buf2word(SUBSTR(cDevmode,47,2))
dmPaperLength=buf2word(SUBSTR(cDevmode,49,2))
dmPaperWidth=buf2word(SUBSTR(cDevmode,51,2))

I know that if a member of DEVMODE is initialized, its corresponding bit of dmFields is set, otherwise the bit is clear.

How to modify dmFields to correspond with the modified DEVMODE members ?

For example I tried to change bit 2 for dmPaperSize using
BITSET(dmFields,2) but didn't work, the changes are not reflected to printing preferences of selected printer.
 

Would it be easier to hack you report and put in the values you need, rather then changing to actul printer driver? This information you trying change is stored in the report itself on the field Expr of the report if you use the report like a table.
Take a look at faq184-1934 to change the printer tray.


Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
ReFox XI (www.mcrgsoftware.com)
 
I don't setting printer for report printing, I want to
print files without user intervention from a directory -
I find the file (pdf), I see the page size of document (all pages of document have the same page size), I setting the printer and I print the file using ShellExecute.
 
Dear IDS4Ids

It is still very unclear what you want to achieve: reading your last posting I understand you are printing PDF files. Why do you want to reset the papertray and size? These values are stored in yourReport.FRX look for the field Expr they can be hacked or changed if you prefer that expression. However I would simply base my report on the correct settings and incase you would need an other setting, for what ever reason, build a second yourReport.FRX whith these settings. Programaticaly you can select the correct report with the corresponding settings.
If this sounds like nonsense to you I prefer to have a more detailed info what/why you are changing default behaviour.
Best regards,

Jockey(2)
horse1.gif
 
I find the file (pdf), I see the page size of document (all pages of document have the same page size), I setting the printer and I print the file using ShellExecute.

PDF? Your first post does not indicate that. Vut why would you want to change the printer settings to print a PDF file. Wheter through ShellExcute or Acrobat reader, the printer driver will ajust itself accordingly, unless you are trying to limit the number of pages being printed, but paper size shouldn't have to be changed.

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
ReFox XI (www.mcrgsoftware.com)
 
Here is my situation:
I want to change printer setting for printing PDF files, NOT for printing .frx files (vfp reports) and NOT for printing .frx files in pdf format.
I have installed Acrobat Reader 7.0.
My default printer has default paper size A4.
When print the PDF file with ShellExecute(0,"Print",file,"","",0) Adobe Reader is launched (but file is not open) and file is printed.
I printed 3 files with different paper sizes (A4,A3,Letter) and I checked every print job from spooler queue if the driver is ajusted itself and it was not. All print jobs have the default paper size.
In case of using another printer (not default printer) I select programmaticaly the default printer (using Windows Scripting) but the change is not done immediate. I must pause the vfp execution for minimum 2 seconds and after that the chage is done. If Adobe Reader is already launched the chage is ignored.
Can anybody have a suggestion ?
 
Ids4ids,
Hi,
do I understand correctly that e.g. yourReport.FRX is in size A4 and would you like to print this as yourReport.PDF in size A5?
Are you using VFPSP1-9.0 with ReportListener.VCX? if not which VFP version?

Regards,

Jockey(2)
 
Jockey2,

do I understand correctly that e.g. yourReport.FRX is in size A4 and would you like to print this as yourReport.PDF in size A5

You might be misreading the question, he is not using Foxpro reports, his using PDF files directly to the printer using ShellExecute.

Ids4ids said:
I want to change printer setting for printing PDF files, NOT for printing .frx files (vfp reports) and NOT for printing .frx files in pdf format.
I have installed Acrobat Reader 7.0.

Ids4ids
Here is again a typical problem with what your are asking.
1. This is barely a Foxpro problem since API call are Windows related and Adobe Acrobat nor ShellExecute are not a FoxPro isuue.
2. Your solution 'almost' works but not quite. So instead of looking at the solution Jockey2 and I propose (Mine was from the author of the solution which costs $5.00 USD to acquire and Jockey2 exposes the same solution but free) (and those two solution can be easely adapted to your needs). But you insist on trying to makes yours work. Why re-invent the wheel?

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
ReFox XI (www.mcrgsoftware.com)
 
Mike,

if he is indeed not using FoxPro reports but PDF files which he wants to print using a FoxPro app or exe and wants Fox to change the papersettings of his PDF files than I can only agree with you. Let Acrobat change the papersettings and present foxpro with the correct result to print. What he could also do is to make Acrobat print to TIF file, import into a Foxreport in the correct papersize and print. Sounds like going from Amsterdam to Rome with a stopover at Vancouver, but I have seen bigger trips.
Just curious what Ids4kids wants.

Regards,
Jockey(2)

 
Mike, Jockey2,

I want to print pdf files without user intervention using VFP7 but the problem is that Acrobat Reader 7.0 don't change by itself the paper size in printer driver to suit the paper size of document from pdf file.
 
Ids4kids,
OK if you would like to print PDF files created by Acrobat Reader in a different papershape my best bet is to attack Acrobat and change the coding overthere to the requested paper size. Tell me more specific why / what is going on.
Regards,
Jockey(2)
 
Hi Jockey2,

I want to print pdf files from a hot folder.
Application must check periodically a folder and print the pdf files without user intervention. But, most important, the application must change driver paper size concordantly with the document paper size from pdf file. For example, if I want to print the file "testa4.pdf" which contain a document in A4 format, my application must change the paper size from printer driver to print on proper paper size (A4) - I don't want to print an A4 document on A3 paper size.

Direct printing using ShellExecute call Adobe Reader to print the file but the paper size is not changed by Adobe Reader itself. In case of other type of files (MS Word, Excel,...) the paper size is changed by application (Word, Excel...) itself.

I tried to change the paper size from my program but I don't know what I did wrong because it didn't works (I changed the structure, I saved it but without a result in printing process)...I used API DocumentProperties to change DEVMODE structure...I should use SetPrinter...?...I don't know how to change DEVMODE substructure from PRINTER_INFO_2 structure in case of using SetPrinter...

I found a solution (theoretical, it's not tested yet) but it's not what I'm looking for... to check the document paper size from pdf file and to print using different installations of the same printer driver. For example, I have the same printer installed for twice ("prn1" is setted to use A4 as default paper size and "prn2" is setted to use A3 as default paper size) and I want to print 2 pdf files ("f1.pdf" is A4 format and "f2.pdf" is A3 format). In this situation I check what paper size is for "f1.pdf", I find that it is A4 and I set default printer to "prn1" and I print the file, idem for "f2.pdf".

Have anybody any suggestion ?
 
Hi Jockey2, Mike,

Finally I found the solution (it's just I'm looking for) to change, without user intervention, the paper size of printer driver to suit paper size of pdf file. I found how to change immediate the default printer too.
From my point of view this topic is closed.
Thank you for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top