Another requirement to delete text

I

Ian

Using Word97.

I need to delete a chunk of text from a document. Although I have no
detailed knowledge of the nature of the chunk, I do know exactly where
it starts (and can set a "start" bookmark there), and I do know exactly
where it finishes (and can set a "finish" bookmark there).

For example:

~~~~~~~~~~~~~~~~~~~~~~~~~~
First line of doc
another line
and another
| <<< "start" bookmark, followed by lots
of text of unknown nature or
length "finish" bookmark >>> |
and here is the rest of the document
~~~~~~~~~~~~~~~~~~~~~~~~~~~

What I need to know is, having positioned at "start", how can I then
extend the selection from there down to "finish", and then delete the
selected chunk?
 
J

Jonathan West

Ian said:
Using Word97.

I need to delete a chunk of text from a document. Although I have no
detailed knowledge of the nature of the chunk, I do know exactly where
it starts (and can set a "start" bookmark there), and I do know exactly
where it finishes (and can set a "finish" bookmark there).

For example:

~~~~~~~~~~~~~~~~~~~~~~~~~~
First line of doc
another line
and another
| <<< "start" bookmark, followed by lots
of text of unknown nature or
length "finish" bookmark >>> |
and here is the rest of the document
~~~~~~~~~~~~~~~~~~~~~~~~~~~

What I need to know is, having positioned at "start", how can I then
extend the selection from there down to "finish", and then delete the
selected chunk?

Hi Ian,

Something ike this should do.

Dim myRange as Range
Set myRange = ActiveDocument.Bookmarks("StartBookmark").Range
myRange.End = ActiveDocument.Bookmarks("FinishBookmark").Range.End
myRange.Delete

Or if you want to make a one-liner out of it, this does the same thing

ActiveDocument.Range(ActiveDocument.Bookmarks("StartBookmark").Range.Start,
_
ActiveDocument.Bookmarks("FinishBookmark").Range.End).Delete
 
I

Ian

Jonathan West said:
Hi Ian,

Something ike this should do.

Dim myRange as Range
Set myRange = ActiveDocument.Bookmarks("StartBookmark").Range
myRange.End = ActiveDocument.Bookmarks("FinishBookmark").Range.End
myRange.Delete

Or if you want to make a one-liner out of it, this does the same thing

ActiveDocument.Range(ActiveDocument.Bookmarks("StartBookmark").Range.Start,
_
ActiveDocument.Bookmarks("FinishBookmark").Range.End).Delete

Hi Jonathan.

I went for the one-liner. 100% first time. Many thanks.
 
J

Jay Freedman

Hi, Ian,

Since you were just asking about Range objects, here's a situation
where they provide a clean solution.

Almost every item in a document has a .Range property that expresses
where that item occurs relative to the start of the document. The
Range in turn has properties .Start and .End, which are the numbers of
characters from the start of the document to the start and end of the
range.

In this case, you want a range that goes from the .Start of the range
of ActiveDocument.Bookmarks("start") [or whatever you've actually
named that bookmark] to the .End of
ActiveDocument.Bookmarks("finish"). Once you get that range, you can
use its .Delete method to remove it from the document. Here's the
code:

Sub DeleteBetweenBookmarks()
' declare a Range object
Dim MyRange As Range

' point the Range object to a preexisting range
' returned by the "start" bookmark's .Range property
Set MyRange = ActiveDocument.Bookmarks("start").Range

' extend the Range object by moving its End
' while MyRange.Start stays where it is
MyRange.End = ActiveDocument.Bookmarks("finish").Range.End

' remove it
MyRange.Delete
End Sub
 
I

Ian

Jay Freedman said:
Hi, Ian,

Since you were just asking about Range objects, here's a situation
where they provide a clean solution.
[Snip]

Jay, that is most helpful. Ever thought of writing a book .... :))
 

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