This is a follow-up question to my question (How to search/find for multiple format styles in VBA for Word?). This time instead of inserting a text to the beginning of each heading, we want to remove a few characters from the start of each heading after navigating to a heading titled 'Appendix'.
Trying to get rid of the first number along with the following white space or a period for multi-style paragraphs. For example, we would have headings with '4 Appendix A', '5.1 Intro', '10.2.3 Glossary...', which would be renamed to 'Appendix A', '1 Intro', '2.3 Glossary...'.
I removed the Selection.TypeText Text:=" *Test* " Selection.MoveStart wdParagraph, 1
lines after navigating to the Appendix section and replaced Selection.TypeText Text:=" *Test* "
in the If found Then
statement with the code seen below.
`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`
Getting run-time error '5941' - The requested member of the collection does not exist. If IsNumeric(Selection.Characters(2) = True) Then
seems to be the cause of the error. If I change the '2' to a '1' and Count:=3
to Count:=2
in the If
statement and '1' to a '2' and Count:=2
to Count:=3 in the
ElseIf, then the code is executable. This is a problem because it doesn't recognize the
ElseIf` and only deletes 2 characters for a double-digit number leaving an unwanted white-space or period, i.e., '.2.3 Glossary...' or ' Appendix G'.
IsNumeric
- sorry about that :-) After a good night's sleep and reading how you want to remove only part of the numbering, a different approach occurred to me. I could have usedIsNumeric
by loopingMoveEnd
until the result was no longer a number, but the other way seemed more efficient and less code :-) – Cindy Meister