Defining Range Objects Using Bookmarks

M

MT DOJ Help Desk

Word 2000

I have a document that is used as a boiler plate for text. Throughout the
course of a shift we will paste blocks of data into the document. At the
end of the shift we process the data using a set a macros that I've written.

Currently, the macros work by moving the cursor in the document to select
blocks of text. I'm now working on having the macros use range objects
instead. The blocks of text can be of varying lengths, so I can't use a set
number of characters, lines, or paragraphs to create the range objects--they
have to be dynamic in that respect. So I thought that I would use
bookmarks. I've figured out how to create a bookmark each time a block of
text is pasted into the document. I'm creating the bookmark at the end of
the block of text. However, I haven't been able to figure out how to use
those bookmarks to define my range objects.

What I would like to do is have the macro define the range object so that it
starts with the current cursor location and ends with the NEXT bookmark.
How can I do that?

--Tom
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

Dim myrange As Range
Set myrange = Selection.Range
myrange.End = ActiveDocument.Range.End
myrange.End = myrange.Bookmarks(1).Range.End

will set a range that contains the text from the selection to the next
bookmark in the document.
--
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
 
J

Jezebel

First, each bookmark represents a range, so the first part of your question
is easy:

Dim pRange as Word.Range
:
Set pRange = ActiveDocument.Bookmarks("BookmarkName").Range

Second, you can use the Range method to define a range given any two
positions in the document, so to get a reference from the cursor location up
to a bookmark you could use

Set pRange = ActiveDocument.Range(Start:=Selection.Start, _
End:=ActiveDocument.Bookmarks("NextBookmarkName").Range.Start - 1)

You'll need your own logic to determine which is the next bookmark --
presumably depends on what else your macro is doing.
 

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