G
Greg Maxey
AFAIK, and unlike with bookmarks and tasks, there is no "Exists" procedure for determining if a Style is present in a document.
One way I can do this is:
Sub Test1()
Dim oStyle As Style
Dim styName As String
styName = "Normal"
For Each oStyle In ActiveDocument.Styles
If oStyle.NameLocal = styName Then
MsgBox styName & " style exists in this document."
Exit Sub
End If
Next oStyle
MsgBox "Style not found in this document."
End Sub
I realize that for the purpose of determining if a style exists that the code above works fine and answers the question with most documents in the bat of the eye. Yet it seems inefficient and if there were say a few thousand or so something or another then rather than looking at each one it makes more sense to just call out the item in question and see if responds. I was cobbled together the following code using error handling. I figure if I attempt so action with a style that doesn't exists then it will immediately throw an error and Bob's your uncle.
Sub Test2()
On Error GoTo Handler
Dim styName As String
styName = "Normal"
Debug.Print ActiveDocument.Styles(styName).NameLocal
MsgBox styName & " style exists in this document."
Exit Sub
Handler:
If Err.Number = 5941 Then
MsgBox "Style not found in this document."
Err.Clear
End If
End Sub
As many of you know I am not a purist and have no formal training in VBA. I sheepishly admit that I am still as dumb as a box of rocks wrt most of the technical aspects of the object model (whatever that means) ;-).
My questions. Is there anything unsound or fundamentally wrong with the approach used in Sub Test2?
One way I can do this is:
Sub Test1()
Dim oStyle As Style
Dim styName As String
styName = "Normal"
For Each oStyle In ActiveDocument.Styles
If oStyle.NameLocal = styName Then
MsgBox styName & " style exists in this document."
Exit Sub
End If
Next oStyle
MsgBox "Style not found in this document."
End Sub
I realize that for the purpose of determining if a style exists that the code above works fine and answers the question with most documents in the bat of the eye. Yet it seems inefficient and if there were say a few thousand or so something or another then rather than looking at each one it makes more sense to just call out the item in question and see if responds. I was cobbled together the following code using error handling. I figure if I attempt so action with a style that doesn't exists then it will immediately throw an error and Bob's your uncle.
Sub Test2()
On Error GoTo Handler
Dim styName As String
styName = "Normal"
Debug.Print ActiveDocument.Styles(styName).NameLocal
MsgBox styName & " style exists in this document."
Exit Sub
Handler:
If Err.Number = 5941 Then
MsgBox "Style not found in this document."
Err.Clear
End If
End Sub
As many of you know I am not a purist and have no formal training in VBA. I sheepishly admit that I am still as dumb as a box of rocks wrt most of the technical aspects of the object model (whatever that means) ;-).
My questions. Is there anything unsound or fundamentally wrong with the approach used in Sub Test2?