checking if a style exists in a styles collection

J

jimi_hendricks

Hi,

Silly, i know, but how do i check if a particular style exists in the styles
collection? i.e. i know the name i am looking for (e.g. "foobar"), i just
want to know if a style by that name exists already or not...

I'm just not sure of the syntax...
 
S

Stefan Blom

Something like this should work:

Sub Test()
Dim s As Style
For Each s In ActiveDocument.Styles
If s.NameLocal = "style name here" Then
MsgBox "Style exists"
Exit For
End If
Next
'More code here...
End Sub
 
J

jimi_hendricks

Thanks Stefan,

I wondered if it was possible to directly check for the presence of a
particular style, rather than looping through the styles collection? I just
thought this would be more efficient, but wasnt sure if it was possible.

Any thoughts?

Thanks again!
 
S

Stefan Blom

Not that I know of, but I'm not using the latest version of Word. Have
you checked with Word VBA Help for your version?
 
J

Jay Freedman

For the two specific collections Bookmarks and Tasks, there's an Exists()
function (look it up in the VBA help). For all other collections, either you
iterate through the collection as Stefan said, or you can try to use the
style and catch the error if it doesn't exist, something like this:

Sub foo()
On Error Resume Next
Selection.Paragraphs(1).Style = _
ActiveDocument.Styles("foobar")
If Err.Number <> 0 Then
If Err.Number = 5941 Then ' not exist
' let VBA handle any further errors
On Error GoTo 0
Selection.Paragraphs(1).Style = _
ActiveDocument.Styles("Body Text")
Else ' other error
MsgBox Err.Description
Exit Sub
End If
End If

' let VBA handle any further errors
On Error GoTo 0

' more code...
End Sub

Personally, I prefer the iteration method. It's quick enough that it won't
slow the macro noticeably. In contrast, error handling like the above can be
hard to get right.
 
J

jimi_hendricks

thanks stefan, thanks jay.

I have implemented the iteration method now.

My intention is to have a seperate .dot file for each of any number of
users, and each .dot file will store a single style to be loaded in for an
individual. The template should never be manipulated directly, only used as a
place to save the style, for that reason there should never be too many
styles to iterate through, just the built-in ones and 'foobar' :)

incidentally, there must be a reason why some collections have an 'exists()'
method, but others don't... are these things just a matter of developer
demand or is there an explicit reason why the styles collection has no
'exists()' method?

thanks
 
J

Jay Freedman

If there's a reason for not having .Exists() for other collections, MS
haven't shared it with us mere mortals. I consider it a minor miracle that
any built-in collection in VBA has it. I've never noticed that "developer
demand" -- including that from MVPs -- has much to do with what gets into
VBA.

If you ever get around to writing your own collection classes, it's
certainly worth including a .Exists method for them.
 

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