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:
- The Windows file association for PDF format. It must be associated with a product capable of opening a PDF file, usually Adobe Reader.
- 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.
- 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