Solved

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

Posted on 2006-11-10
3
1,448 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

910 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

25 Experts available now in Live!

Get 1:1 Help Now