Share via

How to fix a style creation fault in Word when using VBA?

CRUTZEN Jean-Pierre 20 Reputation points
2025-07-28T10:50:13.91+00:00

The given task is the following: create or update a style in a Word document with a VBA function.

The sequence is as follows:

  • verify if the style is existing
  • add a new style or update existing style.

All is part of a class.

  1. Detection
'
'   Detect existing style
'
'	Arguments:
'	+	Name	the name of the style
'
'   Note: poStyles declared as Styles in the class : private object Styles
'
Private Function StyleExists(Name As String) As Boolean
    Dim oStyle As Style
    '
    StyleExists = False
    For Each oStyle In poStyles
        If oStyle.NameLocal = Name Then
            StyleExists = True
            Exit For
        End If
    Next oStyle
End Function
  1. Creation or update
'
'   Create or update a paragraph style
'
'	Arguments:
'	+	clsStyle	Class to handle a single style
'	+	oStyleName	Name of the style
'
Private Function DoOneParagraph(clsStyle As TSpecStyle, oStyleName As String) As Style
    Dim oStyle As Style
    Dim NoFault As Boolean
    '
    If Not StyleExists(oStyleName) Then
        Set oStyle = poStyles.Add(oStyleName, wdStyleTypeParagraph)
    Else
        Set oStyle = poStyles(oStyleName)
    End If
    NoFault = clsStyle.Prepare(oStyle)
    '
    If Not NoFault Then
        Set DoOneParagraph = Null
    Else
        Set DoOneParagraph = oStyle
    End If
End Function

What is happening ?

For a few specific styles, the instruction

Set oStyle = poStyles.Add(oStyleName, wdStyleTypeParagraph)

Set oStyle = poStyles.Add(oStyleName, wdStyleTypeParagraph)

raises an error "Run-time error '5173':

This style name already exists or is reserved for a built-in style.

Although the style is confirmed as not existing by the test function "StyleExits()". Its name is "Help6B1".

What can I do next ?

I move the next statement in debugging mode to

Set oStyle = poStyles(oStyleName)

continue the execution (F5). The macro continues to run.

Intermediate conclusion

The style effectively exists but is not detected. The reason is not understandable to me. The style is not displayed in the list of styles in Word (tab Home, group Styles) which is consistant.

What did I check ?

In the StyleExists function, I checked that the "For Each" was running through all the styles. The total count of styles, including the built-in styles is 415, and the "For Each loops 415 times if the style is not found.

What else ?

I have added a error trapping to help the function to continue running, but this error trapping, compared to the instructions and helps, seems also not be we working.

The code looks like:

Private Function DoOneParagraph(clsStyle As TSpecStyle, oStyleName As String, x As Integer) As Style
    Dim oStyle As Style
    Dim NoFault As Boolean
    '
    If Not StyleExists(oStyleName) Then
        On Error Resume Next
        Set oStyle = poStyles.Add(oStyleName, wdStyleTypeParagraph)
        If Err.Number <> 0 Then
            If Err.Number = 5173 Then
                Set oStyle = poStyles(oStyleName)
                Err.Clear
            Else
                NoFault = False
            End If
        End If
    Else
        Set oStyle = poStyles(oStyleName)
    End If
    NoFault = clsStyle.Prepare(oStyle)
    '
    If Not NoFault Then
        Set DoOneParagraph = Null
    Else
        Set DoOneParagraph = oStyle
    End If
End Function

But this error handling is also unexpectedly not working. This is another subject.

Finally

Did somebody else face the same type of issue ? Or did I write a mistake in the code ?

Thnak you.

Microsoft 365 and Office | Word | For business | Windows
0 comments No comments

Answer accepted by question author

Anonymous
2025-07-28T14:52:10.5833333+00:00

Hi  @CRUTZEN Jean-Pierre

Welcome to Microsoft Q&A forum 

Regarding your concern, I suggest to check: 

1.Enhance StyleExists to Use a More Robust Check 

Instead of relying solely on For Each, try accessing the style directly via poStyles(oStyleName) with error handling to detect its existence, as this may catch styles missed by enumeration. 

Private Function StyleExists(Name As String) As Boolean
    Dim oStyle As Style
    On Error Resume Next
    Set oStyle = poStyles(Name)
    StyleExists = (Not oStyle Is Nothing)
    On Error GoTo 0
End Function

2.Fix DoOneParagraph with Robust Error Handling

Modify DoOneParagraph to handle errors more reliably and debug the issue. Also, ensure NoFault is initialized and errors are logged for clarity. 

Private Function DoOneParagraph(clsStyle As TSpecStyle, oStyleName As String) As Style
    Dim oStyle As Style
    Dim NoFault As Boolean
    NoFault = True ' Initialize to True
    On Error Resume Next
    Set oStyle = poStyles(oStyleName) ' Try accessing style first
    If Err.Number = 0 And Not oStyle Is Nothing Then
        ' Style exists, use it
    ElseIf Err.Number = 5173 Or Err.Number = 5834 Then
        ' Style exists but is inaccessible (e.g., corrupted or template-based)
        Set oStyle = poStyles(oStyleName)
        Err.Clear
    Else
        ' Style does not exist, try adding it
        Err.Clear
        Set oStyle = poStyles.Add(oStyleName, wdStyleTypeParagraph)
        If Err.Number <> 0 Then
            Debug.Print "Error adding style '" & oStyleName & "': " & Err.Number & " - " & Err.Description
            NoFault = False
        End If
    End If
    On Error GoTo 0
    If Not oStyle Is Nothing Then
        NoFault = clsStyle.Prepare(oStyle)
    Else
        NoFault = False
    End If
    If NoFault Then
        Set DoOneParagraph = oStyle
    Else
        Set DoOneParagraph = Nothing ' Use Nothing instead of Null
    End If
End Function

**3.**Debug Style Existence:  

In the VBA Immediate Window, run:  

On Error Resume Next: Debug.Print ActiveDocument.Styles("Help6B1").InUse, ActiveDocument.AttachedTemplate.Styles("Help6B1").InUse: On Error GoTo 0

4.Test in a New Document:  

Create a new .docx document and test the code to rule out document or template corruption. 


If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread. 

Was this answer helpful?

1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.