Consulting

Results 1 to 5 of 5

Thread: inserting a pdf as ole object into word doc from excel

  1. #1
    VBAX Regular
    Joined
    Aug 2014
    Posts
    18
    Location

    inserting a pdf as ole object into word doc from excel

    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.

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Did you define and create the object objDoc earlier?

  3. #3
    VBAX Regular
    Joined
    Aug 2014
    Posts
    18
    Location
    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!

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    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

  5. #5
    VBAX Regular
    Joined
    Aug 2014
    Posts
    18
    Location
    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •