Application Development 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: 

Changing ActivePrinter using OLE in Winword and Excel

Markus7
Participant
0 Kudos
515

Dear experts

I need to change the property ActivePrinter in Word and Excel. While it works in Word, I am not able to get it working in Excel. Please find below my ABAP code for Word and Excel. Any help is highly appreciated!

* Changing ActiveDocument in Winword
PARAMETERS: pa_prt1 TYPE rspoprname
              DEFAULT 'Canon MG5300 series Printer',
            pa_prt2 TYPE rspoprname
                DEFAULT 'Microsoft Print to PDF'.

DATA: lo_application    TYPE ole2_object,
      lo_documents      TYPE ole2_object,
      lo_activedocument TYPE ole2_object,
      le_string         TYPE string.

CREATE OBJECT lo_application 'Word.Application'.
SET PROPERTY OF lo_application 'Visible' = 0.

CALL METHOD OF lo_application 'Documents' = lo_documents.
CALL METHOD OF lo_documents 'Open'
  EXPORTING
    #1 = 'C:\Temp\File.doc'.
CALL METHOD OF lo_application 'ActiveDocument' = lo_activedocument.

GET PROPERTY OF lo_application 'ActivePrinter' = le_string.

CASE le_string.
  WHEN pa_prt1.
    SET PROPERTY OF lo_application 'ActivePrinter' = pa_prt2.
  WHEN pa_prt2.
    SET PROPERTY OF lo_application 'ActivePrinter' = pa_prt1.
  WHEN OTHERS.
ENDCASE.

CALL METHOD OF lo_activedocument 'Close'.
CALL METHOD OF lo_application 'Quit'
  EXPORTING
    #1 = 0.

* Changing ActiveDocument in Excel
PARAMETERS: pa_prt1 TYPE rspoprname
              DEFAULT 'Canon MG5300 series Printer',
            pa_prt2 TYPE rspoprname
                DEFAULT 'Microsoft Print to PDF'.

DATA: lo_application    TYPE ole2_object,
      lo_workbooks      TYPE ole2_object,
      lo_activeworkbook TYPE ole2_object,
      le_string         TYPE string.

CREATE OBJECT lo_application 'Excel.Application'.
SET PROPERTY OF lo_application 'Visible' = 0.

CALL METHOD OF lo_application 'Workbooks' = lo_workbooks.
CALL METHOD OF lo_workbooks 'Open'
  EXPORTING
    #1 = 'C:\Temp\Excel.xls'.
CALL METHOD OF lo_application 'ActiveWorkbook' = lo_activeworkbook.

GET PROPERTY OF lo_application 'ActivePrinter' = le_string.

CASE le_string.
  WHEN pa_prt1.
    SET PROPERTY OF lo_application 'ActivePrinter' = pa_prt2.
  WHEN pa_prt2.
    SET PROPERTY OF lo_application 'ActivePrinter' = pa_prt1.
  WHEN OTHERS.
ENDCASE.

CALL METHOD OF lo_activeworkbook 'Close'.
CALL METHOD OF lo_workbooks 'Close'.
CALL METHOD OF lo_application 'Quit'
  EXPORTING
    #1 = 0.

4 REPLIES 4

raymond_giuseppi
Active Contributor
0 Kudos
124

Which value did you actually get for Application.Activeprinter, for Excel shouldn't it be like '<printer> on <port>' such as 'PDFCreator on Ne01:' ?

0 Kudos
124

You ar right, I missed "on <port>". Thanks for this hint!! This is surely part of my problem but I am still not able to solve it. My printers are

  • Microsoft Print to PDF auf Ne01:
  • Canon MG5300 series Printer auf Ne05:

I changed my code as follows but SET PROPERTY OF lo_application 'ActivePrinter' = le_string. with le_string having value:

  • Microsoft Print to PDF auf Ne01:
  • Canon MG5300 series Printer auf Ne05:

does not work either.

*Changing ActiveDocument in Excel
PARAMETERS: pa_prt1 TYPE rspoprname
              DEFAULT 'Canon MG5300 series Printer',
            pa_prt2 TYPE rspoprname
              DEFAULT 'Microsoft Print to PDF'.

  DATA: lo_application    TYPE ole2_object,
        lo_workbooks      TYPE ole2_object,
        lo_activeworkbook TYPE ole2_object,
        le_string         TYPE string,
        le_printer        TYPE string,
        le_separator      TYPE string,
        le_port           TYPE string.

  CASE sy-langu.
    WHEN 'E'.
      le_separator = 'on'.
    WHEN 'D'.
      le_separator = 'auf'.
    WHEN OTHERS.
  ENDCASE.

  CREATE OBJECT lo_application 'Excel.Application'.
  SET PROPERTY OF lo_application 'Visible' = 0.

  CALL METHOD OF lo_application 'Workbooks' = lo_workbooks.
  CALL METHOD OF lo_workbooks 'Open'
    EXPORTING
      #1 = 'C:\Temp\Excel.xls'.
  CALL METHOD OF lo_application 'ActiveWorkbook' = lo_activeworkbook.

  GET PROPERTY OF lo_application 'ActivePrinter' = le_string.
  IF le_separator IS NOT INITIAL.
    SPLIT le_string AT le_separator INTO le_printer le_port.
    CLEAR: le_string.
    CONDENSE: le_printer, le_port.
  ENDIF.

  CASE le_printer.
    WHEN pa_prt1.
      CONCATENATE pa_prt2 le_separator 'Ne01:' INTO le_string SEPARATED BY space.
    WHEN pa_prt2.
      CONCATENATE pa_prt1 le_separator 'Ne05:' INTO le_string SEPARATED BY space.
    WHEN OTHERS.
  ENDCASE.
  SET PROPERTY OF lo_application 'ActivePrinter' = le_string.

  CALL METHOD OF lo_activeworkbook 'Close'.
  CALL METHOD OF lo_workbooks 'Close'.
  CALL METHOD OF lo_application 'Quit'
    EXPORTING
      #1 = 0.

Markus7
Participant
0 Kudos
124

No suggestions?

0 Kudos
124

Hi Markus.

I had the same problem and solved just now. Different computers have different printer ports. See the following link.

[ ABAP OLE ] How to print with local non-system default printer?