Try
Selection.Range.InlineShapes.AddOLEObject , "FilePath\Filename.pdf", False, False
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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
Try
Selection.Range.InlineShapes.AddOLEObject , "FilePath\Filename.pdf", False, False
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:
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
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
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.