For applying for a copyright, the Library of Congress wants your book (as a document) tagged with their coding. This involves placing the <ch1>, <ch2>, <ch3>, etc. tagging before each chapter. I'm creating a macro to apply all this coding automatically.
Here is my code:
Selection.Find.ClearFormatting
Selection.Find.style = ActiveDocument.Styles("chapter number")
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Copy
Selection.PasteAndFormat (wdFormatOriginalFormatting)
Selection.PasteAndFormat (wdFormatOriginalFormatting)
Selection.HomeKey Unit:=wdLine
Selection.MoveUp Unit:=wdParagraph, Count:=2
Selection.TypeText Text:="<ch"
Selection.MoveRight Unit:=wdWord, Count:=1
Selection.TypeText Text:=">"
Selection.MoveDown Unit:=wdParagraph, Count:=2
We have a style called "chapter number," so this code is finding the next instance of the "chapter number" style and inserting the <ch_> tag at the beginning. In the macro, I've repeated this code twenty times (since rarely do our books have more than 20
chapters). If there are less than 20 chapters, the following dialogue box appears at the end of the macro: "Word has reached the end of the document. Do you want to continue searching from the beginning?" I select "No," and then a Runtime Error 4605 dialog
box appears.
Here's my question: Overall, the code works if I click through these dialog boxes at the end, but since others will be using this code, is there a way to simplify the code so that it doesn't require these extra clicks?