【发布时间】: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,但另一种方式似乎更有效且代码更少:-)