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
On 5月29日, 下午8時07分, StevenM <stevencraigmiller(at)comcast(dot)net>
wrote:
> To: Cyberdude,
>
[quoted text clipped - 30 lines]
>
> - 顯示被引用文字 -
StevenM - 29 May 2008 14:58 GMT
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
> Hi Steven,
>
[quoted text clipped - 56 lines]
> >
> > - 顯示被引用文字 -
cyberdude - 30 May 2008 03:21 GMT
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
On 5月29日, 下午9時58分, StevenM <stevencraigmiller(at)comcast(dot)net>
wrote:
> To: Cyberdude,
>
[quoted text clipped - 109 lines]
>
> - 顯示被引用文字 -
StevenM - 29 May 2008 18:50 GMT
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
Doug Robbins - Word MVP - 30 May 2008 01:44 GMT
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

Signature
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
> Hi Steven,
>
[quoted text clipped - 64 lines]
>>
>> - $Bp}<(Ho0zMQJ8;z(B -