循环通过样式更改 NextParagraphStyle 时出现问题 - 运行时错误 91 - Word vba

问题描述 投票:0回答:1

如标题所示。

我的宏是:

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

我在运行时收到此错误消息:

screenshot of error 91

停在

Let thisStyle = .Styles(iCount)

该宏的目的是更改大部分段落的 QuickStyles,使其正文文本样式如下所示。我正在尝试更改 50 多个[快速]样式集,因为我不想让大部分文档使用普通样式。

vba for-loop ms-word
1个回答
0
投票

我尝试放弃问题陈述,不为样式分配变量,它起作用了。

我只是使用 .Styles(iCount) 而不是变量 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

我不知道“为什么”,但很高兴我可以让它做我想做的事。

© www.soinside.com 2019 - 2024. All rights reserved.