svallevazquez

Inserting a PDF file as object in Excel

5 posts in this topic

#1 ·  Posted

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.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#3 ·  Posted

@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.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#5 ·  Posted

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 ^^

Share this post


Link to post
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