Find/replace styles

S

Sandra

Hello everyone,
I am trying to write a macro that replaces automatically
old styles with new ones with different names. My problem
is that if one of the old styles the macro looks for does
not exist in the document, it gives an error message and
stops. Help please.
Thanks in advance.
 
W

ward

Before I do any action on a (supposed) style, I first
check if it does exist with the code below:

greetz
Ward

---Code-----

Private Function StyleExists(StyleName As String) As
Boolean
'Returns true if the specified style exists

Dim Sty As Style

If Application.Documents.Count = 0 Then Exit Function

StyleExists = False
For Each Sty In ActiveDocument.Styles
If Sty.NameLocal = StyleName Then StyleExists =
True: Exit For
Next

'Clean up
Set Sty = Nothing

End Function
 
J

Jezebel

With a lot of styles, that's a very slow way to do it. Much simpler is --

Private Function StyleExists(StyleName As String) As Boolean
'Returns true if the specified style exists

Dim Sty As Style

on error resume next
Set Sty = ActiveDocument.Styles(StyleName)
on error goto 0

StyleExists = not (Sty is nothing)

End Function
 

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