Results 1 to 9 of 9

Thread: Solved: from excel to word (fill bookmarks)

  1. #1
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location

    Solved: from excel to word (fill bookmarks)

    Hello

    I have an excel file, that will open a word document with some bookmarks and then, try to fill those bookmarks, without delete them.The code is skipping those instructions:


    Set r = doc.Bookmarks(strBM).Range
    doc.Bookmarks.Add Name:=strBM, Range:=strText


    the code to fill the bookmarks:
    [vba]

    Sub FillBM(strBM As String, strText As String)


    Dim doc As Document
    Dim r As Range

    On Error GoTo Err_Handler

    Set doc = Documents.Open(ThisWorkbook.Path & "\tabel")
    With doc
    r.Text = strText
    Set r = doc.Bookmarks(strBM).Range

    doc.Bookmarks.Add Name:=strBM, Range:=strText

    End With

    Err_Handler:
    Exit Sub

    End Sub

    [/vba]

  2. #2
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    [vba]r.Text = strText [/vba] error 91 'object variable or with block variable not set
    [vba]doc.Bookmarks.Add Name:=strBM, Range:=strText[/vba] error 13 type mismatch

  3. #3
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,710
    Location
    [vba]doc.Bookmarks.Add Name:=strBM, Range:=strText [/vba]will indeed give error 13, as Range requires a range object and strText is a string.

    the error 91 comes from the fact r is Dim'd but never Set. Where is it skipping:
    Set r = doc.Bookmarks(strBM).Range

    this is not in the Sub

  4. #4
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    Hello Gerry, thx for watching this:

    I putted like this to skip err 91
    [VBA]
    Sub FillBM(strBM As String, strText As String)

    Dim doc As Document
    Dim r As Range

    Set doc = Documents.Open(ThisWorkbook.Path & "\tabel")
    Set r = doc.Bookmarks(strBM).Range

    With doc
    r.Text = strText
    doc.Bookmarks.Add Name:=strBM, Range:=strText
    End With


    End Sub
    [/VBA]

    but the same error 13(missmatch) like you said range not equale to string.

    what can be done?

  5. #5
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    I putted like this:

    [VBA]
    Sub FillBM(strBM As String, strText As String)
    Dim doc As Document
    Set doc = Documents.Open(ThisWorkbook.Path & "\tabel.doc")

    With doc
    .Bookmarks.Add Name:=strBM, Range:=strText
    End With

    End Sub

    [/VBA]

    now err 4218

  6. #6
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,273
    Location
    Hi white_flag,

    It really does look like you're blundering around in vba without any real idea of what you're doing. For example, I can't see any refernces to the Word application and you've even got lines of code in the wrong order:
            r.Text = strText 
            Set r = doc.Bookmarks(strBM).Range
    You can't write to the 'r' range before you've defined it!!!

    Try something along the lines of:
    [vba]Sub UpdateBookmark(BmkNm As String, NewTxt As String)
    'Note: A reference to the Word object model is required
    Dim wdApp As Word.Application
    Set wdApp = Word.Application
    Dim wdDoc As Word.Document
    Dim BmkRng As Word.Range
    With wdApp
    Set wdDoc = .Documents.Open(ThisWorkbook.Path & "\tabel.doc")
    With wdDoc
    If .Bookmarks.Exists(BmkNm) Then
    Set BmkRng = .Bookmarks(BmkNm).Range
    BmkRng.Text = NewTxt
    .Bookmarks.Add BmkNm, BmkRng
    End If
    End With
    .Visible = True
    End With
    Set BmkRng = Nothing: Set wdDoc = Nothing: Set wdApp = Nothing
    End Sub[/vba]
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  7. #7
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,710
    Location
    I agree it does seem you are stumbling around a bit. First of all, please clarify if you are executing this from Excel. If so, then for sure you need to do as macropod suggests (ensuring you DO have a reference to Word!). If you are executing from Excel then:

    Dim doc As Document

    will be meaningless to VBA. Document is not an Excel term.

  8. #8
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,273
    Location
    Quote Originally Posted by fumei
    First of all, please clarify if you are executing this from Excel. If so, then for sure you need to do as macropod suggests (ensuring you DO have a reference to Word!). If you are executing from Excel then:

    Dim doc As Document

    will be meaningless to VBA. Document is not an Excel term.
    Hi Gerry,

    I took white_flag's initial post at face value:
    I have an excel file, that will open a word document with some bookmarks and then, try to fill those bookmarks, without delete them.
    This seems to be reinforced by the reference to 'ThisWorkbook.Path'.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  9. #9
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    Hello there

    I have an word reference in the sub that will start the fillBmk not in sub fillBmk (based on my VBA small knowledge was enough). anyway, thank you, Paul and Gerry (for pointing me in the good direction).

    have a nice day

Posting Permissions

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