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

RE: Does Style Exist in Document? by Bear

Bear
Thu Jun 29 17:40:01 CDT 2006

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.


RE: Does Style Exist in Document? by AngieM

AngieM
Thu Jun 29 18:51:02 CDT 2006

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!

"Bear" wrote:

> 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.
>

RE: Does Style Exist in Document? by Bear

Bear
Thu Jun 29 19:17:01 CDT 2006

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

RE: Does Style Exist in Document? by AngieM

AngieM
Thu Jun 29 19:43:01 CDT 2006

THANKS BEAR!

"Bear" wrote:

> 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

RE: Does Style Exist in Document? by Jose

Jose
Mon Jul 03 07:20:02 CDT 2006

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." wrote:

> THANKS BEAR!
>
> "Bear" wrote:
>
> > 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