How to tell if User Defined Style is in use or not?

D

DannyE

Hi Guys,

I have a template which contains a bunch of bespoke Styles which ar
copied to the current document via a macro. If someone using th
template goes out of sequence and runs a macro that requires th
presence of one of thses Styles but it hasn't been copied across yet
how can I do a test to see if is in use or not yet?

For example I was using a toolbar to apply one of the styles and the
got confused when nothing happened, it was simply because I hadn'
copied the styles across yet...

Any help would be very gratefully recieved as I am new to writin
vba...

Many thanks,

Da
 
S

Shauna Kelly

Hi Dan

The real answer is here is to avoid the problem. A template is best
constructed with the styles in the template. Then, when the user does File >
new and creates a new document from your template, the new document will
inherit the template's styles. Furthermore, copying numbered or bulleted
styles using a macro rarely works absolutely accurately.

However, it is prudent in any code to check for the existence of a custom
style before using it. Something like this:

Public Sub MyMainSub()

Dim oStyle As Word.Style

If StyleExists("MyStyleName") Then
'do stuff involving style MyStyleName
Else
'do stuff to cope when the style
'doesn't exist in the Active Document
End If

End Sub



Private Function StyleExists(sStyleName) As Boolean
'Purpose: Determine whether a style named
'sStyleName exists in the ActiveDocument

Dim oStyle As Word.Style

'See if the style exists by trying to get
'hold of it
On Error Resume Next
Set oStyle = ActiveDocument.Styles(sStyleName)

'set the return value
StyleExists = Not oStyle Is Nothing

'Clean up
Set oStyle = Nothing

End Function



Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
D

DannyE

Hi Shauna,

Your suggestion works perfectly... Thank-you :)

My problem came from the fact that the files I am going to be workin
with aren't new ones but ones once created in Wordperfect circa 97/9
so styles and other formatting weren't implimented very consistently i
them. Now I am doing a conversion to Word and putting in tweaks to ge
rid of incompatible field codes and stuff... real headachy :)

But thank-you very much for your help, I'm a little nearer my goal no
:)

Best regards,

Da
 
D

DannyE

Hi Shauna,

Thanks again... I am already having fun striping out all fields tha
have been left over from the original docs but I am running int
headaches now where styles weren't used properly and list numberin
hasn't be styled consistently or applied consistently...

Definately not the friendliest project I have been involved with!!

Thanks again for your help :)

Da
 

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