Does Style Exist in Document?

A

Angie M.

Hello, I'm trying to write a macro that finds out if a particular style
(MyBodyText) exists in the active document. Can you help? Thank you
 
B

Bear

Angie:

Here's my version. I examine every style in the document for a match and set
the flag.

Public Function StyleExists(strStyleName As String) As Boolean

' Determines whether or not the target style exists in the
' active document.

Dim objStyle As Style

StyleExists = False
For Each objStyle In ActiveDocument.Styles
If objStyle.NameLocal = strStyleName Then
StyleExists = True
Exit For
End If
Next objStyle

End Function



You'd call this using a statement like:

If StyleExists("BO") = True Then etc., etc.
 
A

Angie M.

Thanks Bear. I have a question. I'm using this code as part of another
macro. My macro first needs to check to see if BodyText is available for
use, if so it exits my macro, if not it creates the BodyText style. Is there
a way to get around looping through all the styles? I was hoping there was a
way to just check for one partcular style. Thanks for your help!
 
B

Bear

Angie:

That code really doesn't take up much execution time. I don't even think
about it, I just use it like it was a built-in function. However, I think
this will also work without examining all the styles:

On Error Resume Next
objStyle = ActiveDocument.Styles("ribbo")
On Error GoTo 0
If objStyle Is Nothing Then
MsgBox "Style is missing."
Else
MsgBox "Style is present."
End If

You have to set On Error Resume Next because if the style doesn't exist the
objStyle = ActiveDocument.Styles("xxx") statement fails. I THINK On Error
GoTo 0 reestablishes error handling. If you already have an error handler,
then use it instead, writing On Error GoTo [Whatever label you've created for
the handler].

If the objStyle = statement errors out because the style doesn't exist, the
objStyle object will be as it started -- Nothing.

I don't like it, but it should work for you.

Bear
 
J

Jose AP

Angie:

I am doing a macro in Excel to copy Excel charts and comments into an
existing word document. I am trying to add a Table of Contents at the
beginning of the word document but I don't know how to create the BodyText
styles in the opened document (to use them later for the creation of the
table of contents).

I can see from your emails with Bear how I check which BodyTexts are
available in the word document. However I am not sure how to create them if
they aren't.

Would you be able to help?

Thanks

Jose AP


Angie M. said:
THANKS BEAR!

Bear said:
Angie:

That code really doesn't take up much execution time. I don't even think
about it, I just use it like it was a built-in function. However, I think
this will also work without examining all the styles:

On Error Resume Next
objStyle = ActiveDocument.Styles("ribbo")
On Error GoTo 0
If objStyle Is Nothing Then
MsgBox "Style is missing."
Else
MsgBox "Style is present."
End If

You have to set On Error Resume Next because if the style doesn't exist the
objStyle = ActiveDocument.Styles("xxx") statement fails. I THINK On Error
GoTo 0 reestablishes error handling. If you already have an error handler,
then use it instead, writing On Error GoTo [Whatever label you've created for
the handler].

If the objStyle = statement errors out because the style doesn't exist, the
objStyle object will be as it started -- Nothing.

I don't like it, but it should work for you.

Bear
 

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