Creating bookmarks in inserted text.

S

Sophia

Hello,

If I have an item such as:

"[PetOwner] would like to have [PetsName] returned by [DayOfTheWeek]"
in a document.

What is the code to create a set of "enclosing" bookmarks B1, B2, B3
that can be used to replace, for example [PetOwner], [PetsName] and
[DayOfTheWeek] with a variables?


TIA
 
P

Peter Hewett

Hi Sophia

I'm not sure why you're trying to create the boookmarks programatically (I
may have missed your point here!). Normally you create the boilerplate text
in your template and manually add bookmarks where you want to insert variable
text (say entered through a UserForm) into your document.

If this is what you're trying to do check out the following links:
http://word.mvps.org/FAQs/Userforms/index.htm
http://word.mvps.org/FAQs/Userforms/CreateAUserForm.htm

the second link also contains code on how to get text entered into a UserForm
control into the bookmark in your document.

You can of course create bookmarks programatically, but the trick of course
is knowing where in the document you want to create them!

If you need more info, or I missed the mark please post again.

HTH + Cheers - Peter
 
S

Sophia

Peter,

Sorry to have given you only part of the picture. We actually have
*hundreds* of these text entries now ... all in the format I described
(with the square brackets as shown) and more are coming to us from
other sources, which may or may not have originated in Word.

I was thinking that rather than to go back and edit each one of those
hundreds (by adding Word bookmarks), and accomodate the new arrivals,
we might be able to plug them as is, and from the insertion point,
locate the enclosing brackets programmatically, and add enclosing
bookmarks between the pairs of brackets.

I've already put together an array that provides the starting and
ending positions of the brackets in the insertion ... so I know where
they are with reference to the insertionpoint. What I don't know, and
have been unable to discover, is the code for "between this starting
bracket and that ending bracket ... create and name a bookmark that
will be used later to accept the data in the fields in the form."

Could you help me with that?

TIA


Hi Sophia

I'm not sure why you're trying to create the boookmarks programatically (I
may have missed your point here!). Normally you create the boilerplate text
in your template and manually add bookmarks where you want to insert variable
text (say entered through a UserForm) into your document.

If this is what you're trying to do check out the following links:
http://word.mvps.org/FAQs/Userforms/index.htm
http://word.mvps.org/FAQs/Userforms/CreateAUserForm.htm

the second link also contains code on how to get text entered into a UserForm
control into the bookmark in your document.

You can of course create bookmarks programatically, but the trick of course
is knowing where in the document you want to create them!

If you need more info, or I missed the mark please post again.

HTH + Cheers - Peter


Hello,

If I have an item such as:

"[PetOwner] would like to have [PetsName] returned by [DayOfTheWeek]"
in a document.

What is the code to create a set of "enclosing" bookmarks B1, B2, B3
that can be used to replace, for example [PetOwner], [PetsName] and
[DayOfTheWeek] with a variables?


TIA
 
P

Peter Hewett

Hi Sophia

I'm trying to develop a better picture of what you're working with as a
whole so that I don't push you in the wrong direction.

Are these hundreds on entries in the same or different documents?
Where is the data comming from that you want to insert between the
enclosing brackets.
Do you need to work with all of the bracketed items in the document or just
a subset, if a subset how is that subset determined?

I don't understand the correlation between the items "[....]" and the
values they need to be replaced with. E.g.: How do you recognise data that
should go in the "[PetOwner]" area?

I can help, but I need it clear in my head first!

Cheers - Peter


Peter,

Sorry to have given you only part of the picture. We actually have
*hundreds* of these text entries now ... all in the format I described
(with the square brackets as shown) and more are coming to us from
other sources, which may or may not have originated in Word.

I was thinking that rather than to go back and edit each one of those
hundreds (by adding Word bookmarks), and accomodate the new arrivals,
we might be able to plug them as is, and from the insertion point,
locate the enclosing brackets programmatically, and add enclosing
bookmarks between the pairs of brackets.

I've already put together an array that provides the starting and
ending positions of the brackets in the insertion ... so I know where
they are with reference to the insertionpoint. What I don't know, and
have been unable to discover, is the code for "between this starting
bracket and that ending bracket ... create and name a bookmark that
will be used later to accept the data in the fields in the form."

Could you help me with that?

TIA


Hi Sophia

I'm not sure why you're trying to create the boookmarks programatically
(I may have missed your point here!). Normally you create the
boilerplate text in your template and manually add bookmarks where you
want to insert variable text (say entered through a UserForm) into your
document.

If this is what you're trying to do check out the following links:
http://word.mvps.org/FAQs/Userforms/index.htm
http://word.mvps.org/FAQs/Userforms/CreateAUserForm.htm

the second link also contains code on how to get text entered into a
UserForm control into the bookmark in your document.

You can of course create bookmarks programatically, but the trick of
course is knowing where in the document you want to create them!

If you need more info, or I missed the mark please post again.

HTH + Cheers - Peter


Hello,

If I have an item such as:

"[PetOwner] would like to have [PetsName] returned by [DayOfTheWeek]"
in a document.

What is the code to create a set of "enclosing" bookmarks B1, B2, B3
that can be used to replace, for example [PetOwner], [PetsName] and
[DayOfTheWeek] with a variables?


TIA
 
W

Word Heretic

G'day Sophia <[email protected]>,

myrange.bookmarks.add aUniqueName, myrange

aUniqueName is just aUniqueStem appended with an incrementing number.

Before you start you just check for the existance of a bookmark with
the proposed start UniqueName and increment until you dont find one

While ActiveDocument.Bookmarks.Exists(aUniqueStem & format(k))
k=k+1
wend


Steve Hudson - Word Heretic Sydney Australia
Tricky stuff with Word or words

Email: steve at wordheretic.com


Sophia reckoned:
Peter,

Sorry to have given you only part of the picture. We actually have
*hundreds* of these text entries now ... all in the format I described
(with the square brackets as shown) and more are coming to us from
other sources, which may or may not have originated in Word.

I was thinking that rather than to go back and edit each one of those
hundreds (by adding Word bookmarks), and accomodate the new arrivals,
we might be able to plug them as is, and from the insertion point,
locate the enclosing brackets programmatically, and add enclosing
bookmarks between the pairs of brackets.

I've already put together an array that provides the starting and
ending positions of the brackets in the insertion ... so I know where
they are with reference to the insertionpoint. What I don't know, and
have been unable to discover, is the code for "between this starting
bracket and that ending bracket ... create and name a bookmark that
will be used later to accept the data in the fields in the form."

Could you help me with that?

TIA


Hi Sophia

I'm not sure why you're trying to create the boookmarks programatically (I
may have missed your point here!). Normally you create the boilerplate text
in your template and manually add bookmarks where you want to insert variable
text (say entered through a UserForm) into your document.

If this is what you're trying to do check out the following links:
http://word.mvps.org/FAQs/Userforms/index.htm
http://word.mvps.org/FAQs/Userforms/CreateAUserForm.htm

the second link also contains code on how to get text entered into a UserForm
control into the bookmark in your document.

You can of course create bookmarks programatically, but the trick of course
is knowing where in the document you want to create them!

If you need more info, or I missed the mark please post again.

HTH + Cheers - Peter


Hello,

If I have an item such as:

"[PetOwner] would like to have [PetsName] returned by [DayOfTheWeek]"
in a document.

What is the code to create a set of "enclosing" bookmarks B1, B2, B3
that can be used to replace, for example [PetOwner], [PetsName] and
[DayOfTheWeek] with a variables?


TIA
 
S

Sophia

Thank you Steve and Peter,

The Text entries are listed sequentially in the first column of a
table, which table is part of a document which is opened by my
userform using

Set sourcedoc = Documents.Open(FileName:="C:\My
Documents\MacroName.doc")

A second column of the table provides a key "MacroName", which is
searched to access the appropriate row in the table, where something
like "[PetOwner] would like to have [PetsName] returned by
[DayOfTheWeek]" is found in the second "InsertText" column , and
eventually used as an insertion into the ActiveDocument at the
insertion point by a UserForm that is well under control at this
point.

Based on your previous replies, and after thinking about it over the
weekend, I think what I really need is some code to search the table
row by row, and in each instance where there is an
"[anythingwhatever]" in the InsertText cell, to set a range therein to
place a hidden "enclosed" bookmark that embraces the entire
"[anythingwhatever]" (while leaving the "[anythingwhatever]" in place
and visible) and then continuing in the same InsertText cell,
doing the same thing until it gets to the end of the text in
InsertText cell for that row. If I had that, I could go forward. I
understand the need to check for a pre-existing bookmark with the same
name before assigning a name.

So, something of the order:

For each

"[" in InsertText followed by a complementary "]" ...
set an enclosing bookmark named "BookMarkName"
(previously checked for to prevent duplicates)
that encloses every character from the "[" to the "]" and

Then

go on doing that until to the end of that InsertText cell.

Next RowInTheTable


I think I can manage what goes on outside this routine ... and to
describe it further would, I think, add some unnecessary confusion to
the picture.

(At least, I think it would ... otherwise I'll be coming to the door
again, with my outstretched hand. Please bear with me and get me to
this point.)

TIA

Sophia






G'day Sophia <[email protected]>,

myrange.bookmarks.add aUniqueName, myrange

aUniqueName is just aUniqueStem appended with an incrementing number.

Before you start you just check for the existance of a bookmark with
the proposed start UniqueName and increment until you dont find one

While ActiveDocument.Bookmarks.Exists(aUniqueStem & format(k))
k=k+1
wend


Steve Hudson - Word Heretic Sydney Australia
Tricky stuff with Word or words

Email: steve at wordheretic.com


Sophia reckoned:
Peter,

Sorry to have given you only part of the picture. We actually have
*hundreds* of these text entries now ... all in the format I described
(with the square brackets as shown) and more are coming to us from
other sources, which may or may not have originated in Word.

I was thinking that rather than to go back and edit each one of those
hundreds (by adding Word bookmarks), and accomodate the new arrivals,
we might be able to plug them as is, and from the insertion point,
locate the enclosing brackets programmatically, and add enclosing
bookmarks between the pairs of brackets.

I've already put together an array that provides the starting and
ending positions of the brackets in the insertion ... so I know where
they are with reference to the insertionpoint. What I don't know, and
have been unable to discover, is the code for "between this starting
bracket and that ending bracket ... create and name a bookmark that
will be used later to accept the data in the fields in the form."

Could you help me with that?

TIA


Hi Sophia

I'm not sure why you're trying to create the boookmarks programatically (I
may have missed your point here!). Normally you create the boilerplate text
in your template and manually add bookmarks where you want to insert variable
text (say entered through a UserForm) into your document.

If this is what you're trying to do check out the following links:
http://word.mvps.org/FAQs/Userforms/index.htm
http://word.mvps.org/FAQs/Userforms/CreateAUserForm.htm

the second link also contains code on how to get text entered into a UserForm
control into the bookmark in your document.

You can of course create bookmarks programatically, but the trick of course
is knowing where in the document you want to create them!

If you need more info, or I missed the mark please post again.

HTH + Cheers - Peter


4ax.com:

Hello,

If I have an item such as:

"[PetOwner] would like to have [PetsName] returned by [DayOfTheWeek]"
in a document.

What is the code to create a set of "enclosing" bookmarks B1, B2, B3
that can be used to replace, for example [PetOwner], [PetsName] and
[DayOfTheWeek] with a variables?


TIA
 
P

Peter Hewett

Hi Sophia

I'm still not sure why you need a table of macro names, but as long as
you're in control - no problems.

This code will search the entire table of a document for any text in square
brackets. If it finds it, it will create a bookmark for the text inside the
brackets (excluding the brackets). Since I don't know how you're going to
derive a bookmark name from the text you're going to have to complete this
code for yourself. Do this just before the "ActiveDocument.Bookmarks.Add"
statement. And replace the "Name your bookmark here" with the variable
containing your bookmark name.

Public Sub FindInTable()
Dim rngFind As Word.Range
Dim rngToBookmark As Word.Range

' Search the first table in the document
Set rngFind = ActiveDocument.Tables(1).Range

' Setup for the search
With rngFind.Find
.ClearFormatting
.Text = "\[*\]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True

' Find the required text
Do While .Execute

' We don't want the enclosing brackets
Set rngToBookmark = rngFind.Duplicate
rngToBookmark.MoveStart wdCharacter, 1
rngToBookmark.MoveEnd wdCharacter, -1

' Now create a bookmark for the text
' but not the enclosing brackets
ActiveDocument.Bookmarks.Add "Name your bookmark here", _
rngToBookmark
Loop
End With
End Sub

HTH + Cheers - Peter


Thank you Steve and Peter,

The Text entries are listed sequentially in the first column of a
table, which table is part of a document which is opened by my
userform using

Set sourcedoc = Documents.Open(FileName:="C:\My
Documents\MacroName.doc")

A second column of the table provides a key "MacroName", which is
searched to access the appropriate row in the table, where something
like "[PetOwner] would like to have [PetsName] returned by
[DayOfTheWeek]" is found in the second "InsertText" column , and
eventually used as an insertion into the ActiveDocument at the
insertion point by a UserForm that is well under control at this
point.

Based on your previous replies, and after thinking about it over the
weekend, I think what I really need is some code to search the table
row by row, and in each instance where there is an
"[anythingwhatever]" in the InsertText cell, to set a range therein to
place a hidden "enclosed" bookmark that embraces the entire
"[anythingwhatever]" (while leaving the "[anythingwhatever]" in place
and visible) and then continuing in the same InsertText cell,
doing the same thing until it gets to the end of the text in
InsertText cell for that row. If I had that, I could go forward. I
understand the need to check for a pre-existing bookmark with the same
name before assigning a name.

So, something of the order:

For each

"[" in InsertText followed by a complementary "]" ...
set an enclosing bookmark named "BookMarkName"
(previously checked for to prevent duplicates)
that encloses every character from the "[" to the "]" and

Then

go on doing that until to the end of that InsertText cell.

Next RowInTheTable


I think I can manage what goes on outside this routine ... and to
describe it further would, I think, add some unnecessary confusion to
the picture.

(At least, I think it would ... otherwise I'll be coming to the door
again, with my outstretched hand. Please bear with me and get me to
this point.)

TIA

Sophia
 
S

Sophia

Thank you again Peter for your help, patience and understanding,
especially in view of what may appear to you to be my intransigence.

I'm certain your wisdom in all of this would have led me in a
different and more productive direction if I had begun here, in this
Group instead of charging out on my own (a bad habit of mine).

I'm embarrassed to say that I've become committed to the approach I
first used by conducting too many demos of "my method" too soon and
would find it somewhat embarrassing to revise things at this point.
I'm confident that what I'm doing will work, despite myself ...

Based upon what I've seen of your kind, patient and impeccably
detailed advice in this Group and others, I'm sure your code, which
I'll begin weaving into my own as soon as I finish this message, is
the last piece I need at this point.

Perhaps, when I get past this, I'll be able to suggest a "Version 2,"
which will bring me here for the advice I needed in the first place,
and you may choose to set me straight at that point.

Thanks again,

Sophia





Hi Sophia

I'm still not sure why you need a table of macro names, but as long as
you're in control - no problems.

This code will search the entire table of a document for any text in square
brackets. If it finds it, it will create a bookmark for the text inside the
brackets (excluding the brackets). Since I don't know how you're going to
derive a bookmark name from the text you're going to have to complete this
code for yourself. Do this just before the "ActiveDocument.Bookmarks.Add"
statement. And replace the "Name your bookmark here" with the variable
containing your bookmark name.

Public Sub FindInTable()
Dim rngFind As Word.Range
Dim rngToBookmark As Word.Range

' Search the first table in the document
Set rngFind = ActiveDocument.Tables(1).Range

' Setup for the search
With rngFind.Find
.ClearFormatting
.Text = "\[*\]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True

' Find the required text
Do While .Execute

' We don't want the enclosing brackets
Set rngToBookmark = rngFind.Duplicate
rngToBookmark.MoveStart wdCharacter, 1
rngToBookmark.MoveEnd wdCharacter, -1

' Now create a bookmark for the text
' but not the enclosing brackets
ActiveDocument.Bookmarks.Add "Name your bookmark here", _
rngToBookmark
Loop
End With
End Sub

HTH + Cheers - Peter


Thank you Steve and Peter,

The Text entries are listed sequentially in the first column of a
table, which table is part of a document which is opened by my
userform using

Set sourcedoc = Documents.Open(FileName:="C:\My
Documents\MacroName.doc")

A second column of the table provides a key "MacroName", which is
searched to access the appropriate row in the table, where something
like "[PetOwner] would like to have [PetsName] returned by
[DayOfTheWeek]" is found in the second "InsertText" column , and
eventually used as an insertion into the ActiveDocument at the
insertion point by a UserForm that is well under control at this
point.

Based on your previous replies, and after thinking about it over the
weekend, I think what I really need is some code to search the table
row by row, and in each instance where there is an
"[anythingwhatever]" in the InsertText cell, to set a range therein to
place a hidden "enclosed" bookmark that embraces the entire
"[anythingwhatever]" (while leaving the "[anythingwhatever]" in place
and visible) and then continuing in the same InsertText cell,
doing the same thing until it gets to the end of the text in
InsertText cell for that row. If I had that, I could go forward. I
understand the need to check for a pre-existing bookmark with the same
name before assigning a name.

So, something of the order:

For each

"[" in InsertText followed by a complementary "]" ...
set an enclosing bookmark named "BookMarkName"
(previously checked for to prevent duplicates)
that encloses every character from the "[" to the "]" and

Then

go on doing that until to the end of that InsertText cell.

Next RowInTheTable


I think I can manage what goes on outside this routine ... and to
describe it further would, I think, add some unnecessary confusion to
the picture.

(At least, I think it would ... otherwise I'll be coming to the door
again, with my outstretched hand. Please bear with me and get me to
this point.)

TIA

Sophia
 
P

Peter Hewett

Hi Sophia

Glad to have been of help. But wisdom and patience I dispute (you can't hear
me ranting at times!!), I'm happy to share the bits and pieces I know.

Good luck - Peter
 

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