none
Problem Inserting pdf into Word Document in VBA RRS feed

  • Question


  • I am using Word 2007.  I have been able to write code that will insert a pdf into a document:

    Selection.InlineShapes.AddOLEObject ClassType:="AcroExch.Document.7", _
            FileName:=(FolderPath & filename) _
        , LinkToFile:=False, DisplayAsIcon:=False

    This works as long as the user has Adobe Reader as their default association for pdf's.  If the user has another app as default, or even Adobe Acrobat, the pdf image does not come in.  Instead a link icon is inserted.  It is a 'live' functional link, but I need the actual pdf image.

    Are there any tweeks to this code that will bring the image in reagrdless of the pdf default?

    Is the "AcroExch.Document.7"  the source of my problem?

     

    Tuesday, November 8, 2011 10:55 PM

Answers

  • As you see, I have the DisplayAsIcon set to false.  I would like to use your comments, but, frankly, don't follow them.  A more fundamental description would be appreciated.


    Hi Doug

    Yes, that parameter would have to be set to True. In that case, by default, you'd pick up the default icon symbol for the application that's serving as the OLE Server - in other words, the result would probably be the same as you're seeing now.

    The additional parameters let you specify the pathname of a *.ico file, the index of the icon you want to display within that file and a label for the icon inside the document. The first of these is of primary relevance, and possibly the second.

    Note that this isn't something I've really used before, I just know how it's supposed to work and played with it :-) More about the file format can be found here: http://en.wikipedia.org/wiki/ICO_(file_format)

    If you can assume that the AdobeReader would be on all systems where your programm will be used, then it should be enough that you retrieve that particular *.ico file. An alternative would be to create your own *.ico file with the image you want to use and reference that.


    Cindy Meister, VSTO/Word MVP
    • Proposed as answer by Shasur Friday, November 11, 2011 7:42 AM
    • Marked as answer by Bruce Song Wednesday, November 16, 2011 7:49 AM
    Friday, November 11, 2011 7:21 AM
    Moderator

All replies

  • Hello,

    Inserting an OLE object implies that you have a program associated with that object type. That program is invoked when you activate such an object (e.g. you can double-click it). Also, that program supplies Word with a picture that you are talking about.

    That is, such a picture won't be created if there's no program associated with objects of the "AcroExch.Document.7" type on the PC where your code is run.

    If the image is a constant thing, I'd check if it is possible to use Range.InsertXML to insert such an object. I also belive it is possible to create such a document via Open XML.


    Regards from Belarus (GMT + 2),

    Andrei Smolin
    Add-in Express Team Leader
    Wednesday, November 9, 2011 6:57 AM
  • Hi Doug

    I agree with Andrei's assessment...

    One additional thought: Do you see the DisplayAsIcon parameter in your code snippet? You can show the document as an icon, rather as whatever graphical symbol the OLE mechanism is picking up from the application in the Registry that's recognized as the "OLE Server" for the file type. And this should allow you to specify the image (the parameters [IconFileName], [IconIndex], [IconLabel]).


    Cindy Meister, VSTO/Word MVP
    Wednesday, November 9, 2011 3:36 PM
    Moderator
  • The pdf's that this app is calling are not "constant".  They are being created by non-tech types, then placed in a file folder for later insertion.  So your Open XML option is not viable.

    Thanx for your efforts.

    Thursday, November 10, 2011 4:05 PM
  • As you see, I have the DisplayAsIcon set to false.  I would like to use your comments, but, frankly, don't follow them.  A more fundamental description would be appreciated.
    Thursday, November 10, 2011 4:09 PM
  • You can start reading at Wikipedia, see http://en.wikipedia.org/wiki/Object_Linking_and_Embedding.


    Regards from Belarus (GMT + 2),

    Andrei Smolin
    Add-in Express Team Leader
    Thursday, November 10, 2011 4:20 PM
  • As you see, I have the DisplayAsIcon set to false.  I would like to use your comments, but, frankly, don't follow them.  A more fundamental description would be appreciated.


    Hi Doug

    Yes, that parameter would have to be set to True. In that case, by default, you'd pick up the default icon symbol for the application that's serving as the OLE Server - in other words, the result would probably be the same as you're seeing now.

    The additional parameters let you specify the pathname of a *.ico file, the index of the icon you want to display within that file and a label for the icon inside the document. The first of these is of primary relevance, and possibly the second.

    Note that this isn't something I've really used before, I just know how it's supposed to work and played with it :-) More about the file format can be found here: http://en.wikipedia.org/wiki/ICO_(file_format)

    If you can assume that the AdobeReader would be on all systems where your programm will be used, then it should be enough that you retrieve that particular *.ico file. An alternative would be to create your own *.ico file with the image you want to use and reference that.


    Cindy Meister, VSTO/Word MVP
    • Proposed as answer by Shasur Friday, November 11, 2011 7:42 AM
    • Marked as answer by Bruce Song Wednesday, November 16, 2011 7:49 AM
    Friday, November 11, 2011 7:21 AM
    Moderator
  • Cindy,

    I'm having pretty much the same problem that Doug is: I want to programmatically insert a PDF file as an image into a Word document.  Your solution above doesn't really seem to solve Doug's problem, or mine: we don't want an icon for the PDF to show up in our docs, we want the PDF file itself to be an image.  In my case, I'm trying to insert images of invoices from my vendors into the invoices I'm sending to my clients, generally non-technical accounting types.  I don't want them to see an icon they can click to open; I want them to see the vendor invoice itself.

    The question, then, is: why does the file show up as an icon when Doug (and I) have explicitly added the parameter "DisplayAsIcon:=False"?

    Thanks for your help,

    Allen

    Tuesday, November 22, 2011 11:39 PM
  • Hi Allen

    OK, I may have misunderstood Doug's request - yours is clear :-) And Andrei's response contains the information, but I can repeat it...

    In order to display the content of a file (Excel or PDF or Powerpoint - all the same!) Word (like other Office apps) uses OLE (Object linking and embedding). In order for this to work, there must be software installed on the machine where the document is opened that can function as an OLE Server.

    Excel, PowerPoint and Word can all function as OLE Servers (as well as OLE clients). But not all software offers this capability. So, if there's no OLE Server software for PDF files registered in Windows, Word can't display the content.

    The only way around this would be for you to create static screenshots of what you want to show and use these as icons. OR insert the graphics file as a hyperlink to the PDF file.


    Cindy Meister, VSTO/Word MVP
    Friday, November 25, 2011 11:18 AM
    Moderator
  • Allen,  Thanx for helping to clarify this one.

    In my case the solutions provided don't help much, for 2 reasons;

    First I'm sure there is an ". . . OLE Server software for PDF files registered in Windows . . ." because the file displays as long as the default pdf viewer for the User is the Stnadard Adobe Reader, but not, if any other reader is the associated program.

    Second, regarding using 'icons' and 'links', in my case, the user will ultimately be PRINTING the resultant document with the pdf inserted in at an appropriate place.

     

    Friday, November 25, 2011 7:51 PM