Importing a PDF to word doc in code

Mike VaughanEdwards 141 Reputation points
2021-09-30T17:08:33.703+00:00

I have some code that imports a PDF file and inserts it in a Word document. Here is the relevant line:

oDoc.Words.Last.InsertFile(filePath, ConfirmConversions: false);

where oDoc is a Word Document and filePath points to a saved PDF.

With a simple PDF this works fine. However, I have had serious problems with some PDFs, usually scanned documents that show some distortion. The result can be a very mangled version of the image and/or text.

Strangely, if I use Word itself, i.e. not through code, and go Insert > Object > Object > Create from file using the same document and the same PDF the result is much better and acceptable quality.

Is there a better way of doing it in code so that I get the same results as with Word's own menu?

Thanks

Developer technologies Visual Basic for Applications
{count} votes

Accepted answer
  1. Doug Robbins - MVP 896 Reputation points
    2021-10-03T03:47:29.543+00:00

    Try

    Selection.Range.InlineShapes.AddOLEObject , "FilePath\Filename.pdf", False, False

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Mike VaughanEdwards 141 Reputation points
    2021-10-06T09:19:23.86+00:00

    Thanks for that Doug.
    I can use that to create a clickable link to the document if I give the full path as well as the file name. However, I want to offer my users two options:

    1. Include the PDF in the document, the actual PDF not just a link
    2. A clickable link but one that still works if the document and PDF are moved, so long as they stay together.

    I have not been able to achieve 1 with AddOLEObject. Here is my code:

    oDoc.InlineShapes.AddOLEObject(ClassType: null, FileName: attachmentName, LinkToFile: true, DisplayAsIcon: true, IconLabel: "Double click to open", Range:oDoc.Words.Last);

    Am I missing something?

    I can only achieve 2 if I give the full path, which will be wrong if the files are moved. If I just give the file name and the two files are in the same folder I get an error.

    Is what I want possible?

    Thanks

    0 comments No comments

  2. Doug Robbins - MVP 896 Reputation points
    2021-10-06T10:13:36.863+00:00

    To include the PDF in the document, Use

    Dim rng As Range
    Set rng = ActiveDocument.Range
    rng.Collapse wdCollapseEnd
    rng.Select
    Selection.InlineShapes.AddOLEObject , "Attachment Fullname", False, False, , , rng

    The Attachment Fullname must be the path\filename.fileextension of the file that is to be inserted.

    If you want to display an icon in the document that on double clicking will open the pdf in the default application, such as Adobe Acrobat, if the document and the pdf will always be in the same folder, use

    Dim rng As Range
    Set rng = ActiveDocument.Range
    rng.Collapse wdCollapseEnd
    rng.Select
    Selection.InlineShapes.AddOLEObject , ActiveDocument.FullName & "\..\filename.pdf", False, True, , , "Double Click to Open", rng

    0 comments No comments

  3. Mike VaughanEdwards 141 Reputation points
    2021-11-08T13:08:33.027+00:00

    I do apologise for taking so long to respond. Thanks very much for your suggestions. The InlineShapes.AddOLE object worked quite well for simple attachments but behaved oddly for things like large word documents, i.e. just included the first page and opened the whole document as a separate document.

    For a clickable link I went with HTML in the end as the documents I am assembling start with HTML. I just included an <A HREF> section and when the HTML was converted to Word and then PDF the links worked as expected.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.