Write data in Bookmarks

K

Ken

Hi,

Is it possible to write the data between two brackets
["data"] of the bookmark. I know when I use "InsertAfter"
method that the data are always followed by the bookmark
but I would like to insert the data in the brackets of
the the bookmark. Thanks!

Ken
 
M

macropod

Hi Ken,

The most reliable way to do this is to overwrite & recreate the bookmark.

The following macro replaces a specified bookmark with a new text string.

Sub UpdateBookMark()
Dim strNewText As String
Dim intBmkLength As Integer, rngTemp As Range
BmkNm = "Bookmark Name"
strNewText = "New Bookmark Text"
If ActiveDocument.Bookmarks.Exists(BmkNm) Then
Set rngTemp = ActiveDocument.Bookmarks(BmkNm).Range
rngTemp.Text = strNewText
ActiveDocuments.Bookmarks.Add BmkNm, rngTemp
Else
MsgBox "Bookmark " & BmkNm & " not found."
End If
Set rngTemp = Nothing
End Sub

Cheers


Ken said:
Hi,

Is it possible to write the data between two brackets
["data"] of the bookmark. I know when I use "InsertAfter"
method that the data are always followed by the bookmark
but I would like to insert the data in the brackets of
the the bookmark. Thanks!

Ken
 
G

Guest

Hi,

Thank you! What I am trying to achieve is I would like to
replace the text string followed by the specified
bookmark with a new text string. That's why I am thinking
if the text string is between the brackets. That would be
perfect to replace it.

Ken
-----Original Message-----
Hi Ken,

The most reliable way to do this is to overwrite & recreate the bookmark.

The following macro replaces a specified bookmark with a new text string.

Sub UpdateBookMark()
Dim strNewText As String
Dim intBmkLength As Integer, rngTemp As Range
BmkNm = "Bookmark Name"
strNewText = "New Bookmark Text"
If ActiveDocument.Bookmarks.Exists(BmkNm) Then
Set rngTemp = ActiveDocument.Bookmarks(BmkNm).Range
rngTemp.Text = strNewText
ActiveDocuments.Bookmarks.Add BmkNm, rngTemp
Else
MsgBox "Bookmark " & BmkNm & " not found."
End If
Set rngTemp = Nothing
End Sub

Cheers


Hi,

Is it possible to write the data between two brackets
["data"] of the bookmark. I know when I use "InsertAfter"
method that the data are always followed by the bookmark
but I would like to insert the data in the brackets of
the the bookmark. Thanks!

Ken


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.593 / Virus Database: 376 - Release Date: 20/02/2004


.
 
M

macropod

I think that's quite different to what you originally described, and creates
a number of challenges, including:
a) find the bookmark
b) search backwards for the closing bracket & select it, then
b) continue extending the selection backwards while searching for the
opening bracket.
d) delete the selected text & brackets and insert the replacement text (plus
new brackets?).

Any reason you can't just bookmark the target text and use the suggested
method?

Cheers


Hi,

Thank you! What I am trying to achieve is I would like to
replace the text string followed by the specified
bookmark with a new text string. That's why I am thinking
if the text string is between the brackets. That would be
perfect to replace it.

Ken
-----Original Message-----
Hi Ken,

The most reliable way to do this is to overwrite & recreate the bookmark.

The following macro replaces a specified bookmark with a new text string.

Sub UpdateBookMark()
Dim strNewText As String
Dim intBmkLength As Integer, rngTemp As Range
BmkNm = "Bookmark Name"
strNewText = "New Bookmark Text"
If ActiveDocument.Bookmarks.Exists(BmkNm) Then
Set rngTemp = ActiveDocument.Bookmarks(BmkNm).Range
rngTemp.Text = strNewText
ActiveDocuments.Bookmarks.Add BmkNm, rngTemp
Else
MsgBox "Bookmark " & BmkNm & " not found."
End If
Set rngTemp = Nothing
End Sub

Cheers


Hi,

Is it possible to write the data between two brackets
["data"] of the bookmark. I know when I use "InsertAfter"
method that the data are always followed by the bookmark
but I would like to insert the data in the brackets of
the the bookmark. Thanks!

Ken


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.593 / Virus Database: 376 - Release Date: 20/02/2004


.
 
A

Alan Pullman

Hi Ken,

I have recently used this with no problems. I use a sub as I have to d
it may times in my template:

updatebookmark "DonorName", TextBox1

Sub updatebookmark(bookmarktoupdate As String, texttouse As String)
Dim bmrange As Range
Set bmrange = ActiveDocument.Bookmarks(bookmarktoupdate).Range
bmrange.Text = texttouse
ActiveDocument.Bookmarks.Add bookmarktoupdate, bmrange
End Su
 
M

macropod

Hi Christian,

Your solution seems to do much the same as the one I originally posted, but
Ken's response indicated that this isn't what's wanted. Ken's reply says the
task is to "replace the text string followed by the specified bookmark with
a new text string". This suggests to me that it isn't the bookmarked text
that is to be replaced(contrary to what was originally posted), but some
text "between the brackets" preceding the bookmark. Hence my reply about the
difficulty of doing that.

Cheers
 
K

Ken

Hi,

Thank you! I think I should redescribe my situation. I
have a data file and I have one word document. I usually
write visual basic code in the visual basic editor. When
the document first opens, it will read the data file by
visual basic code. The data file has two fields, one is
bookmark and the other one is text string that we want to
insert it in the position of the bookmark in word
document. After the word document opens, the text string
will be in the position of the bookmark. Now, if I have a
new data file, I would like to rerun the word document
again. This time, what I try to achieve is I hope the
text string I insert in the first time will be deleted
and the new text string from the data file will be
showing in the same word document. Thank you so much for
all the discussion. I will try all of them to see which
one is working.

That's why I was thinking about inserting the text string
between the bracket of the bookmark because I find out
every text between in the bracket of the the bookmark
will be deleted after I open the document and run the vba
code. That was my first thought. Maybe you have better
idea. Thank you!

Ken
-----Original Message-----
I think that's quite different to what you originally described, and creates
a number of challenges, including:
a) find the bookmark
b) search backwards for the closing bracket & select it, then
b) continue extending the selection backwards while searching for the
opening bracket.
d) delete the selected text & brackets and insert the replacement text (plus
new brackets?).

Any reason you can't just bookmark the target text and use the suggested
method?

Cheers


Hi,

Thank you! What I am trying to achieve is I would like to
replace the text string followed by the specified
bookmark with a new text string. That's why I am thinking
if the text string is between the brackets. That would be
perfect to replace it.

Ken
-----Original Message-----
Hi Ken,

The most reliable way to do this is to overwrite & recreate the bookmark.

The following macro replaces a specified bookmark
with a
new text string.
Sub UpdateBookMark()
Dim strNewText As String
Dim intBmkLength As Integer, rngTemp As Range
BmkNm = "Bookmark Name"
strNewText = "New Bookmark Text"
If ActiveDocument.Bookmarks.Exists(BmkNm) Then
Set rngTemp = ActiveDocument.Bookmarks(BmkNm).Range
rngTemp.Text = strNewText
ActiveDocuments.Bookmarks.Add BmkNm, rngTemp
Else
MsgBox "Bookmark " & BmkNm & " not found."
End If
Set rngTemp = Nothing
End Sub

Cheers


Hi,

Is it possible to write the data between two brackets
["data"] of the bookmark. I know when I use "InsertAfter"
method that the data are always followed by the bookmark
but I would like to insert the data in the brackets of
the the bookmark. Thanks!

Ken


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.593 / Virus Database: 376 - Release Date: 20/02/2004


.


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.593 / Virus Database: 376 - Release Date: 20/02/2004


.
 

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