![]() |
|
#1
|
|||
|
|||
|
I work on 200+page long documents and want to write a macro that hide all paragraphs that do not contain the specific words I search for. For example, I want to search for the term "White Rabbit", highlight all the search results and then hide all paragraphs that do not contain this search term.
I put together a macro based on the other macros I found on this forum. It sometimes works really well. But then out of no where it will stop working and crash Microsoft Word (I am using Word 2013 Windows 10 64 bit) and I have to restart the computer to make it work again. Could you help me see what might be the problem? For example, it crashes each time I search for any single word. It occasionally works if I search for multiple words and occasionally not. This is driving me crazy. Thanks! Code:
Sub ShowFoundResults()
Application.ScreenUpdating = False
ActiveWindow.View.ShowAll = True
Dim StrFnd As String
Dim DocRange As Range
Dim ParaRange As Range
Dim WasFound As Boolean
StrFnd = InputBox("Find Text with Wildcard")
If Trim(StrFnd) = "" Then Exit Sub
WasFound = False
ActiveDocument.Range.Font.Hidden = True
Set DocRange = ActiveDocument.Range
With DocRange.Find
.ClearFormatting
.Text = StrFnd
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
Do While .Execute
WasFound = True
Set ParaRange = DocRange.Paragraphs(1).Range
ParaRange.Font.Hidden = False
DocRange.SetRange DocRange.Paragraphs(1).Range.End, _
ActiveDocument.Range.End
Loop
End With
ActiveWindow.View.ShowAll = False
Options.DefaultHighlightColorIndex = wdYellow
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Highlight = True
With Selection.Find
.Text = StrFnd
.Replacement.Text = "^&"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Application.ScreenUpdating = True
End Sub
|
|
#2
|
||||
|
||||
|
Start off by hiding all text, then unhiding the paragraphs containing the found text. There is no need to do any looping:
Code:
Sub Demo()
Application.ScreenUpdating = False
StrFnd = InputBox("Find Text with Wildcard")
If Trim(StrFnd) = "" Then Exit Sub
With ActiveDocument.Range
.Font.Hidden = True
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "[!^13]@" & StrFnd & "*^13"
.Font.Hidden = True
.Replacement.Font.Hidden = False
.Replacement.Text = "^&"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Show/hide paragraphs based on upfront decisions
|
miscia76 | Word | 1 | 11-13-2013 04:29 PM |
| Show & hide paragraphs, parts of tables, etc | Preloader | Word | 2 | 10-19-2013 02:37 PM |
add the functionality to show & hide paragraphs, parts of tables, etc
|
pgwolfe | Word | 3 | 09-24-2013 07:58 PM |
| the macro cannot be found or has been disabled | map7 | Word | 10 | 04-10-2012 01:40 PM |
Eliminate paragraph breaks
|
geobruin | Word | 1 | 06-12-2009 06:55 AM |