Solved

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

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

839 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