2020 Jan 31 8:25 PM
Hello Friends, I have a simple Excel file and I want to translate it to PDF with ABAP codes. Converting from DOC file to PDF is done with the code below but it doesn't happen when you try to integrate Excel into this code. Please Help Me.!!
***********************************************************************************
data: lv_wordapp type ole2_object,
lv_worddoc type ole2_object,
lv_wordadoc type ole2_object,
lv_wordcont type c length 8.
create object lv_wordapp 'WORD.APPLICATION'.
set property of lv_wordapp 'Visible' = 0.
call method of lv_wordapp 'Documents' = lv_worddoc.
call method of lv_worddoc 'Open'
exporting
#1 = 'D:\ITSM\huseyins.docx'. "Bu uzantıyı .xlsx şeklinde yaptığımda PDF formatına dönüştürmüyor.
call method of lv_wordapp 'ActiveDocument' = lv_wordadoc.
call method of lv_wordadoc 'Content' = lv_wordcont.
call method of lv_wordadoc 'SaveAs'
exporting
#1 = 'D:\ITSM\huseyins.pdf'
#2 = 17. "save as a PDF file
call method of lv_wordapp 'Quit'.
free object lv_wordapp .
***********************************************************************************
2020 Jan 31 10:35 PM
This will work. Paths are case sensitive:
*----------------------------------------------------------------------*
* Use Excel to convert a spreadsheet to pdf
*----------------------------------------------------------------------*
REPORT y_excel_ole_pdf.
LOAD-OF-PROGRAM.
* Allow OLE calls to COM enabled programs on Workstation (e.g. Excel)
INCLUDE ole2incl.
* handles for OLE objects
DATA: oexcel TYPE ole2_object, " Excel object
owbs TYPE ole2_object, " collection of workbooks
owb TYPE ole2_object. " workbook
*&---------------------------------------------------------------------*
*& Form ErrorHandler
*&---------------------------------------------------------------------*
* outputs OLE error if any was returned by SAP
*----------------------------------------------------------------------*
FORM errorhandler.
IF sy-subrc <> 0.
WRITE: / 'OLE-Automation Error:'(010), sy-subrc.
CALL METHOD OF oexcel 'Quit'.
FREE OBJECT oexcel.
STOP.
ENDIF.
ENDFORM. " ErrorHandler
PARAMETERS: p_source TYPE string LOWER CASE.
PARAMETERS: p_dest TYPE string LOWER CASE.
START-OF-SELECTION.
* Start a new copy of Excel
CREATE OBJECT oexcel 'EXCEL.APPLICATION'.
PERFORM errorhandler. "Set oExcel = CreateObject()
* Hide Excel window for now (faster)
SET PROPERTY OF oexcel 'Visible' = 0.
PERFORM errorhandler. "oExcel.Visible = False
* Tell Excel not to update entire worksheet with each change (faster)
SET PROPERTY OF oexcel 'ScreenUpdating' = 0.
PERFORM errorhandler. "oExcel.ScreenUpdating = False
* Get list of workbooks, initially empty
CALL METHOD OF oexcel 'Workbooks' = owbs.
PERFORM errorhandler. "Set oWBs = oExcel.Workbooks
* Open excel file, returning workbook
CALL METHOD OF owbs 'Open' "= owb
EXPORTING #1 = p_source.
PERFORM errorhandler. "oWBs.Open P_Source
CALL METHOD OF owbs 'Item' = owb
EXPORTING #1 = 1.
PERFORM errorhandler. "Set oWB = oWBs.Item(1)
* Export file as PDF using the name User defined previously
CALL METHOD OF owb 'ExportAsFixedFormat'
EXPORTING #1 = 0 "Type = xlTypePDF
#2 = p_dest. "FileName
PERFORM errorhandler. "oWB.ExportAsFixedFormat xlTypePDF, P_Dest
* Close Excel file without saving changes
CALL METHOD OF owb 'Close'
EXPORTING #1 = 0. "Save = False
PERFORM errorhandler. "oWB.Close ( Save = False)
* Tell Excel to resume updating entire worksheet with each change
SET PROPERTY OF oexcel 'ScreenUpdating' = 1.
PERFORM errorhandler. "Application.ScreenUpdating = True
* Tell Excel to show worksheet to the User
SET PROPERTY OF oexcel 'Visible' = 1.
PERFORM errorhandler. "oExcel.Visible = True
CALL METHOD OF oexcel 'Quit'.
PERFORM errorhandler. "oExcel.Quit
* Disconnect from Excel
FREE OBJECT oexcel.
PERFORM errorhandler.
This will work. Paths are case sensitive:
*----------------------------------------------------------------------*
* Use Excel to convert a spreadsheet to pdf
*----------------------------------------------------------------------*
REPORT y_excel_ole_pdf.
LOAD-OF-PROGRAM.
* Allow OLE calls to COM enabled programs on Workstation (e.g. Excel)
INCLUDE ole2incl.
* handles for OLE objects
DATA: oexcel TYPE ole2_object, " Excel object
owbs TYPE ole2_object, " collection of workbooks
owb TYPE ole2_object. " workbook
*&---------------------------------------------------------------------*
*& Form ErrorHandler
*&---------------------------------------------------------------------*
* outputs OLE error if any was returned by SAP
*----------------------------------------------------------------------*
FORM errorhandler.
IF sy-subrc <> 0.
WRITE: / 'OLE-Automation Error:'(010), sy-subrc.
CALL METHOD OF oexcel 'Quit'.
FREE OBJECT oexcel.
STOP.
ENDIF.
ENDFORM. " ErrorHandler
PARAMETERS: p_source TYPE string LOWER CASE.
PARAMETERS: p_dest TYPE string LOWER CASE.
START-OF-SELECTION.
* Start a new copy of Excel
CREATE OBJECT oexcel 'EXCEL.APPLICATION'.
PERFORM errorhandler. "Set oExcel = CreateObject()
* Hide Excel window for now (faster)
SET PROPERTY OF oexcel 'Visible' = 0.
PERFORM errorhandler. "oExcel.Visible = False
* Tell Excel not to update entire worksheet with each change (faster)
SET PROPERTY OF oexcel 'ScreenUpdating' = 0.
PERFORM errorhandler. "oExcel.ScreenUpdating = False
* Get list of workbooks, initially empty
CALL METHOD OF oexcel 'Workbooks' = owbs.
PERFORM errorhandler. "Set oWBs = oExcel.Workbooks
* Open excel file, returning workbook
CALL METHOD OF owbs 'Open' "= owb
EXPORTING #1 = p_source.
PERFORM errorhandler. "oWBs.Open P_Source
CALL METHOD OF owbs 'Item' = owb
EXPORTING #1 = 1.
PERFORM errorhandler. "Set oWB = oWBs.Item(1)
* Export file as PDF using the name User defined previously
CALL METHOD OF owb 'ExportAsFixedFormat'
EXPORTING #1 = 0 "Type = xlTypePDF
#2 = p_dest. "FileName
PERFORM errorhandler. "oWB.ExportAsFixedFormat xlTypePDF, P_Dest
* Close Excel file without saving changes
CALL METHOD OF owb 'Close'
EXPORTING #1 = 0. "Save = False
PERFORM errorhandler. "oWB.Close ( Save = False)
* Tell Excel to resume updating entire worksheet with each change
SET PROPERTY OF oexcel 'ScreenUpdating' = 1.
PERFORM errorhandler. "Application.ScreenUpdating = True
* Tell Excel to show worksheet to the User
SET PROPERTY OF oexcel 'Visible' = 1.
PERFORM errorhandler. "oExcel.Visible = True
CALL METHOD OF oexcel 'Quit'.
PERFORM errorhandler. "oExcel.Quit
* Disconnect from Excel
FREE OBJECT oexcel.
PERFORM errorhandler.
2020 Jan 31 8:28 PM
Thank you for visiting SAP Community to get answers to your questions. I recommend that you familiarize yourself with https://community.sap.com/resources/questions-and-answers (if you haven't already), as it provides tips for preparing questions that draw responses from our members. For example, you can outline what steps you took to find answers (and why they weren't helpful), share screenshots of what you've seen/done, make sure you've applied the correct tags, and so on. The more details you provide, the more likely it is that members will be able to assist you.
Should you wish, you can revise your question by selecting Actions, then Edit (although once someone answers your question, you'll lose the ability to edit the question). While editing, you can also "insert code" in the question (not just copy as text into the body of the question). You'll see the "insert code" option to the far right of the options (next to the "insert file" icon).
--Jerry
2020 Jan 31 10:35 PM
This will work. Paths are case sensitive:
*----------------------------------------------------------------------*
* Use Excel to convert a spreadsheet to pdf
*----------------------------------------------------------------------*
REPORT y_excel_ole_pdf.
LOAD-OF-PROGRAM.
* Allow OLE calls to COM enabled programs on Workstation (e.g. Excel)
INCLUDE ole2incl.
* handles for OLE objects
DATA: oexcel TYPE ole2_object, " Excel object
owbs TYPE ole2_object, " collection of workbooks
owb TYPE ole2_object. " workbook
*&---------------------------------------------------------------------*
*& Form ErrorHandler
*&---------------------------------------------------------------------*
* outputs OLE error if any was returned by SAP
*----------------------------------------------------------------------*
FORM errorhandler.
IF sy-subrc <> 0.
WRITE: / 'OLE-Automation Error:'(010), sy-subrc.
CALL METHOD OF oexcel 'Quit'.
FREE OBJECT oexcel.
STOP.
ENDIF.
ENDFORM. " ErrorHandler
PARAMETERS: p_source TYPE string LOWER CASE.
PARAMETERS: p_dest TYPE string LOWER CASE.
START-OF-SELECTION.
* Start a new copy of Excel
CREATE OBJECT oexcel 'EXCEL.APPLICATION'.
PERFORM errorhandler. "Set oExcel = CreateObject()
* Hide Excel window for now (faster)
SET PROPERTY OF oexcel 'Visible' = 0.
PERFORM errorhandler. "oExcel.Visible = False
* Tell Excel not to update entire worksheet with each change (faster)
SET PROPERTY OF oexcel 'ScreenUpdating' = 0.
PERFORM errorhandler. "oExcel.ScreenUpdating = False
* Get list of workbooks, initially empty
CALL METHOD OF oexcel 'Workbooks' = owbs.
PERFORM errorhandler. "Set oWBs = oExcel.Workbooks
* Open excel file, returning workbook
CALL METHOD OF owbs 'Open' "= owb
EXPORTING #1 = p_source.
PERFORM errorhandler. "oWBs.Open P_Source
CALL METHOD OF owbs 'Item' = owb
EXPORTING #1 = 1.
PERFORM errorhandler. "Set oWB = oWBs.Item(1)
* Export file as PDF using the name User defined previously
CALL METHOD OF owb 'ExportAsFixedFormat'
EXPORTING #1 = 0 "Type = xlTypePDF
#2 = p_dest. "FileName
PERFORM errorhandler. "oWB.ExportAsFixedFormat xlTypePDF, P_Dest
* Close Excel file without saving changes
CALL METHOD OF owb 'Close'
EXPORTING #1 = 0. "Save = False
PERFORM errorhandler. "oWB.Close ( Save = False)
* Tell Excel to resume updating entire worksheet with each change
SET PROPERTY OF oexcel 'ScreenUpdating' = 1.
PERFORM errorhandler. "Application.ScreenUpdating = True
* Tell Excel to show worksheet to the User
SET PROPERTY OF oexcel 'Visible' = 1.
PERFORM errorhandler. "oExcel.Visible = True
CALL METHOD OF oexcel 'Quit'.
PERFORM errorhandler. "oExcel.Quit
* Disconnect from Excel
FREE OBJECT oexcel.
PERFORM errorhandler.
2020 Feb 04 8:38 AM
Hello Juan. Thanks for your answer. Finally, I have a request from you. I want to rotate the table 90 degrees. Can you help me? Thanks.

| User | Count |
|---|---|
| 3 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |