Word VBA JoinList 语句运行时错误 4605 此命令不可用

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

#这是我正在运行的代码。

    Sub joinThisParagraphToList()
        Dim prgh As Paragraph
        
        Debug.Print Selection.Paragraphs(1).Range.ListFormat.ListString & " " & Selection.Paragraphs(1).Range.Text
        Debug.Print xlateWdListType(Selection.Paragraphs(1).Range.ListFormat.ListType)
        Debug.Print xlateWdListType(Selection.Paragraphs(2).Range.ListFormat.ListType)
        Set prgh = Selection.Paragraphs(1)
        Debug.Print "<" & prgh.Range.Text & ">"
        prgh.JoinList
        Set prgh = Nothing
    End Sub
    
    Private Function xlateWdListType(ByVal typ As WdListType) As String
        If typ = wdListBullet Then
            xlateWdListType = "wdListBullet"
        ElseIf typ = wdListListNumOnly Then
            xlateWdListType = "wdListListNumOnly"
        ElseIf typ = wdListMixedNumbering Then
            xlateWdListType = "wdListMixedNumbering"
        ElseIf typ = wdListNoNumbering Then
            xlateWdListType = "wdListNoNumbering"
        ElseIf typ = wdListOutlineNumbering Then
            xlateWdListType = "wdListOutlineNumbering"
        ElseIf typ = wdListPictureBullet Then
            xlateWdListType = "wdListPictureBullet"
        ElseIf typ = wdListSimpleNumbering Then
            xlateWdListType = "wdListSimpleNumbering"
        Else
            xlateWdListType = "unknown"
        End If
    End Function

debug.print 语句的输出是:

        11 XXXXXXX XXXXXXX XXXXXXXXXXX Hello
        
        wdListOutlineNumbering
        wdListOutlineNumbering
        <XXXXXXX XXXXXXX XXXXXXXXXXX Hello
        >

我要解决的问题是,看起来像大纲编号列表(1. 1.1、1.2、1.2.1、1.2.2等)实际上是几个列表,并且它们并不都使用自动编号功能MS Word 的。

有些段落是用Word编号的,有些是其他用户手动插入的。 我每年都会遇到这个问题 10 或 15 次,并且必须花费几个小时来更正文档中的编号,以便简化文档的未来维护。 我正在尝试找到至少一种半自动化的方法来发现和纠正这些问题。

我的文件中有两个段落,大纲编号分别为 11. 和 11.1。 第 11.1 段紧接在第 11 段之后,两者都只有 1 行长。 问题是这两个段落不是同一列表中的项目。 我对 JoinList 语句的理解是,它基本上应该采用代码中由 prgh 标识的段落(即第 11 段),并将其作为文档中最近列表的一部分,在本例中将是下一行(即第 11.1 段)。 在开始上面的宏代码之前,我选择了第 11 段。

我的问题是,我遇到了 4605 运行时错误,而且我在网上没有找到任何讨论 4605 的内容是在讨论 JoinList 语句,而且除了基本的 MS Word 对象模型参考信息之外,我找不到更多的内容;关于该错误在我的情况下真正意味着什么,或者如何修复或围绕它编写代码,一无所知。 我有可以提供的整个文档作为示例,但我看不到任何附加文档的方法。

vba ms-word
1个回答
0
投票

大纲列表可能会变得非常混乱。我的建议是使用左缩进来确定列表级别。为简单起见,我假设您只需要查看列表段落,但这种方法可以扩展以测试任何内容。我也没有添加任何内容来修剪硬编码的数字,并且我或多或少随机地选择了轮廓样式。

Sub fixOutlineList()
    Dim listPara As Paragraph
    Dim outlineList As ListTemplate
    Dim i As Integer
    
    ' Define an outline list template
    Set outlineList = Document.ListTemplates.Add(OutlineNumbered:=True)
    For i = 1 To 4
        With outlineList.ListLevels(i)
            .TrailingCharacter = wdTrailingTab
            .NumberStyle = wdListNumberStyleArabic
            .NumberPosition = InchesToPoints(0)
            .Alignment = wdListLevelAlignLeft
            .TextPosition = InchesToPoints(1)
            .TabPosition = InchesToPoints(1)
            .ResetOnHigher = 0
            .StartAt = 1
            .ResetOnHigher = True
        End With
    Next i
    outlineList.ListLevels(1).NumberFormat = "%1"
    outlineList.ListLevels(2).NumberFormat = "%1.%2"
    outlineList.ListLevels(3).NumberFormat = "%1.%2.%3"
    
    ' Loop through the list paras in the doc using
    ' left indent to determine list level
    For Each listPara In Document.ListParagraphs
        Select Case listPara.LeftIndent
        Case 36
           Call setListLevel(listPara, 1, outlineList)
        Case 72
            Call setListLevel(listPara, 2, outlineList)
        Case 96
             Call setListLevel(listPara, 3, outlineList)
        Case Else
            Debug.Print "no case for indent " & listPara.LeftIndent
        End Select
    Next listPara
End Sub
Sub setListLevel(listPara As Paragraph, level As Integer, outlineList As ListTemplate)
    ' get rid of existing indents and tabs just
    ' to normalize everything
    With listPara
        .Range.ListFormat.RemoveNumbers
        .TabStops.ClearAll
        .FirstLineIndent = 0
        .LeftIndent = 0
    End With
    ' try to explictly set the list level if one already exists
    ' this can throw an error when (I think) a preceding level
    ' can't be determined
    On Error Resume Next
    listPara.Range.ListFormat.ListLevelNumber = level
    If Err.Number <> 0 Then
        Debug.Print "unable to establish an item for level preceding " & level
        Err.Clear
    End If
    ' apply the list template and set the list level
    listPara.Range.ListFormat.ApplyListTemplateWithLevel ListTemplate:=outlineList, ContinuePreviousList:=True, ApplyLevel:=level
    ' I've found there are times when you need to add this step again at the end
    listPara.Range.ListFormat.ListLevelNumber = level
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.