Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / May 2008

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
cyberdude - 29 May 2008 03:40 GMT
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
Cindy M. - 29 May 2008 09:46 GMT
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 :-)
cyberdude - 29 May 2008 12:13 GMT
Hi Cindy,

I mean a line of text.

Mike

> Hi Cyberdude,
>
[quoted text clipped - 11 lines]
> This reply is posted in the Newsgroup; please post any follow question
> or reply in the newsgroup and not by e-mail :-)
Doug Robbins - Word MVP - 29 May 2008 12:59 GMT
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

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 Cindy,
>
[quoted text clipped - 19 lines]
>> This reply is posted in the Newsgroup; please post any follow question
>> or reply in the newsgroup and not by e-mail :-)
StevenM - 29 May 2008 13:07 GMT
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

> Hi Cindy,
>
[quoted text clipped - 17 lines]
> > This reply is posted in the Newsgroup; please post any follow question
> > or reply in the newsgroup and not by e-mail :-)
cyberdude - 29 May 2008 13:50 GMT
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 -
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.