Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / March 2005

Tip: Looking for answers? Try searching our database.

checking if a style exists in a styles collection

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
jimi_hendricks - 10 Mar 2005 13:05 GMT
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...
Stefan Blom - 10 Mar 2005 14:16 GMT
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

Signature

Stefan Blom

> Hi,
>
[quoted text clipped - 3 lines]
>
> I'm just not sure of the syntax...
jimi_hendricks - 10 Mar 2005 14:29 GMT
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!

> Something like this should work:
>
[quoted text clipped - 18 lines]
> >
> > I'm just not sure of the syntax...
Stefan Blom - 10 Mar 2005 15:11 GMT
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?

Signature

Stefan Blom

> Thanks Stefan,
>
[quoted text clipped - 28 lines]
> > >
> > > I'm just not sure of the syntax...
Jay Freedman - 10 Mar 2005 16:08 GMT
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.

Signature

Regards,
Jay Freedman
Microsoft Word MVP          FAQ: http://word.mvps.org

> Thanks Stefan,
>
[quoted text clipped - 31 lines]
>>>
>>> I'm just not sure of the syntax...
jimi_hendricks - 10 Mar 2005 16:29 GMT
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

> 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
[quoted text clipped - 62 lines]
> >>>
> >>> I'm just not sure of the syntax...
Jay Freedman - 10 Mar 2005 16:48 GMT
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.

Signature

Regards,
Jay Freedman
Microsoft Word MVP          FAQ: http://word.mvps.org

> thanks stefan, thanks jay.
>
[quoted text clipped - 87 lines]
>>>>>
>>>>> I'm just not sure of the syntax...
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.