error when adding a new style

A

Associates

Hi,

I was trying to add a new style into an existing word document (for office
word 2003). I want the following code to run when a user opens it so that it
would add this new marginstyle beforehand. So i call that function in the
AutoOpen macro. However, i got an error message.

Here is the code
Sub marginstyle()
Dim styleName As String
Dim oStyle As Style

styleName = "Section Document Heading 1"
'Create/Setup the style
For Each oStyle In ActiveDocument.Styles
If oStyle.NameLocal = styleName Then GoTo Setup
Next oStyle
ActiveDocument.Styles.Add Name:=styleName, Type:=wdStyleHeading1

Setup:
'With ActiveDocument.Styles(styleName)
' .AutomaticallyUpdate = False
' .BaseStyle = ""
' .NextParagraphStyle = "Normal"
'End With
With ActiveDocument.Styles(styleName).Font
.Size = 16
.ColorIndex = wdGreen
End With

MsgBox "Style setup completed"
End Sub

Sub AutoOpen()
Call marginstyle
End Sub

The error is at "ActiveDocument.Styles.Add Name:=styleName,
Type:=wdStyleHeading1". The description of it is "one of the values passed to
this method or property is out of range".

Any ideas?

Thank you in advance
 
K

Klaus Linke

The error is at "ActiveDocument.Styles.Add Name:=styleName,
Type:=wdStyleHeading1". The description of it is "one of the values
passed to this method or property is out of range".


Hi,

The VBA help for the Add method of the Styles object has a list of the possible wdStyleType constants.
You likely want to use Type:=wdStyleTypeParagraph.

If you then want the style to have an outline level of 1, as I assume, you could use

With ActiveDocument.Styles(styleName)
.ParagraphFormat.OutlineLevel = wdOutlineLevel1
...

.... and/or base it on the built-in "Heading 1" style, so it inherits that setting:

With ActiveDocument.Styles(styleName)
' ...
.BaseStyle = ActiveDocument.Styles(wdStyleHeading1)


Regards,
Klaus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top