Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Specifying a Printer

romolo
Explorer
0 Likes
1,308

Hi Everyone,

I am trying to print my Excel worksheet to a specific printer like a local one on my machine or to a network printer, but having no luck. Could someone please tell me how I could change my code below to print to specific printers like "Printer1" on my LPT1 port and also to a network printer like "Printer 2" on \\Server2?

Many thanks in advance.

 

REPORT YTEST_EXCEL09.

 

INCLUDE OLE2INCL .

DATA: E_APPL TYPE OLE2_OBJECT,

E_WORK TYPE OLE2_OBJECT,

E_SHEET TYPE OLE2_OBJECT.

 

* Start the application

CREATE OBJECT E_APPL 'EXCEL.APPLICATION'.

SET PROPERTY OF E_APPL 'VISIBLE' = 0.

 

* Open the file

CALL METHOD OF E_APPL 'WORKBOOKS' = E_WORK.

 

CALL METHOD OF E_WORK 'OPEN'

EXPORTING

#1 = 'c:\test.xls'.

CALL METHOD OF E_APPL 'ACTIVESHEET' = E_SHEET .

CALL METHOD OF E_SHEET 'PRINTOUT'

EXPORTING

#01 = 1      "From

#02 = 1      "To

#03 = 3.     "Copies

 

* Close the file 

CALL METHOD OF E_WORK 'close'.

 

* Quit the file 

CALL METHOD OF E_APPL 'QUIT'.

1 ACCEPTED SOLUTION
Read only

romolo
Explorer
0 Likes
1,271

Thanks, Raymond. I tried adding the 5th parameter using the syntax below (highlighted in blue), but couldn't get it to work: nothing prints, not even from the system default printer. Any ideas?

CALL METHOD OF E_SHEET 'PRINTOUT'

EXPORTING

#01 = 1      "From

#02 = 1      "To

#03 = 3.     "Copies

#05 = 'Printer1 on LPT1:'.

9 REPLIES 9
Read only

RaymondGiuseppi
Active Contributor
0 Likes
1,271

Use the 5th parameter of method Worksheets.PrintOut it is the printer name, 4th is preview.(ref : Worksheets.PrintOut Method (Excel))

Regards,

Raymond

Read only

romolo
Explorer
0 Likes
1,272

Thanks, Raymond. I tried adding the 5th parameter using the syntax below (highlighted in blue), but couldn't get it to work: nothing prints, not even from the system default printer. Any ideas?

CALL METHOD OF E_SHEET 'PRINTOUT'

EXPORTING

#01 = 1      "From

#02 = 1      "To

#03 = 3.     "Copies

#05 = 'Printer1 on LPT1:'.

Read only

0 Likes
1,271

Try to give a false value to 4th parameter (preview).

Regards,

Raymond

Read only

0 Likes
1,271

I tried adding #04 = 'X' but it was the same.

Read only

0 Likes
1,271

Then try to record a macro in your version of Excel and anlayze the vb source generated.

Read only

romolo
Explorer
0 Likes
1,271

I took the 05 parameter (printer name)  'Printer1 on LPT1:' form the vb source

Read only

0 Likes
1,271

You could else try to use method ActivePrinter of Workbook before printout ?

Read only

romolo
Explorer
0 Likes
1,271

I figured it out: your initial suggestion of using a 4th parameter worked perfectly. I just had to use a valid value for that parameter. In the end, the final code that worked is as below.

Thanks again for your help!

CALL METHOD OF E_SHEET 'PRINTOUT'

EXPORTING

#01 = 1      "From

#02 = 1      "To

#03 = 3      "Copies

#04 = 0      "Preview

#05 = 'Printer1 on LPT1:'.     "ActivePrinter

Read only

0 Likes
1,271

Was a typo from mine : "a false value" for "a value of false" misled you ...