Solved

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

Posted on 2006-11-10
3
1,445 Views
Last Modified: 2007-12-19
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!
0
Comment
Question by:jtaylerg
  • 2
3 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 17917114
You could write a function to check it the style exists. You won't need to rely on error trapping.

Function StyleExists(strName As String, Doc As Document) As Boolean
    Dim sty As Style
   
    For Each sty In ActiveDocument.Styles
        If LCase$(strName) = LCase$(sty.NameLocal) Then
            StyleExists = True
            Exit Function
        End If
    Next sty
End Function

0
 

Author Comment

by:jtaylerg
ID: 17965677
Thank you so very much.

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

Thanks -- JTG

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 17969512
OK. Thanks and good luck.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now