Newbie - Locate positions in inserted text

C

Carol Williams

Hello ...

In Word97:

Take the sentence: "X took X units out of stock on X.

I'd like to insert this text into a document using a macro or
Autocorrect ... then have the cursor move to the first "X" and allow
some text to be entered ... replacing the "X".

After that, I'd like to be able to press a selected key (say, the ">"
key) that would move the cursor to the next "X" and allow some text
to be entered ... replacing that "X" ... and so forth, until the end
of the inserted text is reached.

Can someone give me some ideas on how to do this?

TIA
 
C

Carol Williams

Hello again,

Been doing my homework ...

I've been experimenting with my preceeding post and discovered
"enclosing" bookmarks, much better way of accomplishing what I needed
to do.

Could someone please give me an idea of what code I would use to move
from "enclosing" bookmark to the next within the inserted text?

The idea would be:

Insert the text in the document using AutoCorrect (or AutoText) entry.
Move to the first bookmark ... insert the text ... go to the next
bookmark, etc. The move would be triggered by pressing a designated
key.

Is there a "goto the next bookmark" statement, rather than to goto a
bookmark by name?

Thanks for your help ...
 
J

Jay Freedman

Hi, Carol,

There isn't any "goto next bookmark" statement. But there are a couple
of ways to work with this sort of thing.

Probably the easiest way is to code a loop that will go through the
entire Bookmarks collection of the document, using the bookmark's
..Name property to decide what to put in each one. This little macro
shows how you can select each one in turn:

Sub WalkBookmarksCollection()
Dim bm As Bookmark
For Each bm In ActiveDocument.Range.Bookmarks
bm.Select
MsgBox bm.Name
Next bm
End Sub

[If you use ActiveDocument.Bookmarks instead of
ActiveDocument.Range.Bookmarks, you'll visit them in alphabetical name
order instead of location order.]

So you would replace the bm.Select and the MsgBox with bm.Range.Text =
"whatever", or insert an AutoText entry there. [You don't have to
select a bookmark in order to change its contents! In fact, it's
faster if you don't select.]

When you do put something in a bookmark, if you want the bookmark to
still be there (so you might replace it again later), you have to
reapply it. See
http://word.mvps.org/FAQs/MacrosVBA/InsertingTextAtBookmark.htm.

AutoCorrect has no place in all this -- it's triggered strictly by
keystrokes, not by macros.

If you really want to move the cursor to the next bookmark, you'd have
to do something like this:

Sub GotoNextBookmark()
Dim r1 As Range, r2 As Range

Set r1 = ActiveDocument.Range
r1.Start = Selection.Start
If r1.Bookmarks.Count = 0 Then Exit Sub

Set r2 = r1.Bookmarks(1).Range
If r2.Start = r1.Start Then
r1.Start = r2.End
If r1.Bookmarks.Count = 0 Then Exit Sub
Set r2 = r1.Bookmarks(1).Range
End If

r2.Select
End Sub
 
M

Mark

Thank you Jay, for all the effort you obviously put into this.

I'll give your ideas a try over the weekend ... and may end up a
heroine on Monday morning ;-).

I did come across a piece of code on a Usenet search that I'll also
put to a test:

"This might be a bit of a sledge hammer approach, but it works:

Selection.Extend
Selection.EndKey Unit:=wdStory
Selection.Bookmarks(1).Range.Select

Hope this helps, Doug Robbins - Word MVP"

Regards,

Carol



Hi, Carol,

There isn't any "goto next bookmark" statement. But there are a couple
of ways to work with this sort of thing.

Probably the easiest way is to code a loop that will go through the
entire Bookmarks collection of the document, using the bookmark's
.Name property to decide what to put in each one. This little macro
shows how you can select each one in turn:

Sub WalkBookmarksCollection()
Dim bm As Bookmark
For Each bm In ActiveDocument.Range.Bookmarks
bm.Select
MsgBox bm.Name
Next bm
End Sub

[If you use ActiveDocument.Bookmarks instead of
ActiveDocument.Range.Bookmarks, you'll visit them in alphabetical name
order instead of location order.]

So you would replace the bm.Select and the MsgBox with bm.Range.Text =
"whatever", or insert an AutoText entry there. [You don't have to
select a bookmark in order to change its contents! In fact, it's
faster if you don't select.]

When you do put something in a bookmark, if you want the bookmark to
still be there (so you might replace it again later), you have to
reapply it. See
http://word.mvps.org/FAQs/MacrosVBA/InsertingTextAtBookmark.htm.

AutoCorrect has no place in all this -- it's triggered strictly by
keystrokes, not by macros.

If you really want to move the cursor to the next bookmark, you'd have
to do something like this:

Sub GotoNextBookmark()
Dim r1 As Range, r2 As Range

Set r1 = ActiveDocument.Range
r1.Start = Selection.Start
If r1.Bookmarks.Count = 0 Then Exit Sub

Set r2 = r1.Bookmarks(1).Range
If r2.Start = r1.Start Then
r1.Start = r2.End
If r1.Bookmarks.Count = 0 Then Exit Sub
Set r2 = r1.Bookmarks(1).Range
End If

r2.Select
End Sub

Carol Williams said:
Hello again,

Been doing my homework ...

I've been experimenting with my preceeding post and discovered
"enclosing" bookmarks, much better way of accomplishing what I needed
to do.

Could someone please give me an idea of what code I would use to move
from "enclosing" bookmark to the next within the inserted text?

The idea would be:

Insert the text in the document using AutoCorrect (or AutoText) entry.
Move to the first bookmark ... insert the text ... go to the next
bookmark, etc. The move would be triggered by pressing a designated
key.

Is there a "goto the next bookmark" statement, rather than to goto a
bookmark by name?

Thanks for your help ...
 
G

Graham Mayor

Is he having a sex change?
http://word.mvps.org/AboutMVPs/jay_freedman.htm

--
<>>< ><<> ><<> <>>< ><<> <>>< <>>< ><<>
Graham Mayor - Word MVP
E-mail (e-mail address removed)
Web site www.gmayor.com
Word MVP web site www.mvps.org/word
<>>< ><<> ><<> <>>< ><<> <>>< <>>< ><<>

Thank you Jay, for all the effort you obviously put into this.

I'll give your ideas a try over the weekend ... and may end up a
heroine on Monday morning ;-).

I did come across a piece of code on a Usenet search that I'll also
put to a test:

"This might be a bit of a sledge hammer approach, but it works:

Selection.Extend
Selection.EndKey Unit:=wdStory
Selection.Bookmarks(1).Range.Select

Hope this helps, Doug Robbins - Word MVP"

Regards,

Carol



Hi, Carol,

There isn't any "goto next bookmark" statement. But there are a
couple of ways to work with this sort of thing.

Probably the easiest way is to code a loop that will go through the
entire Bookmarks collection of the document, using the bookmark's
.Name property to decide what to put in each one. This little macro
shows how you can select each one in turn:

Sub WalkBookmarksCollection()
Dim bm As Bookmark
For Each bm In ActiveDocument.Range.Bookmarks
bm.Select
MsgBox bm.Name
Next bm
End Sub

[If you use ActiveDocument.Bookmarks instead of
ActiveDocument.Range.Bookmarks, you'll visit them in alphabetical
name order instead of location order.]

So you would replace the bm.Select and the MsgBox with bm.Range.Text
= "whatever", or insert an AutoText entry there. [You don't have to
select a bookmark in order to change its contents! In fact, it's
faster if you don't select.]

When you do put something in a bookmark, if you want the bookmark to
still be there (so you might replace it again later), you have to
reapply it. See
http://word.mvps.org/FAQs/MacrosVBA/InsertingTextAtBookmark.htm.

AutoCorrect has no place in all this -- it's triggered strictly by
keystrokes, not by macros.

If you really want to move the cursor to the next bookmark, you'd
have to do something like this:

Sub GotoNextBookmark()
Dim r1 As Range, r2 As Range

Set r1 = ActiveDocument.Range
r1.Start = Selection.Start
If r1.Bookmarks.Count = 0 Then Exit Sub

Set r2 = r1.Bookmarks(1).Range
If r2.Start = r1.Start Then
r1.Start = r2.End
If r1.Bookmarks.Count = 0 Then Exit Sub
Set r2 = r1.Bookmarks(1).Range
End If

r2.Select
End Sub

Carol Williams said:
Hello again,

Been doing my homework ...

I've been experimenting with my preceeding post and discovered
"enclosing" bookmarks, much better way of accomplishing what I
needed to do.

Could someone please give me an idea of what code I would use to
move from "enclosing" bookmark to the next within the inserted text?

The idea would be:

Insert the text in the document using AutoCorrect (or AutoText)
entry. Move to the first bookmark ... insert the text ... go to the
next bookmark, etc. The move would be triggered by pressing a
designated key.

Is there a "goto the next bookmark" statement, rather than to goto a
bookmark by name?

Thanks for your help ...



Hello ...

In Word97:

Take the sentence: "X took X units out of stock on X.

I'd like to insert this text into a document using a macro or
Autocorrect ... then have the cursor move to the first "X" and
allow some text to be entered ... replacing the "X".

After that, I'd like to be able to press a selected key (say, the
">" key) that would move the cursor to the next "X" and allow
some text to be entered ... replacing that "X" ... and so forth,
until the end of the inserted text is reached.

Can someone give me some ideas on how to do this?

TIA
 
J

Jay Freedman

Hi, Carol (I assume you're just using Mark's computer for a while):

I'll have to have a word with Doug about that code snippet. <eg> It
will work, but only if there is a bookmark somewhere after the current
cursor location. If there isn't one, the code will stop with an error
("The requested member of the collection does not exist"), and the
whole rest of the document from the original cursor position to the
end will be left selected. Throwing in an On Error trap will suppress
the error message, but the selection will still be messed up.

The code I showed does essentially the same thing -- select the first
bookmark that occurs between the cursor position and the end of the
document -- but it spends some effort making sure there really is a
bookmark before it does anything to change the selection.

Mark said:
Thank you Jay, for all the effort you obviously put into this.

I'll give your ideas a try over the weekend ... and may end up a
heroine on Monday morning ;-).

I did come across a piece of code on a Usenet search that I'll also
put to a test:

"This might be a bit of a sledge hammer approach, but it works:

Selection.Extend
Selection.EndKey Unit:=wdStory
Selection.Bookmarks(1).Range.Select

Hope this helps, Doug Robbins - Word MVP"

Regards,

Carol



Hi, Carol,

There isn't any "goto next bookmark" statement. But there are a couple
of ways to work with this sort of thing.

Probably the easiest way is to code a loop that will go through the
entire Bookmarks collection of the document, using the bookmark's
.Name property to decide what to put in each one. This little macro
shows how you can select each one in turn:

Sub WalkBookmarksCollection()
Dim bm As Bookmark
For Each bm In ActiveDocument.Range.Bookmarks
bm.Select
MsgBox bm.Name
Next bm
End Sub

[If you use ActiveDocument.Bookmarks instead of
ActiveDocument.Range.Bookmarks, you'll visit them in alphabetical name
order instead of location order.]

So you would replace the bm.Select and the MsgBox with bm.Range.Text =
"whatever", or insert an AutoText entry there. [You don't have to
select a bookmark in order to change its contents! In fact, it's
faster if you don't select.]

When you do put something in a bookmark, if you want the bookmark to
still be there (so you might replace it again later), you have to
reapply it. See
http://word.mvps.org/FAQs/MacrosVBA/InsertingTextAtBookmark.htm.

AutoCorrect has no place in all this -- it's triggered strictly by
keystrokes, not by macros.

If you really want to move the cursor to the next bookmark, you'd have
to do something like this:

Sub GotoNextBookmark()
Dim r1 As Range, r2 As Range

Set r1 = ActiveDocument.Range
r1.Start = Selection.Start
If r1.Bookmarks.Count = 0 Then Exit Sub

Set r2 = r1.Bookmarks(1).Range
If r2.Start = r1.Start Then
r1.Start = r2.End
If r1.Bookmarks.Count = 0 Then Exit Sub
Set r2 = r1.Bookmarks(1).Range
End If

r2.Select
End Sub

Carol Williams said:
Hello again,

Been doing my homework ...

I've been experimenting with my preceeding post and discovered
"enclosing" bookmarks, much better way of accomplishing what I needed
to do.

Could someone please give me an idea of what code I would use to move
from "enclosing" bookmark to the next within the inserted text?

The idea would be:

Insert the text in the document using AutoCorrect (or AutoText) entry.
Move to the first bookmark ... insert the text ... go to the next
bookmark, etc. The move would be triggered by pressing a designated
key.

Is there a "goto the next bookmark" statement, rather than to goto a
bookmark by name?

Thanks for your help ...



Hello ...

In Word97:

Take the sentence: "X took X units out of stock on X.

I'd like to insert this text into a document using a macro or
Autocorrect ... then have the cursor move to the first "X" and allow
some text to be entered ... replacing the "X".

After that, I'd like to be able to press a selected key (say, the ">"
key) that would move the cursor to the next "X" and allow some text
to be entered ... replacing that "X" ... and so forth, until the end
of the inserted text is reached.

Can someone give me some ideas on how to do this?

TIA
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

Hi Jay

I did say it was a "sledge hammer approach" and looking now at the original
thread
http://groups.google.com/groups?hl=...ix%24GA.52%40cppssbbsa02.microsoft.com&rnum=2,
the OP wanted to go to the next bookmark.
--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.
Hope this helps
Doug Robbins - Word MVP
Jay Freedman said:
Hi, Carol (I assume you're just using Mark's computer for a while):

I'll have to have a word with Doug about that code snippet. <eg> It
will work, but only if there is a bookmark somewhere after the current
cursor location. If there isn't one, the code will stop with an error
("The requested member of the collection does not exist"), and the
whole rest of the document from the original cursor position to the
end will be left selected. Throwing in an On Error trap will suppress
the error message, but the selection will still be messed up.

The code I showed does essentially the same thing -- select the first
bookmark that occurs between the cursor position and the end of the
document -- but it spends some effort making sure there really is a
bookmark before it does anything to change the selection.

Mark said:
Thank you Jay, for all the effort you obviously put into this.

I'll give your ideas a try over the weekend ... and may end up a
heroine on Monday morning ;-).

I did come across a piece of code on a Usenet search that I'll also
put to a test:

"This might be a bit of a sledge hammer approach, but it works:

Selection.Extend
Selection.EndKey Unit:=wdStory
Selection.Bookmarks(1).Range.Select

Hope this helps, Doug Robbins - Word MVP"

Regards,

Carol



Hi, Carol,

There isn't any "goto next bookmark" statement. But there are a couple
of ways to work with this sort of thing.

Probably the easiest way is to code a loop that will go through the
entire Bookmarks collection of the document, using the bookmark's
.Name property to decide what to put in each one. This little macro
shows how you can select each one in turn:

Sub WalkBookmarksCollection()
Dim bm As Bookmark
For Each bm In ActiveDocument.Range.Bookmarks
bm.Select
MsgBox bm.Name
Next bm
End Sub

[If you use ActiveDocument.Bookmarks instead of
ActiveDocument.Range.Bookmarks, you'll visit them in alphabetical name
order instead of location order.]

So you would replace the bm.Select and the MsgBox with bm.Range.Text =
"whatever", or insert an AutoText entry there. [You don't have to
select a bookmark in order to change its contents! In fact, it's
faster if you don't select.]

When you do put something in a bookmark, if you want the bookmark to
still be there (so you might replace it again later), you have to
reapply it. See
http://word.mvps.org/FAQs/MacrosVBA/InsertingTextAtBookmark.htm.

AutoCorrect has no place in all this -- it's triggered strictly by
keystrokes, not by macros.

If you really want to move the cursor to the next bookmark, you'd have
to do something like this:

Sub GotoNextBookmark()
Dim r1 As Range, r2 As Range

Set r1 = ActiveDocument.Range
r1.Start = Selection.Start
If r1.Bookmarks.Count = 0 Then Exit Sub

Set r2 = r1.Bookmarks(1).Range
If r2.Start = r1.Start Then
r1.Start = r2.End
If r1.Bookmarks.Count = 0 Then Exit Sub
Set r2 = r1.Bookmarks(1).Range
End If

r2.Select
End Sub


Hello again,

Been doing my homework ...

I've been experimenting with my preceeding post and discovered
"enclosing" bookmarks, much better way of accomplishing what I needed
to do.

Could someone please give me an idea of what code I would use to move
from "enclosing" bookmark to the next within the inserted text?

The idea would be:

Insert the text in the document using AutoCorrect (or AutoText) entry.
Move to the first bookmark ... insert the text ... go to the next
bookmark, etc. The move would be triggered by pressing a designated
key.

Is there a "goto the next bookmark" statement, rather than to goto a
bookmark by name?

Thanks for your help ...



Hello ...

In Word97:

Take the sentence: "X took X units out of stock on X.

I'd like to insert this text into a document using a macro or
Autocorrect ... then have the cursor move to the first "X" and allow
some text to be entered ... replacing the "X".

After that, I'd like to be able to press a selected key (say, the ">"
key) that would move the cursor to the next "X" and allow some text
to be entered ... replacing that "X" ... and so forth, until the end
of the inserted text is reached.

Can someone give me some ideas on how to do this?

TIA
 
J

Jay Freedman

Thanks, Doug, I wasn't sure where that came from. Looking at the whole
thread, the OP's scheme was pretty bizarre, and Cindy steered him in a
different direction. Still, at some point he would have come to the
last bookmark in the document, and invoking the macro one more time
would throw the "member does not exist" error.

Maybe it's my background in network programming, where 80% of most
code is error traps, but that kind of thing just rings my bell.

"Doug Robbins - Word MVP - DELETE UPPERCASE CHARACTERS FROM EMAIL
 
M

Mark

Thank you both for your kind help.

And thank you Jay for your observation: After all, I *do* have to
spend my weekends *somewhere*, and ... actually, I'm happy enough
with the sex I was born with, thank you.

I'm sorry to say I don't always leave my work behind ... and Monday
is looming as I write. Being new at this, I hadn't considered the
question of trying to move beyond the last bookmark ... but as a
famous man once said:

You can use an eraser on the drafting table or a sledge hammer on the
construction site. - Frank Lloyd Wright

Regards,

Carol @ MarksHouse ;-)
 

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