S
StevenM
It appears that if one searches the styles collection of a document, such as:
Dim oStyle As Style
Dim actDoc As Document
Set actDoc = ActiveDocument
For Each oStyle In actDoc.Styles
If oStyle.InUse Then
The result would be:
(1) all the user defined styles available to the document;
(2) the Default Paragraph Font & Normal; and
(3) all the other styles once used in the document.
The question then is how can one determine which styles actually are being
used within the document?
I came up with the following code:
Function IsStyleInRange(ByVal oStyle As Style, ByVal oRange As Range) As
Boolean
With oRange.Find
.ClearFormatting
.Style = oStyle
.Forward = True
.Format = True
.Text = ""
.Execute
End With
If oRange.Find.Found = True Then
IsStyleInRange = True
Else
IsStyleInRange = False
End If
End Function
And it appears to work for the main body of a document, but I can’t seem to
get it to work with headers (and I haven’t tried footers, or endnotes).
I wonder if someone would be willing to look at the following code. It adds
the word “Title†to a document’s header. Then it calls a (modified)
“IsStyleInRange†twice. The first time the msgbox returns: “True,†but the
second time “False,†why is that?
Sub TestIsStyleInRange()
Dim oRange As Range
Dim oStyle As Style
Dim headerRange As Range
Set oRange = ActiveDocument.Range(Start:=ActiveDocument.Range.Start,
End:=ActiveDocument.Range.End)
Set headerRange = oRange.Sections(1).Headers(wdHeaderFooterPrimary).Range
Set oStyle = headerRange.Style
With headerRange
.Delete
.InsertAfter "Title"
End With
Application.ScreenRefresh
Call IsStyleInRange(oStyle, headerRange)
Set headerRange = Nothing
Set headerRange = oRange.Sections(1).Headers(wdHeaderFooterPrimary).Range
Call IsStyleInRange(oStyle, headerRange)
End Sub
Function IsStyleInRange(ByVal oStyle As Style, ByVal oRange As Range) As
Boolean
With oRange.Find
.ClearFormatting
.Style = oStyle
.Forward = True
.Format = True
.Text = ""
.Execute
End With
If oRange.Find.Found = True Then
IsStyleInRange = True
Else
IsStyleInRange = False
End If
'The next bit is for debugging purposes only
If oStyle = "Header" And InStr(1, oRange, "Title") > 0 Then
MsgBox "The Style We're Looking for is: " & oStyle & vbCr _
& "The Style of the Range is: " & oRange.Style & vbCr _
& "Result is: " & IsStyleInRange
End If
End Function
Steven Craig Miller
Dim oStyle As Style
Dim actDoc As Document
Set actDoc = ActiveDocument
For Each oStyle In actDoc.Styles
If oStyle.InUse Then
The result would be:
(1) all the user defined styles available to the document;
(2) the Default Paragraph Font & Normal; and
(3) all the other styles once used in the document.
The question then is how can one determine which styles actually are being
used within the document?
I came up with the following code:
Function IsStyleInRange(ByVal oStyle As Style, ByVal oRange As Range) As
Boolean
With oRange.Find
.ClearFormatting
.Style = oStyle
.Forward = True
.Format = True
.Text = ""
.Execute
End With
If oRange.Find.Found = True Then
IsStyleInRange = True
Else
IsStyleInRange = False
End If
End Function
And it appears to work for the main body of a document, but I can’t seem to
get it to work with headers (and I haven’t tried footers, or endnotes).
I wonder if someone would be willing to look at the following code. It adds
the word “Title†to a document’s header. Then it calls a (modified)
“IsStyleInRange†twice. The first time the msgbox returns: “True,†but the
second time “False,†why is that?
Sub TestIsStyleInRange()
Dim oRange As Range
Dim oStyle As Style
Dim headerRange As Range
Set oRange = ActiveDocument.Range(Start:=ActiveDocument.Range.Start,
End:=ActiveDocument.Range.End)
Set headerRange = oRange.Sections(1).Headers(wdHeaderFooterPrimary).Range
Set oStyle = headerRange.Style
With headerRange
.Delete
.InsertAfter "Title"
End With
Application.ScreenRefresh
Call IsStyleInRange(oStyle, headerRange)
Set headerRange = Nothing
Set headerRange = oRange.Sections(1).Headers(wdHeaderFooterPrimary).Range
Call IsStyleInRange(oStyle, headerRange)
End Sub
Function IsStyleInRange(ByVal oStyle As Style, ByVal oRange As Range) As
Boolean
With oRange.Find
.ClearFormatting
.Style = oStyle
.Forward = True
.Format = True
.Text = ""
.Execute
End With
If oRange.Find.Found = True Then
IsStyleInRange = True
Else
IsStyleInRange = False
End If
'The next bit is for debugging purposes only
If oStyle = "Header" And InStr(1, oRange, "Title") > 0 Then
MsgBox "The Style We're Looking for is: " & oStyle & vbCr _
& "The Style of the Range is: " & oRange.Style & vbCr _
& "Result is: " & IsStyleInRange
End If
End Function
Steven Craig Miller