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
 
Ad

Advertisements

macropod

Microsoft MVP
Joined
Mar 2, 2012
Messages
508
Reaction score
46
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.
 

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