Consulting

Results 1 to 9 of 9

Thread: How to fill several Bookmarks with Word VBA?

  1. #1

    How to fill several Bookmarks with Word VBA?

    I have a code that is supposed to fill bookmarks with information. The code works but it fails to replace ALL the bookmarks. For example, the documents has 3 areas with a "Your Name Here" bookmark, but when the user fills out the userform, it only fills out one of them and leaves the rest blank.
    How can I make the code fill out any available bookmarks sharing the same bookmark name?
    [VBA]Private Sub cmdOK_Click()
    Application.ScreenUpdating = False
    With ActiveDocument
    If .Bookmarks.Exists("cboYourName") Then WriteToBookmarkRange "cboYourName", cboYourName.Value
    If .Bookmarks.Exists("cboYourPhone") Then WriteToBookmarkRange "cboYourPhone", cboYourPhone.Value
    'and so on
    End With
    Application.ScreenUpdating = True
    Unload Me
    End Sub
    Sub WriteToBookmarkRange(ByRef pBMName As String, pStr As String)
    Dim oRng As Word.Range
    Set oRng = ActiveDocument.Bookmarks(pBMName).Range
    oRng.Text = pStr
    ActiveDocument.Bookmarks.Add pBMName, oRng
    Set oRng = Nothing
    End Sub[/VBA]

    VBA Tags added to code

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,339
    Location
    Michelle,

    A bookmark name is unique. You can't have three bookmarks with the same name. What you can do is create one bookmark for the form users name and then use reference fields at the other locations. You would then add a line of code:

    .Fields.Update

    after the 'and so on line in your existing code.

    For example lets say that you have one bookmark in your document "UserName" you wound insert reference fields at the other two locations that you want the UserName to appear:

    { REF UserName } Note: the field braces are entered using CTRL+F9.
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    "How can I make the code fill out any available bookmarks sharing the same bookmark name?"

    Bookmarks are unique. It is not possible for different bookmarks to share the same name.

    However, it IS possible to have reference fields that map back to bookmarks.

    Where you want a field that has the value of the bookmark cboYourName (poor name BTW):

    press Ctrl-F9
    type "cboYourName"
    move the cursor.

    Now you have a field that takes its value from the bookmark cboYourName.

    When you fill the bookmark with a value (using your Sub WriteToBookmarkRange), simply update the document fields.

    You can do as many of these as you want.
    [vba]
    Option Explicit

    Sub FillBM(strBM As String, strText As String)
    Dim r As Range
    Set r = ActiveDocument.Bookmarks(strBM).Range
    r.Text = strText
    ActiveDocument.Bookmarks.Add Name:=strBM, Range:=r
    End Sub

    Private Sub cmdOK_Click()
    Application.ScreenUpdating = False
    With ActiveDocument
    If .Bookmarks.Exists("cboYourName") Then
    FillBM( "cboYourName", cboYourName.Value )
    End If
    If .Bookmarks.Exists("cboYourPhone") Then
    FillBM("cboYourPhone", cboYourPhone.Value )
    End If
    End With
    ActiveDocument.Fields.Update
    Application.ScreenUpdating = True
    Unload Me
    End Sub
    [/vba]

  4. #4
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Darn, Greg got in there before me.

  5. #5
    Thank You!

  6. #6
    Quote Originally Posted by fumei View Post
    "How can I make the code fill out any available bookmarks sharing the same bookmark name?"

    Bookmarks are unique. It is not possible for different bookmarks to share the same name.

    However, it IS possible to have reference fields that map back to bookmarks.

    Where you want a field that has the value of the bookmark cboYourName (poor name BTW):

    press Ctrl-F9
    type "cboYourName"
    move the cursor.

    Now you have a field that takes its value from the bookmark cboYourName.

    When you fill the bookmark with a value (using your Sub WriteToBookmarkRange), simply update the document fields.

    You can do as many of these as you want.
    [vba]
    Option Explicit

    Sub FillBM(strBM As String, strText As String)
    Dim r As Range
    Set r = ActiveDocument.Bookmarks(strBM).Range
    r.Text = strText
    ActiveDocument.Bookmarks.Add Name:=strBM, Range:=r
    End Sub

    Private Sub cmdOK_Click()
    Application.ScreenUpdating = False
    With ActiveDocument
    If .Bookmarks.Exists("cboYourName") Then
    FillBM( "cboYourName", cboYourName.Value )
    End If
    If .Bookmarks.Exists("cboYourPhone") Then
    FillBM("cboYourPhone", cboYourPhone.Value )
    End If
    End With
    ActiveDocument.Fields.Update
    Application.ScreenUpdating = True
    Unload Me
    End Sub
    [/vba]
    Hate to revive a dead thread, but I tried using ActiveDocument.Fields.Update and it simply just erases the reference bookmark and does not get updated (but the original bookmark still does get updated).

    Any thoughts?

    [vba]
    Private Sub CommandButton1_Click()
    Dim City As String
    Dim CityLoc As Range


    Set CityLoc = ActiveDocument.Bookmarks("test").Range


    City = Me.TextBox1.Value

    CityLoc.Text = City

    ActiveDocument.Fields.Update


    End Sub

    [/vba]

  7. #7
    You didn't read the part about using the FillBM function? Use the following FillBM function which doesn't throw an error if the bookmark is missing.
    You shouldn't need to update the fields unless there are cross references toi the bookmark.

    Private Sub CommandButton1_Click()
        FillBM "Test", Me.TextBox1.Text
    End Sub
    
    Private Sub FillBM(strBMName As String, strValue As String)
    'Graham Mayor
    Dim oRng As Range
        With ActiveDocument
            On Error GoTo lbl_Exit
            Set oRng = .Bookmarks(strBMName).Range
            oRng.Text = strValue
            oRng.Bookmarks.Add strBMName
        End With
    lbl_Exit:
        Set oRng = Nothing
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  8. #8
    Hi,

    and Thank you to everyone who has supplied information here.

    How can I update the ref fields automaically once the form has been populated ?

    Thanks

    Philippe

  9. #9
    Yoiu could call the UpdateAll macro used as an example at http://www.gmayor.com/installing_macro.htm e.g.

    Private Sub CommandButton1_Click()
          FillBM "Test", Me.TextBox1.Text 
          UpdateAll
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Posting Permissions

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