Link to home
Start Free TrialLog in
Avatar of jtaylerg
jtaylerg

asked on

VBA - Word 2003: Create a Style if None Exists.

Hi There,

We've set up our templates to use a style called "DocID".  In older docs, it was a character style, in the new ones: paragraph.  We also have docs that do not have the style at all.

We want to shoot for consistency.

I'm trying to write a macro that will create the DocID paragraph style if none exists.  If the style name is in the collection, I want to delete it and add the new paragraph style.  

Unfortunately, my error handler doesn't seem to be working.  I suspect I'm using the wrong one, or I have it in the wrong location.  

If the style exists, I get run time error 5173.  "Style name already exists or is reserved for a built-in style."

Here's the code I'm trying:

On Error GoTo ErrSTYLE_NAME  
    Set MyStyle = ActiveDocument.Styles.Add(Name:="DocID", _
        Type:=wdStyleTypeParagraph)
    With MyStyle.Font
        .Size = 7
    End With
    With MyStyle.ParagraphFormat
        .SpaceBefore = 6
        .Alignment = wdAlignParagraphLeft
    End With
        GoSub NDV

ErrSTYLE_NAME
    ActiveDocument.Styles("DocID").Delete

Any suggestions?

THANKS!
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jtaylerg
jtaylerg

ASKER

Thank you so very much.

It's been such a hectic week -- I haven't had a chance to follow up.

Thanks -- JTG

OK. Thanks and good luck.