Jump to content

Inserting a PDF as object in word


 Share

Recommended Posts

I'm trying to attach a file to a word document (the same way you would if you click insert --> Object --> Create from file --> Display as icon)

I've tried to do:

$oWord=ObjCreate("Word.Application")
    $oWord.Visible = True
    $oDoc=$oWord.Documents.Add('',False,0) ;$WdNewBlankDocument
    $oSelection=$oWord.Selection
    With $oSelection
        .InsertFile($filename,Default,False,False,True)
    endwith

and that works but it doesn't create the icon style (so not really what I'm trying to do.

 

I did some hunting around and I found this:

https://msdn.microsoft.com/en-us/library/office/ff195728.aspx

Which appears to be the correct function but it doesn't seem to work when I try it:

$oWord=ObjCreate("Word.Application")
    $oWord.Visible = True
    $oDoc=$oWord.Documents.Add('',False,0) ;$WdNewBlankDocument
    $oSelection=$oWord.Selection
    With $oSelection
        .OLEObjects.Add(Default,$filename,Default,Default,True,"File")
    endwith

But I get an unknown name error (80020006). Any idea what I'm doing wrong??

 

 

EDIT Upon further investigation it looks like the type is ignored if a file name is given and vice versa. I'm not sure how I need to do this, but everything I'm reading says that this:

            .OLEObjects.Add Filename:={Filename}, Link:=False, DisplayAsIcon:=True, IconFileName="ThisIsATest"

should work, I just don't know how to make that execute in autoit

Edited by Jewtus
Link to comment
Share on other sites

Will have a look at it next week. I'm on vacation right now. 

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

@Danyfirex - I think that embeds a Word doc to an Excel sheet. Excel supports the OLEObjects.Add method.  The question is can you do it in reverse?  I don't see that method on the Word Object Reference Model.  The closest I have found is the Dialogs object that supports inserting the object.  However, I don't think you can bypass the dialog - or at least I have not figured it out just yet :think:.

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Link to comment
Share on other sites

@Jfish, I have seen some example on a few different message boards and they all seem to have the syntax as (the other boards often say excel/word so I assume the function is in both)

.OLEObjects.Add filename:="C:\Filename.pdf" link:=False DisplayAsIcon:=True

Which I can understand, but I don't know how to structure that call in autoit to make it work.

I too found the dialog but that is what I'm trying to avoid. 

The MSDN says the syntax is:

.Add(ClassTypeFileNameLinkDisplayAsIconIconFileNameIconIndexIconLabelLeftTopWidthHeight)

If this is a function just limited to Excel, how would I insert a file as an object? It seems like it should be an option given that you can add word or excel files (not as icons) using vb...

 

Essentially what I'm trying to get my script to do is rebuild a word doc of some data from a database and attach the supporting documents from the network.

Edited by Jewtus
Link to comment
Share on other sites

@Jewtus - It may also be a word function - but I have not found it after reviewing the object model reference and Googling it.  I agree, it should be there.  @Water may be able to pinpoint the correct function when he gets back from vacation.  That said, you may not be able to make that function work if there is no object for it in Word.  Were you able to find it on MSDN for Word?

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Link to comment
Share on other sites

Look:

Local $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")

$objWord = ObjCreate("Word.Application")
$objWord.Visible = True
$objDoc = $objWord.Documents.Add()
$objSelection = $objWord.Selection
Local $sMyPDF="C:\Users\User\Desktop\17598007.pdf"
$objSelection.InlineShapes.AddOLEObject("",$sMyPDF)

Saludos

Edited by Danyfirex
Link to comment
Share on other sites

Look:

Local $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")

$objWord = ObjCreate("Word.Application")
$objWord.Visible = True
$objDoc = $objWord.Documents.Add()
$objSelection = $objWord.Selection
Local $sMyPDF="C:\Users\RAZIEL\Desktop\17598007.pdf"
$objSelection.InlineShapes.AddOLEObject("",$sMyPDF, False, False)

Saludos

Well done!  You found it under InlineShapes!  Here is the MSDN link https://msdn.microsoft.com/EN-US/library/office/ff835835.aspx Good job @Danyfirex :thumbsup:

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Link to comment
Share on other sites

thanks @Jfish 

If you want custom icon and labelicon.

Local $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")

$objWord = ObjCreate("Word.Application")
$objWord.Visible = True
$objDoc = $objWord.Documents.Add()
$objSelection = $objWord.Selection
Local $sMyPDF="C:\Users\User\Desktop\17598007.pdf"

For $i= 1 to 20
$objSelection.InlineShapes.AddOLEObject("",$sMyPDF,false,True,@SystemDir & "\Shell32.dll",190+$i,"Shell32Icon=" & 190+$i)
Next

Saludos

 

Edited by Danyfirex
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

×
×
  • Create New...