Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word: How to delete words between ( ) 1

Status
Not open for further replies.

GPerk

Programmer
Jul 6, 2002
161
US
Assume my program has found a left paren at Words(j) in a paragraph, and a right paren at Words(k) (k>j).
How do I set the Range so I can delete all the words between the parens?
 
Hi GPerk,

As you seem to know, most actions are performed on ranges. To delete the range from the start of the left parenthesis to the end of the right parenthesis, use:

Code:
With ActiveDocument
    .Range(.Words(j).Start, .Words(k).End).Delete
End With

A point to note, if you have, for example, ")." at the end of the sentence, Word treats this as one a single word and there may be other similar "funnies" so it might be more correct to change it to the following. It depends on exactly what you have and what you want.

Code:
With ActiveDocument
    .Range(.Words(j).Start, .Words(k).
Code:
Start + 1
Code:
).Delete
End With

Enjoy,
Tony
 
Tony,
Thank you for your patience.

Your suggestion works:
With ActiveDocument
.Range(.Words(20).Start, .Words(30).End).Delete
End With

But I also want to restrict the operation to specific paragraphs. So I tried:
With ActiveDocument
.Range(.Paragraphs(4).Words(20).Start, .Paragraphs(4).Words(30).End).Delete
End With
But it highlights ".Words(20)" and says "Method or data member not found". Apparently ActiveDocument.Paragraphs(4).Words(20) is not a valid object.

So I tried:
Dim rngPara as Range
Set rngPara = ActiveDocument.Paragraphs(4).Range
rngPara(rngPara.Words(7).Start, rngPara.Words(15).End).Delete
But it doesn't like that either. Can this approach be made to work?
 
Hi,

I know this gets confusing sometimes, but you need to work with Ranges. A paragraph itself is not a range; its Range property gives youi the range which is the paragraph so use, for example:

Code:
.Paragraphs(4)
Code:
.Range
Code:
.Words(20).Start

Enjoy,
Tony
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top