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:="Doc ID", _
Type:=wdStyleTypeParagraph )
With MyStyle.Font
.Size = 7
End With
With MyStyle.ParagraphFormat
.SpaceBefore = 6
.Alignment = wdAlignParagraphLeft
End With
GoSub NDV
ErrSTYLE_NAME
ActiveDocument.Styles("Doc ID").Delet e
Any suggestions?
THANKS!
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(
Type:=wdStyleTypeParagraph
With MyStyle.Font
.Size = 7
End With
With MyStyle.ParagraphFormat
.SpaceBefore = 6
.Alignment = wdAlignParagraphLeft
End With
GoSub NDV
ErrSTYLE_NAME
ActiveDocument.Styles("Doc
Any suggestions?
THANKS!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
OK. Thanks and good luck.
ASKER
It's been such a hectic week -- I haven't had a chance to follow up.
Thanks -- JTG