PDA

View Full Version : How to fill several Bookmarks with Word VBA?



Michelle S.
12-06-2010, 10:07 AM
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?
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 Tags added to code

gmaxey
12-06-2010, 10:39 AM
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.

fumei
12-06-2010, 10:45 AM
"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.

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

fumei
12-06-2010, 10:46 AM
Darn, Greg got in there before me.

Michelle S.
12-09-2010, 10:11 AM
Thank You!

pyccanful
08-17-2015, 10:59 AM
"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.

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


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?


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

gmayor
08-18-2015, 01:48 AM
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

mezcalbean
10-07-2015, 07:50 PM
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

gmayor
10-07-2015, 08:59 PM
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