Bug in ActiveDocument.Range.Font?

D

David Turner

I'm trying to check whether files contain certain font attributes (hidden,
smallcaps, etc.) anywhere within the whole document or in its sections but
the code below seems to return wdUndefined (9999999) irrespective of whether
the document has any hidden text or no at all.
Strangely enough, it works for short test documents but when I try it on a
"real" document 20 or 30 pages long, it always returns .Font.Hidden = 9999999
even though running a Find for hidden text shows that the document has no
text with hidden font..
It also fails at Section level but works properly at Paragraph level.
Is this a bug or am I doing something wrong?
Many thanks.

David

Sub CheckForHidden()
Dim rDoc As Range
Set rDoc = ActiveDocument.Range
With rDoc
Select Case .Font.Hidden
Case 0
MsgBox "none of the text is hidden"
Case -1
MsgBox "all of the text is hidden"
Case Else
MsgBox "some of the text is hidden"
End Select
End With
End Sub
 
D

Doug Robbins - Word MVP

I can replicate the issue using your code, but cannot explain it.

The following, which goes through a document paragraph by paragraph appears
to correctly handle the situation where the hidden attribute is applied to
the whole of a paragraph in a document of a couple of hundred pages.

However, as the hidden font attribute can be applied to just one character
in a document, it would really be necessary to iterate through all of the
characters. As the code will stop running as soon as it hits the "Some"
situation, that may not be too bad if there is a mixture/ On a long
document in which all of the text is formatted in the same way as far as the
hidden attribute is concerned, it will however take a while to run as it has
then to go through the whole document character by character.

Dim i As Long
Dim Flag As Long
Flag = 0
Dim strResult As String
Dim rDoc As Range
With ActiveDocument
For i = 1 To .Paragraphs.Count
Set rDoc = .Paragraphs(i).Range
With rDoc
Select Case .Font.Hidden
Case 0
If Flag = 2 Then
strResult = "Some"
Exit For
ElseIf Flag = 0 Or Flag = 1 Then
strResult = "None"
End If
Flag = 1
Case -1
If Flag = 0 Or Flag = 2 Then
strResult = "All"
ElseIf Flag = 1 Then
strResult = "Some"
Exit For
End If
Flag = 2
End Select
End With
Next i
End With
MsgBox strResult & " of the text is hidden"


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
D

David Turner

Thanks for your confirmation.
With a 5000-paragraph document, your code takes about 5 minutes to run
whereas a search for hidden text only takes a split second so I guess that's
the way to go when testing for font attributes.
I was hoping that If ActiveDocument.Range.Font. Hidden might magically
produce some sort of "If InStr" performance, but obviously not.
Wonder why cycling through paragraphs takes so long?
For i = 1 To .Paragraphs.Count counts 5000 paragraphs (all in tables) more
or less instantaneously.

David
 

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