Delete first words in all paragraphs

  • Thread starter Thread starter Bob Frolek
  • Start date Start date
B

Bob Frolek

Does someone have a macro that will delete the first fifteen words in
all paragraphs in a Word document and delete all paragraphs with less
than 16 words? Thanks
 
Bob said:
Does someone have a macro that will delete the first fifteen words in
all paragraphs in a Word document and delete all paragraphs with less
than 16 words? Thanks

Hi Bob,

Why would anyone have such a special-purpose macro lying around? ;-)

You could write something like this:

Sub Delete15Words()
Dim oPar As Paragraph
Dim i As Integer
For Each oPar In ActiveDocument.Paragraphs
With oPar.Range
If .Words.Count < 16 Then
.Delete
Else
For i = 1 To 15
.Words(1).Delete
Next i
End If
End With
Next oPar
End Sub

But you'd probably be disappointed with the results. The problem is that the
..Words collection in VBA doesn't agree with most people's intuitive
understanding of what a word is, nor does it agree with the Tools > Word
Count dialog. For the purposes of the .Words collection, each punctuation
mark and each (normally invisible) paragraph mark counts as a "word"! If
your "first 15 words" include any commas, periods, question marks,
exclamation points, semicolons, or colons, the macro will delete too few
words.

In any case, the test should be changed to

If .Words.Count < 17 Then

because the paragraph mark at the end of a 15-word paragraph would make
..Words.Count = 16 and you still want to delete the whole paragraph.

What are the 15 words? If they always fit the same pattern, you might do
better with a wildcard search
(http://www.gmayor.com/replace_using_wildcards.htm).
 
Thanks, Jay. Macro works beautifully. Word count I can work around.
Words in first part of para are always different and there's no
pattern.
 

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

Back
Top