Jump to content

Inserting a PDF file as object in Excel


Recommended Posts

Hello everyone!

This is my first topic, so please be gentle with me :P

I want insert a PDF file as object in a excel sheet.

I tried with this:

Global Const $FILEPATH_ICON_PDF = @ScriptDir & "\RESOURCES\pdf_file.ico"

;~~~~~~~~ Variables definition ~~~~~~~~~~~~~
Local $oExcel
Local $oWorkbook
Local $sFilepathXLS = @DesktopDir & "\EXCEL FILE TEST.xlsx"
Local $sFilepathPDF = @DesktopDir & "\PDF FILE TEST.pdf"
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

;Open excel application
$oExcel = _Excel_Open (True)
;Create a new excel book
$oWorkbook = _Excel_BookNew ($oExcel)
;Add pdf file
$oExcel.ActiveSheet.OLEObjects.Add ("", $sFilepathPDF, False, True, $FILEPATH_ICON_PDF, 0, "Document 1").Select

MsgBox (0, "TEST", "STOP") ;Stop the process and watch if the pdf file has been added

;Save the excel book
_Excel_BookSaveAs ($oWorkbook, $sFilepathXLS)
;Close the excel book
_Excel_BookClose ($oWorkbook)

And this:

$oExcel.ActiveSheet.OLEObjects.Add ($sFilepathPDF, False, True, $FILEPATH_ICON_PDF, 0, "Document 1").Select

The output that SciTE shows me is:
 

Quote

 

err.description is:     Add method of OLEObjects class failed
err.windescription:    Exception occurred.

err.number is:     80020009
err.lastdllerror is:     0
err.scriptline is:     1192
err.source is:     Microsoft Excel
err.helpfile is:     xlmain11.chm
err.helpcontext is:     0###############################

 

Any help for this? :'(

Thanks to everyone.

Link to comment
Share on other sites

You could use the Excel macro recorder to check which statement Excel generates when you insert a PDF.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

@water

The problem is that I did it before. Recording a macro with Excel, the code is this:

Sub Macro1()
'
' Macro1 Macro
'

'
    Range("D4").Select
    ActiveSheet.OLEObjects.Add(Filename:= _
        "C:\Users\svallevazquez\Desktop\TEST\TEST.pdf", Link:= _
        False, DisplayAsIcon:=True, IconFileName:= _
        "C:\Users\svallevazquez\Desktop\TEST\pdf_file.ico", _
        IconIndex:=0, IconLabel:="TEST").Select
    Range("D6").Select
End Sub

I executed this in a Excel and works.

But in AutoIt, doesn't work like:

$oExcel.ActiveSheet.OLEObjects.Add ("C:\Users\svallevazquez\Desktop\TEST\TEST.pdf", False, _
                        True, $"C:\Users\svallevazquez\Desktop\TEST\pdf_file.ico", 0, "Document 1").Select

Anything idea? Or maybe I don't do it correctly? :sweating:

Thanks.

Link to comment
Share on other sites

AutoIt does not Support named Parameters. Make sure that the sequence of Parameters is correct.
https://msdn.microsoft.com/en-us/vba/excel-vba/articles/oleobjects-add-method-excel

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Thank you very much,  @water

To the end, I can fixed the problem using this and Excel macro recorder.

The solution:

$oExcel.ActiveSheet.OLEObjects.Add (Null, "C:\Users\svallevazquez\Desktop\TEST\TEST.pdf", False, _
                        True, "C:\Users\svallevazquez\Desktop\TEST\pdf_file.ico", 0, "Document 1", _
                        350, 60, 40, 40).Select

I hope this can help to someone ^^

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...