Problem with cycling through Styles to change NextParagraphStyle – Run time error 91 – Word vba

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật