这是我的问题(如何在 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:=3
以Count:=2
在If
声明和“1”到“2”,并Count:=2
以Count:=3 in the
elseif的, then the code is executable. This is a problem because it doesn't recognize the
ElseIf`,只删除2个字符留下不必要的空白或期双位数,即“ .2.3 词汇表... ”或“附录 G ”。
错误 5941 的原因是Characters(2)
. 这不是你想象的那样。这仅从选择中获取第二个字符,而不是两个字符。选择是一个闪烁的插入点,因此不包含两个字符。错误说:您告诉我要获取第二个字符,但没有两个字符,因此我无法为您提供所需的内容。
该行中的另一个问题(您还没有看到):括号应该在 = 之前,而不是在 True 之后:If IsNumeric(Selection.Characters(2)) = True。
由于需要测试多个字符,因此Range
需要扩展选择(或)。Word VBA 提供了许多“移动”方法;相当于在键盘上按住 Shift 键并按右箭头键是MoveEnd
,并且有一些变体,例如MoveEndWhile
和MoveEndUntil
允许您指定条件。或者,这些可以返回移动的字符数(如下面的代码所示)。
以下方法用于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] 删除。
我来说两句