vba - Create new paragraph style based on another style - not linked style

User avatar
Charles Kenyon
5StarLounger
Posts: 609
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

vba - Create new paragraph style based on another style - not linked style

Post by Charles Kenyon »

This question has two parts.
The first is how to create a new style based upon an existing style using vba and not using the selection object.
The following code creates a new style based on the paragraph style of the selection.

Code: Select all

Private Sub CreateStyle()
    ActiveDocument.Styles.Add Name:="MyStyle", Type:=wdStyleTypeParagraph
 End Sub
The new style is a linked style rather than a paragraph style and is based on the style at the insertion point.

The second part of the question is how to make it a paragraph style.
00 deleteme 9.png
I want it to be based on the Normal style or the Body Text style and both of these are linked styles. However, the code creates a linked style even when the base style is not.
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78402
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: vba - Create new paragraph style based on another style - not linked style

Post by HansV »

1) I don't think it's possible to create a new style in VBA that does not take on the characteristics of the selected text.
You can change the style it is based on to another style than that of the selection though, by setting its BaseStyle property.
2) Set the Type of the style to wdStyleTypeParagraphOnly instead of wdStyleTypeParagraph.

Example:

Code: Select all

    Dim stl As Style
    Set stl = ActiveDocument.Styles.Add(Name:="MyStyle", Type:=wdStyleTypeParagraphOnly)
    stl.BaseStyle = ActiveDocument.Styles(wdStyleListParagraph)
End Sub
3) You can prevent linked styles from being applied as a character style by using

Code: Select all

    Application.RestrictLinkedStyles = True
This is an application-wide setting though, so it's probably not acceptable to use it in macros for general use.
Best wishes,
Hans

User avatar
Charles Kenyon
5StarLounger
Posts: 609
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: vba - Create new paragraph style based on another style - not linked style

Post by Charles Kenyon »

Thank you, Hans. I was missing the wdStyleTypeParagraphOnly.

When displaying the Styles Pane, the mouseover still shows the original Based On but the style does have the characteristics of the newly assigned based on and if you go to the Modify Style dialog it shows the newly assigned base. OK from that point corrects the mouseover information.

User avatar
Charles Kenyon
5StarLounger
Posts: 609
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: vba - Create new paragraph style based on another style - not linked style

Post by Charles Kenyon »

Running the following works fine and the base style does show up in the mouseover.

Code: Select all

Private Sub CreateNewStyle(strStyleNameBase As String, strNewName As String, iLevel As Long)
    Let strNewName = strNewName & iLevel
    ActiveDocument.Styles.Add Name:=strNewName, Type:=wdStyleTypeParagraphOnly
    ActiveDocument.Styles(strNewName).BaseStyle = ActiveDocument.Styles(strStyleNameBase)
End Sub

Private Sub tester()
    CreateNewStyle "Normal", "MyStyle ", 3 
End Sub
When tester is run, the new style "MyStyle 3" is created based on the Normal style even though the insertion point is in a paragraph using a different style. The anomaly with the mouseover was observed when I was creating and then changing the base using the immediate window.

All working fine. Thank you again.