Computer Forum Forum Index
Register  FAQ Profile Log in to check your private messages Log in Register 

Deleting paragraphs or lines NOT marked

 
Post new topic   Reply to topic    Computer Forum Forum Index -> microsoft word programming
View previous topic :: View next topic  
Author Message
Paulz
Guest





PostPosted: Thu Dec 13, 2007 12:10 am    Post subject: Deleting paragraphs or lines NOT marked Reply with quote

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
Back to top
Google
Sponsor





PostPosted: Thu Dec 13, 2007 12:10 am    Post subject: Advertisement

Back to top
Jay Freedman
Guest





PostPosted: Thu Dec 13, 2007 4:00 am    Post subject: Re: Deleting paragraphs or lines NOT marked Reply with quote

On Wed, 12 Dec 2007 16:10:30 -0800 (PST), Paulz <husky101@yahoo.com>
wrote:

Quote:
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

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
Back to top
Paulz
Guest





PostPosted: Thu Dec 13, 2007 4:43 am    Post subject: Re: Deleting paragraphs or lines NOT marked Reply with quote

Thanks Jay,
That was fabulous, worked a treat.
regards paul

On Dec 13, 3:00 pm, Jay Freedman <jay.freed...@verizon.net> wrote:
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Computer Forum Forum Index -> microsoft word programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Computer Forum