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