Using Bookmarks to Define a Range

E

Elessvie

Hello! What I'm shooting for is to set a bookmark "First_Find" as the start
of the range oRng, and set the bookmark "Last_Find" as the end of the range
oRng. These bookmarks have already been set.

Here is my code:

oRng.Start = ActiveDocument.Bookmarks("First_Find")
oRng.End = ActiveDocument.Bookmarks("Last_Find")

The error message is "Run-time error 13: Type mismatch "

Do I have to define the bookmarks first as ranges? I am at a loss here.

If anyone could set me straight, I'd really appreciate it.

Thank you,

-Lynne
 
P

Pesach Shelnitz

Hi Lynn,

In your code, you need to create your Range object before you can set the
values of its properties., and you need to use the Range.Start or Range.End
property of each bookmark to obtain the values that you want to assign to the
Start and End properties of your range object. The following macro does these
things and displays the result.

Sub SetRange()

Dim oRng As Range

Set oRng = Selection.Range
oRng.start = ActiveDocument.Bookmarks("First_Find").Range.start
oRng.End = ActiveDocument.Bookmarks("Last_Find").Range.End
MsgBox oRng.start & vbTab & oRng.End
End Sub
 
E

Elessvie

Pesach, thank you so much! I actually had created the range already
(neglected to make that obvious), but I couldn't figure out how to get the
syntax right for the bookmarks from any of the documentation I have.

This exactly solves my problem.

Have a great day,

-Lynne
 
E

Elessvie

Thank you also for the bonus of the MsgBox to display the bookmark locations.
Very handy.

-Lynne
 

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