View Single Post
  #2   Report Post  
Jay Freedman
 
Posts: n/a
Default

Bob Frolek wrote:
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).

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org