Computer Forum Forum Index
Register  FAQ Profile Log in to check your private messages Log in Register 

error when adding a new style

 
Post new topic   Reply to topic    Computer Forum Forum Index -> microsoft word vba general
View previous topic :: View next topic  
Author Message
Associates
Guest





PostPosted: Wed Dec 19, 2007 11:24 pm    Post subject: error when adding a new style Reply with quote

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
Back to top
Google
Sponsor





PostPosted: Wed Dec 19, 2007 11:24 pm    Post subject: Advertisement

Back to top
Klaus Linke
Guest





PostPosted: Thu Dec 20, 2007 12:18 am    Post subject: Re: error when adding a new style Reply with quote

Quote:
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
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Computer Forum Forum Index -> microsoft word vba general All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Computer Forum