Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-10-2017, 05:41 PM
jplat jplat is offline Macro to hide all paragraphs that do not have the found result Windows 10 Macro to hide all paragraphs that do not have the found result Office 2016
Novice
Macro to hide all paragraphs that do not have the found result
 
Join Date: Sep 2017
Posts: 1
jplat is on a distinguished road
Default Macro to hide all paragraphs that do not have the found result

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

Reply With Quote
  #2  
Old 09-10-2017, 09:13 PM
macropod's Avatar
macropod macropod is offline Macro to hide all paragraphs that do not have the found result Windows 7 64bit Macro to hide all paragraphs that do not have the found result Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to hide all paragraphs that do not have the found result 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
Macro to hide all paragraphs that do not have the found result 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
Macro to hide all paragraphs that do not have the found result Eliminate paragraph breaks geobruin Word 1 06-12-2009 06:55 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:45 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft