Deleting paragraphs or lines NOT marked

P

Paulz

Hi everyone,
I was hoping someone could help with some code.
I have a word 2003 document with 2,000 pages and I need a macro that
deletes all lines /paragraphs that DO NOT begin with the % character.
I have marked lines with % that I want to keep.

Would greatly appreciate a macro that could do this.

Cheers Paul
 
J

Jay Freedman

Hi everyone,
I was hoping someone could help with some code.
I have a word 2003 document with 2,000 pages and I need a macro that
deletes all lines /paragraphs that DO NOT begin with the % character.
I have marked lines with % that I want to keep.

Would greatly appreciate a macro that could do this.

Cheers Paul

This will do it:

Dim nPara As Long
Dim oRg As Range

For nPara = ActiveDocument.Paragraphs.Count To 1 Step -1
Set oRg = ActiveDocument.Paragraphs(nPara).Range
If oRg.Characters.First <> "%" Then
oRg.Delete
End If
Next

If the number of paragraphs is very large, this may take a long time
to run. The reason is that every time the Set statement runs, VBA
counts through the paragraphs from 1 to the current value of nPara to
find the right place to set the range. This variation may run faster:

Dim oPara As Paragraph
Dim prevPara As Paragraph

Set oPara = ActiveDocument.Paragraphs.Last
Do
Set prevPara = oPara.Previous
If oPara.Range.Characters.First <> "%" Then
oPara.Range.Delete
End If
Set oPara = prevPara
Loop Until oPara Is Nothing
 

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