How to modify the following code so that it deletes down to...?

C

cyberdude

Hi,

Someone gave me the following code highlighting the block of words
ending with "test" and deleting it. I would like to modify it so that
it hightlights the block of words down to the end of the row which is
just ABOVE the word "test" and deletes that block. Could someone tell
me how to do it? Thanks.

Sub Testb()
Dim x As Long
Dim rDcm As Range
Set rDcm = Selection.Range
x = rDcm.start
rDcm.End = ActiveDocument.Range.End
With rDcm.Find
.Text = "test"
If .Execute Then
rDcm.start = x
rDcm.HighlightColorIndex = wdYellow
rDcm.Delete
End If
End With
End Sub

Mike
 
C

Cindy M.

Hi Cyberdude,
Someone gave me the following code highlighting the block of words
ending with "test" and deleting it. I would like to modify it so that
it hightlights the block of words down to the end of the row which is
just ABOVE the word "test" and deletes that block.
"Row" in Word means a table row. Do you mean that? Or Paragraph? Or
Line of text?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 
C

cyberdude

Hi Cindy,

I mean a line of text.

Mike

Hi Cyberdude,


"Row" in Word means a table row. Do you mean that? Or Paragraph? Or
Line of text?

Cindy Meister
INTER-Solutions, Switzerlandhttp://homepage.swissonline.ch/cindymeister(last update Jun 17 2005)http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 
D

Doug Robbins - Word MVP

Use:

Dim range1 As Range, range2 As Range
Set range1 = ActiveDocument.Range
range1.End = range1.start + InStr(range1, "test")
Set range2 = range1.Duplicate
range2.Collapse wdCollapseEnd
Set range2 = range2.Bookmarks("\line").Range
range2.Collapse wdCollapseStart
range1.End = range2.start - 1
range1.Delete

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
S

StevenM

To: Cyberdude,

It would be easier if you worked with paragraphs or sentences rather than
lines. For there is a natural hierarchy between sections, paragraphs,
sentences, and words. It is more difficult to work with lines or pages, since
paragraphs, sentences and words often break over lines and pages. Perhaps if
you could give us a sample of your text it would make things easier for us to
understand.

Steven Craig Miller
 
C

cyberdude

Hi Steven,

I should have given an example to make things easier to understand.
I wanted a code that deletes a block of words starting from the
cursor's current position to the end of the line which is just above
the word "test". An example is here:

This is a story. A prince lived in a house with a chimney and a
garden.
One day, a girl appeared.....
......................................
They finally escaped the disaster.
But they couldn't pass the next test together.

Suppose my cursor is currently in front of the word "This". I want
the VBA code to delete the words from "This" up to the line containing
"They finally escaped the disaster." which is just above the word
"test" below. Therefore, after deletion, only this remains:
"But they couldn't pass the next test together."

Thanks.

Mike
 
S

StevenM

To: Cyberdude,

Below are two examples, the first takes your text as one paragraph, the
second makes it a series of paragraphs.

Sub TestDeleteRangeBeforeSentenceWith()
Dim newDoc As Document
Dim newRange As Range

Set newDoc = Documents.Add
Set newRange = newDoc.Range(0, 0)
newRange.Text = "This is a story. A prince lived in a house with a
chimney and a garden. One day, a girl appeared.....
....................................... They finally escaped the disaster. But
they couldn't pass the next test together."
MsgBox "Click ok"
Call DeleteRangeBeforeSentenceWith(newRange, "test")
End Sub

Function DeleteRangeBeforeSentenceWith(ByVal oRange As Range, ByVal sStr As
String)
oRange.End = oRange.Start + InStr(1, oRange, sStr)
oRange.MoveEnd wdSentence, -1
oRange.Delete
End Function

Sub TestDeleteRangeBeforeParagraphWith()
Dim newDoc As Document
Dim newRange As Range
Dim range1 As Range

Set newDoc = Documents.Add
Set newRange = newDoc.Range(0, 0)
newRange.Text = "This is a story." & vbCr _
& "A prince lived in a house with a chimney and a garden. One day, a
girl appeared..... ......................................" & vbCr _
& "They finally escaped the disaster." & vbCr _
& "But they couldn't pass the next test together."
MsgBox "Click ok"
Call DeleteRangeBeforeParagraphWith(newRange, "test")
End Sub

Function DeleteRangeBeforeParagraphWith(ByVal oRange As Range, ByVal sStr As
String)
oRange.End = oRange.Start + InStr(1, oRange, sStr)
oRange.MoveEnd wdParagraph, -1
oRange.Delete
End Function

Steven Craig Miller
 
S

StevenM

To: Cyberdude,

I have another example, I don't like it as much, but it works with "lines"
(per your request). It is similar to the code posted by Doug Robbins, except
I couldn't get "Bookmarks" to work directly with Ranges. So I made
modifications as noted below in the code.

Sub TestDeleteRangeBeforeLineWith()
Dim newDoc As Document
Dim newRange As Range

Set newDoc = Documents.Add
Set newRange = newDoc.Range(0, 0)
newRange.Text = "This is a story. A prince lived in a house with a
chimney and a garden. One day, a girl appeared.....
....................................... They finally escaped the disaster. But
they couldn't pass the next test together."
MsgBox "Click ok"
Call DeleteRangeBeforeLineWith(newRange, "test")
End Sub
'
' I couldn't get "Bookmarks" to work directly with ranges.
' It appears to work only with selections, and so I got it
' to work using .Select and Range.Parent
'
Function DeleteRangeBeforeLineWith(ByVal oRange As Range, ByVal sStr As
String)
Dim tempRange As Range
' Save the original range
Set tempRange = oRange.Duplicate
' Find sStr and select it
With tempRange
.End = oRange.Start + InStr(1, oRange, sStr)
.Collapse wdCollapseEnd
.Select
End With
' Get range of the line where the cursor is now located
Set tempRange = tempRange.Parent.Bookmarks("\line").Range
' Backup one character
With tempRange
.Collapse wdCollapseStart
.Move wdCharacter, -1
End With
' Reset the end of the original range & delete
oRange.End = tempRange.End
oRange.Delete
End Function



-Steven Craig Miller
 
D

Doug Robbins - Word MVP

Dim range1 As Range, range2 As Range
Set range1 = ActiveDocument.Range
range1.Start = Selection.Range.Start
range1.End = range1.start + InStr(range1, "test")
Set range2 = range1.Duplicate
range2.Collapse wdCollapseEnd
Set range2 = range2.Bookmarks("\line").Range
range2.Collapse wdCollapseStart
range1.End = range2.start - 1
range1.Delete


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
C

cyberdude

Hi Steven,

Thanks for your reply.

In your code, one of the lines reads

newRange.Text = "This is a story. A prince lived in a house with a
chimney and a garden. One day, a girl appeared.....
...................................... They finally escaped the disaster. But
they couldn't pass the next test together."

Does your code need me to input the text like you did and store the
block of
text in the variable newRange.Text? Thanks.

Mike
 

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