Multiple bookmarks

J

John

Hi,
I'm using vb.net to merge data to a Word 2000 document.
The document is set up as a letter and I've inserted
bookmarks to merge the data. How do I have multiple
bookmarks to merge the same data. ie I have bookmarks for
client name and address at the top of the document, but I
also want client name further down the letter. I don't
want to have ClientName1, ClientName2 etc. Any ideas?
 
P

Peter Jamieson

It depends on exactly what you are doing. My guess is that you are just
using the bookmarks as placeholders, and the bookmarks are lost when you
insert the data. The simplest change to that approach would probably be to
insert the data, then bookmark the newly inserted data. Then, suppose you
had a bookmark called ClientName, you can use { REF ClientName } fields to
insert further copies of the bookmarked information elsewhere in your
document - you may need to .Execute all the fields in the appropriate range.

Alternatively if your data is always just plain text, you could consider
using Word document variables to contain the texts you want to use, and use
{ DOCVARIABLE } fields to insert the texts. I think that approach is
reasonably safe in Word 2000 - in earlier versions DOCVARIABLE fields in
headers could crash Word.
 
J

John Pearson

Thanks for the help Peter. You are right, I am using the
bookmarks as placeholders and this works fine. A simple
objWordDoc.Bookmarks.Item("CompanyName").Range.InsertAfter
(txtCompanyName.Text)

inserts the company name in the bookmark "CompanyName".
However, if I put a field code further down the document
such as {ref CompanyName} I get a blank. I can only toggle
between blank and the field code! Maybe it's because I'm
not doing an .Update - to what does this method belong?
 
J

John Pearson

ActiveDocument.Fields.Update()

If this is what you mean, it has not had an effect. Still
showing the field code and not the CompanyName!!!
 
C

Cindy M -WordMVP-

Hi John,

No, the problem is that the bookmark doesn't CONTAIN the
string you're inserting. It's more like an insertion point: I

You need this kind of bookmark: [string]

And in order to get it, you need to work with the Range
object a bit differently. Roughly:

bkm = doc.Bookmarks.Item("CompanyName")
If doc.Bookmarks.Exists("CompanyName") Then
rng = bkm.Range
rng.Text = txtcompanyName.Text
doc.Bookmarks.Add(Name:="CompanyName"), Range:=rng)
End If
Thanks for the help Peter. You are right, I am using the
bookmarks as placeholders and this works fine. A simple
objWordDoc.Bookmarks.Item("CompanyName").Range.InsertAfter
(txtCompanyName.Text)

inserts the company name in the bookmark "CompanyName".
However, if I put a field code further down the document
such as {ref CompanyName} I get a blank. I can only toggle
between blank and the field code! Maybe it's because I'm
not doing an .Update - to what does this method belong?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep
30 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any follow
question or reply in the newsgroup and not by e-mail :)
 

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