Programatically Finding Hidden text in MS Word document

D

david

Does anyone know of a way to programatically find the
hidden text in a MS Word Document?

The idea is that I need to rifle through all of the .doc
files in a directory and write out all hidden text to a
separate "log" file.

I'm fine opening all the files and logging the info, I'm
just not sure how to detect the hidden text.

I've seen how (in Word) to do an Edit | Replace...
pressing the "more" button to get to the "format" button
and with the cursor in the "Find what:" text box
select "hidden text" from the Font... dialog to do "Find
Next"'s for each piece of hidden text interactively, so
this gives me hope that it can be done programatically.

Any ideas?
regards,
- dw
 
J

Jay Freedman

Hi, David,

There are two requirements for finding hidden text programmatically. First,
the hidden text must be visible on screen, which is ensured by setting
ShowHiddenText = True at the beginning of the macro. Second, you must set up
the Find parameters .Format = True and .Font.Hidden = True.

Dim myRange As Range
ActiveDocument.ActiveWindow _
.View.ShowHiddenText = True
Set myRange = ActiveDocument.Range
With myRange.Find
.ClearFormatting
.Forward = True
.Wrap = wdFindStop
.Text = ""
.Format = True
.Font.Hidden = True
Do While .Execute
MsgBox myRange.Text
Loop
End With

Each time the Find is successful, it redefines the extent of myRange to
cover the found (hidden) text, and the .Execute method returns True. When
the Find finally fails after the last occurrence in the document, .Execute
returns False and the loop exits.

Instead of the MsgBox statement, you'll substitute code for writing to the
log file.
 

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