Home |
Search |
Today's Posts |
#1
![]() |
|||
|
|||
![]()
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 |
#2
![]() |
|||
|
|||
![]()
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 |
#3
![]() |
|||
|
|||
![]()
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. Jay Freedman wrote: 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 |
Reply |
Thread Tools | |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
My backspace key will not delete entire words | Microsoft Word Help | |||
Finding, cutting and pasting multiple words and paragraphs with ma | Microsoft Word Help | |||
How to delete misspelled words | Microsoft Word Help | |||
How do I delete Word's list of reviewers? | Microsoft Word Help | |||
How do you delete words from the dictionary from Office 2000 | Microsoft Word Help |