PDA

View Full Version : finding out whether a style exists



marcelma
09-10-2011, 05:08 AM
Hello,

In order to find out whether a certain style (V_StyleName) is in existence I am using the following loop:

For Each sty In ActiveDocument.Styles
If sty = V_StyleName Then Vb_StyleExists = True
Next sty
Does anybody know a faster and more elegant way for finding out? Is there any VBA function I have not discovered yet?

My intention is to re-define a number of self-defined styles that might or might not exist in a given document.


Thank you very much in advance,
Marcel

macropod
09-10-2011, 05:22 AM
You could use something like:
Sub StyleTest()
Dim StrSty
StrSty = "MyStyle"
On Error GoTo ErrHandler
If Not ActiveDocument.Styles(StrSty) Then Exit Sub
ErrHandler:
If Err.Number = 5941 Then MsgBox "Style named '" & StrSty & "' does not exist."
End Sub

marcelma
09-10-2011, 09:22 AM
Thank you, Paul.

I understand from your answer, that there is no inbuilt VBA function to do the job.

I have thought about the solution you suggested. Your code is faster than mine, but here as well I find it cumbersome to check every style this way before re-defining it (my style list is pretty long) and I can't really build a loop, because each style is defined differently...

Anyway, thank you very much again,
Marcel

Frosty
09-10-2011, 11:23 AM
There is no built in function, although I've used a variant of Paul's suggestion (testing for an error) for years. Just wrap it into a function and you can forget about the lack of elegance to the solution.

Public Function fStyleExists (sName As String, Optional oDoc As Document) As Boolean

On Error Resume Next
'If no document passed, use the Active Document
If oDoc Is Nothing Then
Set oDoc = ActiveDocument
End If
'here is our dummy test
If oDoc.Styles(sName).BuiltIn Then
End If
'now handle our various errors
Select Case Err.Number
Case 0
'no error, it exists
fStyleExists = True
Case 5941
'unable to reference
fStyleExists = False
Case Else
'some other error-- handle differently? (for example, no active document)
fStyleExists = False
End Select
Err.Clear
End Function
And then you can use it elsewhere. Just remember SHIFT+F8 when stepping through the code, and you don't have to look at it anymore.

If fStyleExists(V_StyleName) Then
'redefine it
Else
'create it
End If
There are, of course, many many variants on this solution... but ultimately testing for the error is going to be faster than looping through all the styles... it's just a question of how you apply that approach in your code.

gmaxey
09-10-2011, 03:04 PM
This doesn't answer your specific question but you might find this useful:

http://gregmaxey.mvps.org/Style_Report.htm


Hello,

In order to find out whether a certain style (V_StyleName) is in existence I am using the following loop:

For Each sty In ActiveDocument.Styles
If sty = V_StyleName Then Vb_StyleExists = True
Next sty
Does anybody know a faster and more elegant way for finding out? Is there any VBA function I have not discovered yet?

My intention is to re-define a number of self-defined styles that might or might not exist in a given document.


Thank you very much in advance,
Marcel

Paul_Hossler
09-10-2011, 04:14 PM
My prefered technique:


Option Explicit

Sub test()
MsgBox "Normal?? " & DoesStyleExist("Normal")
MsgBox "ABCDEF1234 ?? " & DoesStyleExist("ABCDEF1234")
End Sub

Function DoesStyleExist(StyleName As String) As Boolean
Dim s As String

s = vbNullString
On Error Resume Next
s = ActiveDocument.Styles(StyleName).NameLocal
On Error GoTo 0

DoesStyleExist = (Len(s) > 0)
End Function



Paul

macropod
09-10-2011, 09:00 PM
Hi marcelma,

Since you're apparently both creating Styles if they don't exist and redefining them if they do, you should be able to work with something like:

On Error Resume Next
ActiveDocument.Styles.Add Name:="MyStyle", Type:=wdStyleTypeParagraph

That way, regardless whether the Style alrady exists beforehand, the 'Add' method guarantees its existence for configuration purposes in whatever code follows.