Steven,
I've found the "article" I referenced earlier; it's the VBA help topic on
the .InUse property. (~smacks forehead with palm~) This topic reads in part:
Remarks
This property doesn't necessarily indicate whether the style is currently
applied to any text in the document. For instance, if text that's been
formatted with a style is deleted, the InUse property of the style remains
True. For built-in styles that have never been used in the document, this
property returns False.
Accordingly, I'd agree with your assessment of what "InUse" means, and I
believe this is pretty much the way the help explains it as well.
Unfortunately, it's hard to tell from Andreas's initial post if the goal is
to list the styles that Word identifies as "InUse" or if it is to list those
styles that are actually applied someplace in the document. Assuming the
latter then the following code should do the trick (complete with a bit of
error handling and a few other bells & whistles):
Option Explicit
Dim StylesArray() As Variant
Dim SourceDoc As Document
Sub ListStylesInUse()
If Documents.Count = 0 Then
MsgBox "There are no open documents.", vbCritical, "List Style Error"
Else
Set SourceDoc = ActiveDocument
If SourceDoc.ProtectionType <> wdNoProtection Then
MsgBox "Document protected. Cannot list styles.", vbCritical,
"List Style Error"
Else
Dim ParaCount As Integer
Dim myPara As Paragraph
Dim ParaIdx As Integer
Dim ThisParaStyleName As String
Dim ThisParaStyleFontName As String
Dim ThisParaStyleFontSize As String
Dim StyleCount As Integer
Dim i As Integer
Dim bMatchFound As Boolean
Dim StyleDetails As String
ParaCount = SourceDoc.Paragraphs.Count
For ParaIdx = 1 To ParaCount
bMatchFound = False
Set myPara = SourceDoc.Paragraphs(ParaIdx)
ThisParaStyleName = myPara.Style.NameLocal
For i = 1 To StyleCount
If ThisParaStyleName = StylesArray(0, i) Then
bMatchFound = True
Exit For
End If
Next i
If Not bMatchFound Then
StyleCount = StyleCount + 1
With myPara.Style
ThisParaStyleFontName = .Font.Name
ThisParaStyleFontSize = .Font.Size
End With
ReDim Preserve StylesArray(2, 1 To StyleCount)
StylesArray(0, StyleCount) = ThisParaStyleName
StylesArray(1, StyleCount) = ThisParaStyleFontName
StylesArray(2, StyleCount) = ThisParaStyleFontSize
End If
Next ParaIdx
StyleDetails = fcnBuildStyleDetails(StyleCount)
BuildTargetDoc (StyleDetails)
End If
End If
End Sub
Private Function fcnBuildStyleDetails(StyleCount As Integer) As String
Dim n As Integer
Dim Temp As String
Temp = "Styles found in " & SourceDoc.Name & vbCr
Temp = Temp & StyleCount & " styles, as follows:" & vbCr & vbCr
For n = 1 To StyleCount
Temp = Temp & "Style " & n & " -" & vbCr
Temp = Temp & "Style Name: " & StylesArray(0, n) & vbCr
Temp = Temp & "Font Name: " & StylesArray(1, n) & vbCr
Temp = Temp & "Font Size: " & StylesArray(2, n) & vbCr & vbCr
Next n
fcnBuildStyleDetails = Temp
End Function
Private Sub BuildTargetDoc(StyleDetails As String)
Dim TargetDoc As Document
Set TargetDoc = Documents.Add
With TargetDoc.Range
.Text = StyleDetails
.Collapse wdCollapseEnd
.MoveEnd wdCharacter, 1
End With
End Sub
Note that on a *REALLY BIG* document this code can take a long time to
execute because it has to run through a lot of paragraphs. A couple of
hundred paragraphs is probably OK, but I pointed it at a document that
contained 2000+ paragraphs and then went out for coffee... Maybe some other
Clever Dick in here has a faster solution.
*** This code is supplied 'as is' with no warrantee of fitness for a
particular use. E&OE. Caveat emptor. Res ipsa. Etc., etc. ***
--
Cheers!
Gordon
Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.