setting content of bookmark

P

Petrus Canisius

I use the following code to set text of a bookmark:

Set range = docNew.Bookmarks("PubMonth").range
range.Text = "Dezember"

This works "on the page" but not in header or footer!
I tried also switching to header view and setting text --> not working.

What is the correct solution?

Peter
 
H

Helmut Weber

Hi Peter,

like this:

With ActiveDocument.StoryRanges(wdPrimaryHeaderStory)
.Bookmarks("Mark1").Range.Text = "Dezember"
End With

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 
J

Jean-Guy Marcil

Petrus Canisius was telling us:
Petrus Canisius nous racontait que :
Hi Helmut,

it still won't work!
Error: No such element!

Have you changed
"Mark1"
for
"PubMonth"
in
With ActiveDocument.StoryRanges(wdPrimaryHeaderStory)
.Bookmarks("Mark1").Range.Text = "Dezember"
End With
?

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
P

Petrus Canisius

Naturally!

Jean-Guy Marcil said:
Petrus Canisius was telling us:
Petrus Canisius nous racontait que :


Have you changed
"Mark1"
for
"PubMonth"
in
With ActiveDocument.StoryRanges(wdPrimaryHeaderStory)
.Bookmarks("Mark1").Range.Text = "Dezember"
End With
?

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
H

Helmut Weber

Hi Jean-Guy,
he Peter,

I think both explanations are correct.
Either there is no such storyrange
or there is no such bookmark...
or both are missing ;-)

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 
C

Chuck

Hi Peter

Perhaps it would help if you gave us a bit more code -- for instance, how is
the bookmark "PubMonth" set in the first place, by code or in the template on
which the new document ("docNew"?) is created? At what point in your sub is
the code you provided?

I tried the following code (having manually created a bookmark "PubMonth" in
a several different headers) and it worked fine:

Set range = ActiveDocument.Bookmarks("PubMonth").range
range.Text = "Dezember"

Chuck
 
F

fumei

Setting the range.text will work once, but not twice. Setting bookmark
range.text eliminates (usually) the bookmark itself.

Possible solution A:

Use a formfield instead. On theForms toolbar you can undo do the grey field
shading, so it looks like any other text. Then simply:
ActiveDocument.FormFields("HeaderText").Result = whatever

with whatever being either a string or a string variable.

Solution B
Use the bookmark, but after inserting the text, collapse the selection to
the end of the new text, use Selection.MoveStart Unit:=wdcharacter, Count:=
-Len(textinserted). This moves the selection back the length of the inserted
text. The ActiveDocument.Bookmarks.Add Name:= samenameAs Before,
Range:=Selection.Range

This is in fact what is suggested in Help. The replacement of
Bookmark.Range.Text elimates the bookmark locator, as the long Integer for
Start is replaced by the text. Help itself suggests recreating the bookmark
with the selected text. If you back up the Start (using MoveStart), the same
length as the Len(inserted text), the bookmark can be replaced as many times
as you want, and always fits exactly the length of the inserted text.
 
J

Jean-Guy Marcil

fumei was telling us:
fumei nous racontait que :
Solution B
Use the bookmark, but after inserting the text, collapse the
selection to the end of the new text, use Selection.MoveStart
Unit:=wdcharacter, Count:= -Len(textinserted). This moves the
selection back the length of the inserted text. The
ActiveDocument.Bookmarks.Add Name:= samenameAs Before,
Range:=Selection.Range

Much easier with the Range object.
No need to mess with moving a selection point and moving the user selection.

Dim BookName As String
Dim BookRange As Range

BookName = "My_Test"
Set BookRange = ActiveDocument.Bookmarks(BookName).Range
BookRange.Text = "New text"
ActiveDocument.Bookmarks.Add BookName, BookRange

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
C

Chuck

In Word 2000 the code I posted setting the range worked mulitple times, on
the same document. Set range = ActiveDocument.Bookmarks("PubMonth").range
did not eliminate the bookmark no matter how many times I ran the code.

Chuck
 

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