Selecting Text After Bookmark

E

edamron

Hi. I'm need to get the text that has already been placed into a
document and put it into a string variable. The text starts at a
known bookmark.

I was thinking about using a range object but all of the examples I
see assume that I want a particular paragraph etc. The thing is I
can't be sure where in the document the bookmark will appear.

Is there any way to go to the bookmark
(ActiveDocument.Bookmarks(DOCKETBOOKMARK).Select) and then set the
range starting from the bookmark and extending 10 characters?

Thanks.
 
F

Fumei2 via OfficeKB.com

Is there any way to go to the bookmark (ActiveDocument.Bookmarks
(DOCKETBOOKMARK).Select) and then set the
range starting from the bookmark and extending 10 characters?

Yes, and you do NOT need to Select the bookmark.

What do you mean by "go to"? You mention set the range. The following makes
a Range object as you describe, but it is NOT selected. There is no "go to".

Dim r As Range
Set r = ActiveDocument.Range( _
Start:= ActiveDocument.Bookmarks(DOCKETBOOKMARK).Range _
End + 1, _
End:=ActiveDocument.Bookmarks(DOCKETBOOKMARK).Range _
End + 11)

Voila. A range object that starts the NEXT character after DOCKETBOOKMARK,
and is 10 characters in length.
 
F

Fumei2 via OfficeKB.com

Or to be more specific to your stated objective:

"I'm need to get the text that has already been placed into a document and
put it into a string variable."

Dim strMyVar As String
Dim r As Range

Set r = ActiveDocument.Range( _
Start:= ActiveDocument.Bookmarks(DOCKETBOOKMARK).Range _
End + 1, _
End:=ActiveDocument.Bookmarks(DOCKETBOOKMARK).Range _
End + 11)

strMyVar = r.Text

The variable strMyVar contains the text starting from the first character
after the bookmark DOCKETBOOKMARK, for 10 characters.

NOTE: there is no error trapping on this!
 
F

Fumei2 via OfficeKB.com

Or to be more specific to your stated objective:

"I'm need to get the text that has already been placed into a document and
put it into a string variable."

Dim strMyVar As String
Dim r As Range

Set r = ActiveDocument.Range( _
Start:= ActiveDocument.Bookmarks(DOCKETBOOKMARK).Range _
End + 1, _
End:=ActiveDocument.Bookmarks(DOCKETBOOKMARK).Range _
End + 11)

strMyVar = r.Text

The variable strMyVar contains the text starting from the first character
after the bookmark DOCKETBOOKMARK, for 10 characters. Nothing is selected.

NOTE: there is no error trapping on this!
 

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