Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / December 2007

Tip: Looking for answers? Try searching our database.

error when adding a new style

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Associates - 19 Dec 2007 23:24 GMT
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
Klaus Linke - 20 Dec 2007 00:18 GMT
> 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

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.