Problem with cycling through Styles to change NextParagraphStyle – Run time error 91 – Word vba
EDIT: Tim Williams spotted the problem. I was not using Set for an object variable.
My macro is:
Sub StyleFollowingBodyText()
' Charles Kenyon 15 December 2024
' Set the following style for most QuickStyle paragraph styles to be Body Text
Dim StyleCount As Long
Dim thisStyle As Style
Dim iCount As Long
'
With ActiveDocument
Let StyleCount = .Styles.Count
For iCount = 1 To StyleCount
Let thisStyle = .Styles(iCount)
If thisStyle.QuickStyle = True Then
If thisStyle.Type = wdStyleTypeParagraph Or wdStyleTypeLinked Then
If thisStyle.NameLocal <> "Normal" Then
thisStyle.NextParagraphStyle = "Body Text"
End If
End If
End If
Next iCount
End With
End Sub
and I get this error message when running it:
This error message seems to be somewhat of a “catch-all” and is not that helpful. As pointed out by Tim Williams, the problem is not using the Set command for an object variable.
It is stopping on Let thisStyle = .Styles(iCount)
.
The purpose of the macro is to change most of the paragraph QuickStyles to have the Body Text style as the following style. I am trying to change more than 50 [Quick] Style Sets because I prefer to not have the bulk of my documents using the Normal style.
1
For example:
Sub StyleFollowingBodyText()
Dim s As Long
With ActiveDocument
For s = 1 To .Styles.Count
With .Styles(s)
If .QuickStyle = True Then
If .NameLocal <> "Normal" Then
If .Type = wdStyleTypeParagraph Or .Type = wdStyleTypeParagraph Then .NextParagraphStyle = "Body Text"
End If
End If
End With
Next
End With
End Sub
I tried discarding the problem statement and not assigning a variable to the style and it works.
I simply used .Styles(iCount) instead of the variable StyleCount.
Sub StyleFollowingBodyText2()
' Charles Kenyon 15 December 2024
' Set the following style for most QuickStyle paragraph styles to be Body Text
Dim StyleCount As Long
Dim thisStyle As Style
Dim iCount As Long
'
With ActiveDocument
Let StyleCount = .Styles.Count
For iCount = 1 To StyleCount
' Let thisStyle = .Styles(iCount)
If .Styles(iCount).QuickStyle = True Then
If .Styles(iCount).Type = wdStyleTypeParagraph Or wdStyleTypeLinked Then
If .Styles(iCount).NameLocal <> "Normal" Then
.Styles(iCount).NextParagraphStyle = "Body Text"
End If
End If
End If
Next iCount
End With
End Sub
Tim Williams’ comment to my question is spot-on as to why I was getting the error. I was using “Let” rather than “Set” for an object variable.
I also found the following macro by Jay Freedman dealing with the Normal template’s styles.
Sub StylesToBodyText() ' Jay Freedman 2023-12-18 ' Change next style to Body Text, Based on style to Body Text in Normal ' You can use the macro below to change all of the "based on Normal" and "following paragraph Normal" styles ' in the Normal.dotm template to be based on and followed by Body Text. ' Before you run the macro, save a copy of the Normal.dotm template in some folder other ' than the Templates folder, as a backup in case something goes wrong. ' I deliberately did not include a Save in the macro, ' as you should examine the results in the Styles pane before you manually save and close the Normal.dotm window. ' If anything is amiss, close the window without saving and see where the problem occurred. ' https://answers.microsoft.com/en-us/msoffice/forum/all/how-to-change-the-style-for-following-paragraph/64644dfc-92a7-44b7-baa8-53fe622344b8 ' Dim sty As Style NormalTemplate.OpenAsDocument ' Comment this line out to apply active document or template and not normal template For Each sty In ActiveDocument.Styles If sty.Type = wdStyleTypeParagraph Then If sty.BaseStyle = "Normal" Then sty.BaseStyle = "Body Text" End If ' If sty.NextParagraphStyle = "Normal" Then sty.NextParagraphStyle = "Body Text" End If End If Next sty ' After checking styles, save and close normal template End Sub
I will probably be adopting that structure but thought it could help others here as well. He is using the For…Each structure rather than the count.