【问题标题】:Removing characters from the start of multiple style paragraph in VBA for Word从 VBA for Word 中的多个样式段落的开头删除字符
【发布时间】:2019-07-12 22:36:31
【问题描述】:

这是我的问题 (How to search/find for multiple format styles in VBA for Word?) 的后续问题。这次不是在每个标题的开头插入文本,而是在导航到标题为“附录”的标题后从每个标题的开头删除几个字符。

尝试去掉第一个数字以及后面的空格或多样式段落的句点。例如,我们会有带有“4 Annex A”、“5.1 Intro”、“10.2.3 Glossary...”的标题,它将被重命名为“Appendix A”、“1 Intro”、“2.3 Glossary...”。

导航到附录部分后,我删除了Selection.TypeText Text:=" *Test* " Selection.MoveStart wdParagraph, 1 行,并将If found Then 语句中的Selection.TypeText Text:=" *Test* " 替换为下面的代码。

`If found Then
    Selection.HomeKey Unit:=wdLine
    If IsNumeric(Selection.Characters(2) = True) Then
       Selection.Delete Unit:=wdCharacter, Count:=3
       Selection.MoveStart wdParagraph, 1
    ElseIf IsNumeric(Selection.Characters(1) = True) Then
       Selection.Delete Unit:=wdCharacter, Count:=2
       Selection.MoveStart wdParagraph, 1
    Else
       Selection.MoveStart wdParagraph, 1
    End If
 End If`

出现运行时错误“5941”- 请求的集合成员不存在。 If IsNumeric(Selection.Characters(2) = True) Then 似乎是错误的原因。如果我在If 语句中将“2”更改为“1”,将Count:=3 更改为Count:=2,将“1”更改为“2”,将Count:=2 更改为Count:=3 in theElseIf, then the code is executable. This is a problem because it doesn't recognize theElseIf` 和仅删除两位数的 2 个字符,留下不需要的空格或句点,即“.2.3 Glossary...”或“Appendix G”。

【问题讨论】:

  • 我的回答没有使用 IsNumeric - 对此感到抱歉 :-) 经过一夜好眠并阅读您希望如何仅删除 part 的编号,a我想到了不同的方法。我本可以通过循环 MoveEnd 直到结果不再是数字来使用 IsNumeric,但另一种方式似乎更有效且代码更少:-)

标签: vba ms-word


【解决方案1】:

由于Characters(2) 导致错误5941 的原因。这不是你想象的那样。这仅从选择中获得第二个字符,而不是两个字符。并且选择是一个闪烁的插入点,因此不包含两个字符。错误说:你告诉我要获取第二个字符,但没有两个字符,所以我不能给你你需要的东西。

该行中的另一个问题(您还没有看到):括号应该在 = 之前,而不是在 True 之后:如果 IsNumeric(Selection.Characters(2)) = True。

由于需要测试多个字符,选择(或Range)需要扩展。 Word VBA 提供了许多“移动”方法; MoveEnd 等价于按住 Shift 并在键盘上按右箭头,并且有一些变体,例如 MoveEndWhileMoveEndUntil,允许您指定条件。可选地,这些可以返回移动的字符数(如下面的代码所示)。

以下方法首先使用MoveEndWhile 获取数字字符(直到下一个不再是数字):MoveEndWhile("0123456789", wdForward)... 然后扩展直到下一个字符不再是.

然后删除此Range。 (还有一个Debug.Print 行打印出Range 的内容和移动的字符数,以防您对这些信息感兴趣 - 只需删除评论标记')。

请注意,我已经包含了整个代码,以防其他人有兴趣完整地查看它。上一个问题中不再相关的部分已被删除。您会发现新部件标记为'''NEW CODE HERE

Sub AppendixFix()

    ' Declaring variables
    Dim multiStyles As String, i As Integer
    Dim aStyleList As Variant
    Dim counter As Long, s As String, found As Boolean
    Dim rngStart As Range

    multiStyles = "Heading 1,Heading 2,Heading 3,Heading 4,Heading 5,Heading 6,Heading 7,Heading 8,Heading 9"
    aStyleList = Split(multiStyles, ",")

    ' Start at the top of document and clear find formatting
    Selection.HomeKey Unit:=wdStory
    Selection.Find.ClearFormatting

    ' Navigate to Appendix section
    Selection.Find.style = ActiveDocument.styles("Heading 1")
    With Selection.Find
        .Text = "Appendix"
        .Forward = True
        .wrap = wdFindStop
        .Format = True
        .Execute
    End With
    Selection.HomeKey Unit:=wdLine
    Set rngStart = Selection.Range.Duplicate

    ' Loop through all the styles in the list
    For counter = LBound(aStyleList) To UBound(aStyleList)
        'Loop as long as the style is found
        Do
            s = aStyleList(counter)
            With Selection.Find
                .style = ActiveDocument.styles(s)
                .Text = "^p"
                .Forward = True
                .wrap = wdFindStop
                .Format = True
                found = .Execute
            End With
'''NEW CODE HERE                
            Dim rngStartOfLine As Range
            Dim charsMovedNumeric As Long, charsMovedDot
            If found Then
                Selection.HomeKey Unit:=wdLine
                Set rngStartOfLine = Selection.Range
                charsMovedNumeric = rngStartOfLine.MoveEndWhile("0123456789", wdForward)
                charsMovedDot = rngStartOfLine.MoveEndWhile(".")
                'Debug.Print rngStartOfLine, charsMovedNumeric, charsMovedDot
                rngStartOfLine.Delete
                Selection.MoveStart wdParagraph, 1    
             End If
'''END OF NEW CODE
            If Selection.Start = ActiveDocument.content.End - 1 Then
                'End of Document, then loop to next style in list
                Exit For
            End If
        Loop Until found = False
        'start back at the Appendix for the next style
        rngStart.Select
    Next
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-06
    • 1970-01-01
    • 2018-01-24
    • 1970-01-01
    相关资源
    最近更新 更多