Embed PDF into Word using VBA

Anonymous
2016-07-23T01:16:18+00:00

Hello,

I am trying to create a template and embed a PDF into the document using VBA. I have created a macro called InsertAttatchment that is called when my UserForm populates the document.

Sub InsertAttachment()

    If (CheckBox1.Value = True) Then

        Selection.GoTo What:=wdGoToBookmark, Name:="BM"

        Selection.InlineShapes.AddOLEObject _

            ClassType:="AcroExch.Document.7", _

            FileName:="C:\Users\JarettS\Desktop\test.pdf", _

            DisplayAsIcon:=True, _

            IconFileName:="C:\Users\JarettS\Desktop\PDFFile_8.ico", _

            IconLabel:="This is a test"

    End If

End Sub

Every time I run the template, the icon for the document shows up but it is a .png instead. If I double click on the icon the PDF does not open.

If I Open the template, I can run the macro and it works, just not when a New document is created from the template.

Any help would be greatly appreciated!

Thanks!

JarettS

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} votes
Answer accepted by question author
  1. Anonymous
    2016-07-23T06:01:57+00:00

    The code used to insert a PDF file as an inline shape will depend on the version of Adobe Reader installed. If you have the latest version, then the code will be more like the following (which includes some error handling).

    There are three issues that need to be addressed:

    1. The Windows file association for PDF format. It must be associated with a product capable of opening a PDF file, usually Adobe Reader.
    2. The location of the file must be accessible to anyone running the macro. The following will find the file if it is on the current user's desktop.
    3. The icon file location may well be different between different operating systems. An incorrect icon file will not usually cause an error, but will instead be subsituted with a plain icon. That will not stop the link being viable.

    You will notice that the latest version of Adobe Reader uses

    ClassType:="AcroExch.Document.DC"

    The original from your macro

    ClassType:="AcroExch.Document.7"

    may also work.

    Sub InsertAttachment()

    Dim oRng As Range

        On Error GoTo err_Handler

        Set oRng = ActiveDocument.Bookmarks("BM").Range

        If oRng.InlineShapes.Count > 0 Then oRng.InlineShapes(1).Delete

        ActiveDocument.InlineShapes.AddOLEObject _

                ClassType:="AcroExch.Document.DC", _

                FileName:=Environ("UserProfile") & "\Desktop\test.pdf", _

                LinkToFile:=False, _

                DisplayAsIcon:=True, _

                IconFileName:="C:\Windows\Installer{AC76BA86-7AD7-1033-7B44-AC0F074E4100}\PDFFile_8.ico", _

                IconIndex:=0, _

                IconLabel:="This is a test", _

                Range:=oRng

        oRng.End = oRng.End + 2

        oRng.Select

        ActiveDocument.Bookmarks.Add Name:="BM", Range:=oRng

    lbl_Exit:

        Set oRng = Nothing

        Exit Sub

    err_Handler:

        If Err.Number = 5941 Then

            MsgBox "The bookmarked location cannot be found"

        Else

            MsgBox "There has been an unhandled error number " & Err.Number & vbCr & Err.Description

        End If

        Err.Clear

        GoTo lbl_Exit

    End Sub

    3 people found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful