Finding Hidden text in word VBA

Joined
Feb 6, 2017
Messages
1
Reaction score
0
Hello,

I'm using the word VBA macro to show the number of hidden text in the word document, the below code works perfect fine, however if the last hidden text found in the document meets this criteria (entire paragraph in a table cell was hidden) then it loops again and again, not coming out of the loop.

Please help me.

Code:
Sub Test1()
Dim oRg As Range
Set oRg = ActiveDocument.Range
ActiveWindow.View.ShowHiddenText = True
With oRg.Find
.Format = True
.Forward = True
.Font.Hidden = True
a:
If .Execute Then
oRg.Select
With ActiveWindow.View
.ShowHiddenText = False
.ShowAll = True
End With
MsgBox oRg.Text
GoTo a:
Else
ActiveWindow.View.ShowHiddenText = False
End If
End With
End Sub
 

macropod

Microsoft MVP
Joined
Mar 2, 2012
Messages
580
Reaction score
50
Try something along the lines of:

Code:
Sub Demo()
Application.ScreenUpdating = False
Dim bShow As Boolean
bShow = ActiveWindow.View.ShowHiddenText
ActiveWindow.View.ShowHiddenText = True
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .Font.Hidden = True
    .Execute
  End With
  Do While .Find.Found
    MsgBox .Text
    If .Information(wdWithInTable) = True Then
      If (.End = .Cells(1).Range.End) Or (.End + 1 = .Cells(1).Range.End) Then
        .Start = .Cells(1).Range.End + 1
      End If
      If (.End = .Tables(1).Range.End) Or (.End + 1 = .Tables(1).Range.End) Then
        .Start = .Tables(1).Range.End + 1
      End If
    End If
    If .End = ActiveDocument.Range.End Then Exit Do
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
ActiveWindow.View.ShowHiddenText = bShow
Application.ScreenUpdating = True
End Sub
Note the tests for cell, table and document ends. Note also how the hidden text visibility is restored to its initial state.
 
Joined
Jun 17, 2014
Messages
2
Reaction score
0
Given the topic, I thought I'd offer my standard find-hidden-text solution for those who might wander in here looking for that.

Code:
Public Function FindHiddenText(FindRng As Range) As Range
    '
    'Searches for any hidden text in the specified range and, if found, returns a range that spans only that
    'hidden text.
    '
    'AUTHOR: Peter Straton
    '        (free for use by anyone but, if used essentially as-is, please keep this author credit)
    '
    '*************************************************************************************************************
 
    Dim SearchRng As Range
    Dim SaveInclHdnText As Boolean
 
    Set SearchRng = FindRng.Duplicate
    With SearchRng
        SaveInclHdnText = .TextRetrievalMode.IncludeHiddenText
        .TextRetrievalMode.IncludeHiddenText = True '1. Is necessary for finding hidden text, along with...
     
        With .Find
            'First, reset all (other) Find options
         
            .ClearFormatting: .Replacement.ClearFormatting: .Replacement.Text = "": .Forward = True
            .Wrap = wdFindStop: .MatchAllWordForms = False: .MatchCase = False
            .MatchSoundsLike = False: .MatchWholeWord = False: .MatchWildcards = False
     
            'Set the find-hidden-text options and search for any hidden text
         
            .Text = "" 'Find anything
            .Format = True                          '2. ...also necessary for finding hidden text, along with...
            .Font.Hidden = True                     '3. ...this.
            .Execute
         
            If .Found Then
                Set FindHiddenText = SearchRng 'The resulting (updated) found-range span
'                Stop 'DEMO/DEBUG ONLY
            Else
                'Not found, so return Nothing
'                Stop 'DEMO/DEBUG ONLY
            End If
        End With
     
        .TextRetrievalMode.IncludeHiddenText = SaveInclHdnText 'Restore original setting
    End With
End Function 'FindHiddenText
 

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

Top