在 VBA for Word 中从多个样式段落的开头删除字符

其TnTg

这是我的问题(如何在 VBA for Word 中搜索/查找多种格式样式?的后续问题这次不是在每个标题的开头插入文本,而是在导航到标题为“附录”的标题后从每个标题的开头删除几个字符。

试图摆脱第一个数字以及以下空格或多样式段落的句点。例如,我们将有带有“ 4 附录 A ”、“ 5.1 介绍”、“ 10.2.3 词汇表... ”的标题,它们将重命名为“附录 A ”、“ 1 介绍”、“ 2.3 词汇表...”。 '。

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

`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似乎是错误的原因。如果我改变“2”到“1”,并Count:=3Count:=2If声明和“1”到“2”,并Count:=2Count:=3 in theelseif的, then the code is executable. This is a problem because it doesn't recognize theElseIf`,只删除2个字符留下不必要的空白或期双位数,即“ .2.3 词汇表... ”或“附录 G ”。

辛迪大师

错误 5941 的原因是Characters(2). 这不是你想象的那样。这仅从选择中获取第二个字符,而不是两个字符。选择是一个闪烁的插入点,因此不包含两个字符。错误说:您告诉我要获取第二个字符,但没有两个字符,因此我无法为您提供所需的内容。

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

由于需要测试多个字符,因此Range需要扩展选择(或Word VBA 提供了许多“移动”方法;相当于在键盘上按住 Shift 键并按右箭头键是MoveEnd,并且有一些变体,例如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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Word 2007 VBA宏,用于从多个超链接中删除段落标记(显示文本)

来自分类Dev

Word中的VBA阵列

来自分类Dev

Word对象中的Word VBA范围

来自分类Dev

VBA Word的重复删除无效

来自分类Dev

Word VBA中的条件机制

来自分类Dev

Word 2010 VBA中的InputBox

来自分类Dev

Word中的条件替换VBA

来自分类Dev

Word VBA中的嵌套IF字段

来自分类Dev

vba Word在Msgbox中显示多个标签

来自分类Dev

查找段落的边界框尺寸 (Word VBA)

来自分类Dev

在Word文档中每个段落的开头删除空格

来自分类Dev

使用VBA重置样式继承(Word 2013)

来自分类Dev

在Word VBA中使用样式标题

来自分类Dev

VBA,将光标移到Microsoft Word中页面的开头

来自分类Dev

如何使用VBA删除Word文档的整行

来自分类Dev

Word VBA删除带有书签的表

来自分类Dev

从多个Word文档复制和粘贴VBA

来自分类Dev

使用VBA在同一Word文档中管理多个样式定义

来自分类Dev

Word VBA:遍历字符非常慢

来自分类Dev

在Word模板(VBA)中从关闭的Word文档中检索文本

来自分类Dev

VBA从Excel获取Word文档中样式的所有出现

来自分类Dev

无法使用 VBA 在 Word 中更改样式

来自分类Dev

VBA-旋转Word文档中的Word.Shape

来自分类Dev

Excel VBA-删除最多* word *的字符串内容

来自分类Dev

Excel VBA-删除最多* word *的字符串内容

来自分类Dev

Word VBA仅格式化查找替换字符串的一部分(格式化以字符串开头的段落)

来自分类Dev

VBA Excel关闭Word Doc中的灰度

来自分类Dev

对Word VBA中的WdPasteOptions枚举感到困惑

来自分类Dev

在Word VBA中检测其中包含“-”的单词