bookmark macro question

J

jb914

I have a VBA addin for word that will fill in multiple bookmarks in a
document. So, if i have bookmarks in a document bkName, bkName1, bkName2,
ect.......throughout the document the program will fill in the name in the
right spot. it simply looks for a new

My users are novices. i am trying to build them a toolbar to simplify the
process.

i have the toolbar built which includes a bunch of buttons using code like:

Sub bkName()
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="bkName"
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
End Sub

I know the program uses the following code to loop thru filling in the
bookmarks

Private Sub SetBookmarkText(doc As Document, ByVal Name As String, ByVal
Value As String)
On Error Resume Next
With doc
.Bookmarks(Name).Range.Text = Value
Dim i As Integer
i = 1
Do While .Bookmarks.Exists(Name & CStr(i))
.Bookmarks(Name & CStr(i)).Range.Text = Value
i = i + 1
Loop
End With
End Sub

How would i modify the bookmark creation code to check for an existing
bookmark and LOOP thru to create them? So, if bkName is already in the
document it will insert a bookmark bkName1 and if bkName1 is already in it
will do bkName2

any direction would be appreciated.

thanks

JB
 
R

Russ

Joe,

See change to code below.
I have a VBA addin for word that will fill in multiple bookmarks in a
document. So, if i have bookmarks in a document bkName, bkName1, bkName2,
ect.......throughout the document the program will fill in the name in the
right spot. it simply looks for a new

My users are novices. i am trying to build them a toolbar to simplify the
process.

i have the toolbar built which includes a bunch of buttons using code like:

Sub bkName()

Dim n as Long
n = ActiveDocument.Bookmarks.Count + 1
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="bkName"
Change above line to:
.Add Range:=Selection.Range, Name:="bkName" & Cstr(n)
 
J

Jezebel

Your code would be much simpler if you used document properties and
DocProperty fields.
 
J

jb914

Hi Russ,
Thanks for the reply. This works part way. However, i have multiple
buttons for multiple bookmarks. When i tested it the first bookmark it
worked ok. But, when i put the code into another bookmark it picks up the
count where it left off on the first bookmark.

I need it to start fresh for each bookmark. Otherwise my "Fill in the
blanks" code will not work. I'm using this code you modified to create
templates. So, i need it to let me put as many bookmarks into a document as
a user needs. It needs to allow the user to put the same information into
multiple places in the template. Since Word does not allow a bookmark to be
in more than one place in need to put in the bkName, bkName1, bkName2,
ect........

My other code that "fills in the bookmarks" looks for bkName and fills it in
then loops to see if there is the same bkName1 if it's there it fills it and
checks for bkName2 and keeps looping in sequence until there are no more
bkName(number)

It then moves on to the next bookmark and follows the same loop. Does this
make sense? How could i modify the bookmark creation button code to put the
bookmarks into the document in sequence?

Here is the latest code:


Sub bkName()
'
Dim n As Long
n = ActiveDocument.Bookmarks.Count + 1

With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="bkPatientName" & CStr(n)
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
End Sub

Thanks for your help!

JB
 
R

Russ

Jb914,

I agree with Jezebel that DocProperties might be better, if you just want
the user to enter the unique data once and have it automatically show up by
code where it is needed elsewhere in the document. See this link for... Word
offers several solutions to the question, "How do I enter data in one place
in a document and have it repeated in other places." (see #5)

http://gregmaxey.mvps.org/Repeating_Data.htm

See customdocumentproperties in VBA help for how to do it programmatically.
 
J

jb914

Thanks Russ/Jezebel,

the user is not entering the data into the document. The data is pulled
from a DB and populated into the bookmarks in the document. We may be
filling in a name into the document in many places. i just wanted to give
them a button that say "name" and have the push the button to allow them to
insert a bookmark with the appropriate name. when they edit their
templates. the only problem is i need to have the code check to see if a
bookmark name has been used already. if so, it will simply instert a
"name1" if that has been used it inserts a "name2" ect.

i wanted the code, so, i could create buttons allowing the user to create
new templates.

is there a way to just have it push the bookmark to a REF field? then have
other REF fields thru the document? is that what you are suggesting?

thanks

JB
 
R

Russ

JB,
Thanks Russ/Jezebel,

the user is not entering the data into the document. The data is pulled
from a DB and populated into the bookmarks in the document. We may be
filling in a name into the document in many places. i just wanted to give
them a button that say "name" and have the push the button to allow them to
insert a bookmark with the appropriate name. when they edit their
templates. the only problem is i need to have the code check to see if a
bookmark name has been used already. if so, it will simply instert a
"name1" if that has been used it inserts a "name2" ect.

i wanted the code, so, i could create buttons allowing the user to create
new templates.

is there a way to just have it push the bookmark to a REF field? then have
other REF fields thru the document? is that what you are suggesting?
Taking bookmarks out of it for now, you could put each of captured DB data
into its own customdocumentsproperty, then you could have a button that
could repeatedly inject a 'ref field' into the document to that
aforementioned customdocumentproperty just like the article I suggested
reading. Otherwise using bookmarks, you'll need to give each 'type' a unique
'name' and then append a number in order to 'repeat a bookmark'. For example
name1,name2,...and/or street1,street2,... Or maybe inject a bookmark once
and then inject 'ref fields' to the unique bookmark.text.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top