Jewtus

Inserting a PDF as object in word

12 posts in this topic

#1 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites



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

1 person likes this

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

maybe this is what you need.

 

Saludos

Share this post


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

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

@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

Share this post


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

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

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)

Saludos

Edited by Danyfirex
1 person likes this

Share this post


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

Share this post


Link to post
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\RAZIEL\Desktop\17598007.pdf"

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

Saludos

 

1 person likes this

Share this post


Link to post
Share on other sites

PERFECT!!! Thanks!

Share this post


Link to post
Share on other sites

You're wellcome.

 

Saludos

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

  • Similar Content

    • Jury
      By Jury
      I've failed to find an example of _Word_DocFindReplace which searches for formatted text (I'm looking for stand alone paragraph marks that are formatted other than normal i.e. Bold Italic, Underlined). 
      The reason being that when converting a Word document to html one of the main problems in the results is that a stand alone paragraph mark is converted to an html space that retains the formatting ...>&nbsp;<... thus showing up as a underline _  in a browser when it should be blank.  I've played around with the script and got it to at least un-bold  the first paragraph mark regardless if it was bold or not but I'd like to clear all formatting from any stand alone paragraph marks in the whole document.  Below is what I've done so far (not much more than in the help file I'm afraid) .  Way down at the bottom of the _Word_DocFindReplace  help  text is this parameter but without any examples to be found :
      $bFormat   [optional] True to have the find operation locate formatting in addition to or instead of the find text (default = False) #include <MsgBoxConstants.au3> #include <Word.au3> $processing = @MyDocumentsDir & '\AutoIt_code\getter\processing\' Global $oWord = _Word_Create() Global $sTestfile = $processing & "Testing.docx" ConsoleWrite($sTestfile & @CRLF) Global $oDoc = _Word_DocOpen($oWord, $sTestfile) If @error Then Exit MsgBox($MB_SYSTEMMODAL, "ERROR", "Error opening file = '" & $sTestfile & "'" & @CRLF & "@error = " & @error & ", @extended = " & @extended) $oRangeFound = _Word_DocFind($oDoc, "^p", Default, Default) If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFind Example", _ "Error locating paragraph control character in the document." & @CRLF & "@error = " & @error & ", @extended = " & @extended) $oRangeFound.Bold = False If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFind Example", _ "Error inserting text after the paragraph control character in the document." & @CRLF & "@error = " & @error & _ ", @extended = " & @extended) MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFind Example", "Paragraph control character successfully replaced." & @CRLF & _ "Text inserted in paragraph 2.")  
    • Mag91
      By Mag91
      Hey Community,
      cause im too new in the Auto it world i will try it with the your help. hopefully.
      I woud like to know how i can handle my Problem.
      ----
      I have a Excel Data with 362 random numbers.
      For Example:
      1166642335374 1172899897343
      .....
      this numbers are a part of the filepath ...example
      D:\Projekte\1166_64233_5374
      as u can see its the first number of the Excel data. After the first 4 numbers it shoud make a "_" than another 5 "_"
      This is my first question. How can i handle this to make it Shell execute.
       
      --------
      Second question:
      If i am in the path.
      For Example:
      D:\Projekte\1166_64233_5374
      the code shoud search for specific PDF Files.
      They are named like: 0050569E364B1ED79B900F73E62660EC.pdf
      the first 15 letters are always the same
      0050569E364B1ED
      when he found this data he has to copy it on a Folder on the Desktop.
      (There can also be 2 or 3 pdfs in one Folder with this letters)
      ----
      Please give me some help :-)
       
       
       
       
       
       
    • Mag91
      By Mag91
      Hey Everybody,
      as you know im on a very low autoit-level.
      My question is: How can i read all PDFs from a Folder wich is open and copy them to a Folder on a Desktop.
       
      The Folder wich contains the PDFs is variable Z:\Projektls\"*"*"*EVERYTIME ANOTHER ENDING"*"*"*"*"
      There can be 1 PDF or even 15 PDFs.
      i tried it with _FileListToArray and _FileCopy but i Need some help to understand this language
       
      THANKS!
       
    • Skeletor
      By Skeletor
      Hi Guys,
      I've been reading this post ...
      When I came accross the examples, non of them had what I was looking for.
      I basically want to "snapshot" my GUI's multiple tabs and send them into the pdf.
      A little nudge from you guys would be great.
      Im really stuck with this one, therefore I have no code.
      Lets discuss or point me in a right direction... thanks alot
       

    • FrancescoDiMuro
      By FrancescoDiMuro
      Good morning everyone
      I am working on a little script, which takes some data from a SQLite DB and should create a sort of report, inserting rows in a Word Document... I arrived at the point of:
      _Word_DocTableWrite() and, I don't know how to set the range parameter? What does that specify? 
      Thanks a lot for the help
      EDIT:
      Managed to write a table in the Word document, but now I get an error when I save the document with _Word_DocSaveAs(), with error 2.
      Which are possible causes? Thanks a lot, again
      EDIT 2:
      ... And, how can I set a border to the table? Maybe, with a sort of auto-formatting for text ( larger is the text, larger is the height/width of the table's cell ).
      Thanks  
      EDIT 3 ( bug ):
      Including the parameter $WdSaveChanges in the function _Word_DocSaveAs(), a save dialog box appears, and it should not do it, as it's written in the MSDN documentation:
      wdSaveChanges -1 Save pending changes automatically without prompting the user. Thanks again for everyone will answer to me