PDA

View Full Version : inserting a pdf as ole object into word doc from excel



wolle271
09-22-2014, 05:55 AM
cheers guys...

mac vba seems to be pretty ****ty huh?
the problem im having is, that im trying to insert a pdf as an ole object at a bookmark. the code is run from an excel macro.
the code im using for this is:


objDoc.Bookmarks("graphic1").Range.InlineShapes.AddOLEObject filename := "Lutz HD:Users:xxxx:Documents:vba:1.pdf"

im always getting this wonderfull error message:
"The server application, source file, or item cannot be found. Make sure the application is properly installed, and that it has not been deleted, moved, or renamed."

The path must be the correct one, as i have it from manually inserting the pdf into a word document while recording a macro.

Kenneth Hobs
09-22-2014, 10:40 AM
Did you define and create the object objDoc earlier?

wolle271
09-22-2014, 11:02 AM
yes, i did.



Dim objWord as Object
Dim objDoc as Object

On Error Resume Next
Set objWord = GetObject(, "Word.Application")
If Err Then
Set objWord = CreateObject("Word.Application")
End If
On Error GoTo 0

Set objDoc = objWord.Documents.Add(some template)


i know i should post the whole code, but its pretty long and not really that readable i guess. but if we wont find any solution tonight, ill post the whole thing tomorrow, after making it more readable. thx in advance!

Kenneth Hobs
09-22-2014, 02:15 PM
I did not iterate the cells or check for failure if the bookmark did not exist. Obviously, you need to change the path strings since you have a mac.


Sub LinkPDFtoWord()'Requires Tools > References > Microsoft Word 11.0 Object Library
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim doc As String
Dim r As Range

Set r = Range("A1")
r.Value = "x:\msword\MyFile.doc"
doc = r.Value

Set r = Range("A2")
r.Value = "c:\t\ken.pdf"

If Dir(doc) = "" Then
MsgBox "Error, file does not exist." & vbLf & doc, vbCritical, "File is Missing"
Exit Sub
End If

On Error GoTo errorHandler

Set wdApp = New Word.Application

With wdApp
'Add makes a copy like from a template even though it may be a DOC file.
Set wdDoc = .Documents.Add(Template:=doc)
'Set wdDoc = .Documents.Open(doc)
.Visible = True
End With

With wdDoc.Bookmarks
'.Item("testbm").Range.InsertAfter Worksheets("Sheet1").Range("A1").Value
'in MSWord, Selection.InlineShapes.AddOLEObject ClassType:="Acrobat.Document.11", _
Filename:="c:\t\ken.pdf", LinkToFile:=False, DisplayAsIcon:=False
.Item("testbm").Range.InlineShapes.AddOLEObject ClassType:="Acrobat.Document.11", _
Filename:=r.Value, LinkToFile:=False, DisplayAsIcon:=False
End With

errorExit:
Set wdDoc = Nothing
Set wdApp = Nothing

Exit Sub

errorHandler:
MsgBox "Unexpected error: " & Err.Number & vbLf & Err.Description
Resume errorExit

End Sub

wolle271
09-23-2014, 03:33 AM
hey kenneth,

inserting the pdf as an image works perfectly fine. mac vba is really confusing me at the moment...



objdoc.Bookmarks("grafik1").Range.InlineShapes.AddPicture filename:= _
"pathToPDF", LinkToFile:=False, _
SaveWithDocument:=Trueúº


i dont know why, but its working! but thx for your help!