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

    • Iznogoud
      By Iznogoud
      Hi,
      I am trying to find more information on generating PDF files from AutoIT and found the UDF topic 
      Also found some info on creating .FDF file and use PDFTK for merging, but i can't find info about a custom based layout.
      What i trying to find out is, if it is possible to generate a variable layout. For an example i created a script which is based out 20 fields and every field can contain information. If one of those fields are not filled, it shouldn't be printed on the PDF either.
      To make it more difficult, the PDF should contain a heading, the first 10 fields and then a horizontal line across the PDF and then a new heading and then show the last 10 fields.
      But only the fields which are filled.
      Is this possible?
    • Neonovaz
      By Neonovaz
      Hello
       
      Is there anyway to store word documents in Autoit GUI? For example I have a instruction sheet that I want to bundle up with the exe.

      So a user simply clicks the icon and the stored document will launch  (Something like how you can add objects like excel sheets in word documents )

      (I Know we can launch word files from script directory)

       
    • Gowrisankar
      By Gowrisankar
      Hello everyone,
      I'm working on a task where, a PDF file is opened (in IE browser) when I click a link in a website.
      I have to read the first page of the PDF to find particular strings. Can you please share some ideas?
    • mLipok
      By mLipok
      Here:
      https://github.com/nachbar/TRichViewToPdfUsingDebenu/blob/master/Unit1.cpp
      I found a code in C++ for file format conversion from RTF to PDF with using Debenu QuickPDF.
      I know how to use Debenu QuickPDF in AutoIt .
      My question is about RTF part of this code:
       
      HDC hdcNew = debenu->GetCanvasDC( RTFPRINTINGDOTSPERINCH * RTFPAGEWIDTHININCHES, RTFPRINTINGDOTSPERINCH * RTFPAGEHEIGHTININCHES); canvas = new TCanvas; canvas->Handle = hdcNew; RVReportHelper1->DrawPage( PageCounter, canvas, true, RTFPRINTINGDOTSPERINCH * RTFPAGEHEIGHTININCHES); // LastPageHeight);  
      First there is hdcNew declaration , and this is not the problem.
      My problem is in converting the following code snippet, to AutoIt
      canvas = new TCanvas; canvas->Handle = hdcNew; RVReportHelper1->DrawPage( PageCounter, canvas, true, RTFPRINTINGDOTSPERINCH * RTFPAGEHEIGHTININCHES); // LastPageHeight);
      If you remember, I created RTFPrinter sometime ago. But it was some time ago , and created by trial and error, rather than in-depth analysis.
      Also, it was just a modification of another script, not my own work from scratch.
      So now I'm looking for help how to adapt this code snippet to AutoIt.
      Any tips ?
       
      Regards,
      mLIpok
       
    • Kiran_L
      By Kiran_L
      Hi guys,
       
      I am trying to read a pdf file with unstructured data. I dontot know how to handle pdf activities in AutoIt,
      Can you help me with any UDF to open the PDF and read the doc.
       
      Thanks for your time.