Friday, November 15, 2013

Word macro auto chapter renumbered

Chapter Renumberer Word Macro


About how the Macro works

I wrote this for me and how I write, and I identify my chapters by
Manual page break
[Chapter #]

If you don't (nobody but me does) don't panic, you can still use this to automatically renumber your chapters just fine.

All you do is record a macro, have word find your manual page breaks, arrow over, and insert the [ before the word Chapter and ] after the number. Easy. Last thing I do just before publishing is delete all the [], or leave them (no big).

Enjoy.


'
Sub ChaptersRenumberer()
' [Chapter]
' This renumbers chapters and makes them uniform
' Bold, underlined, centered
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
bLd = InputBox("Chapters are anything inside []" + Chr(13) + Chr(13) + _
"B for Bold" + Chr(13) + _
"U for Underline" + Chr(13) + _
"R to renumber Chapters" + Chr(13) + _
"C to Center Chapters" + Chr(13) + _
"S to Center STARS" + Chr(13) + _
" or X to exit this", "Bold/Underline Chapters", "BC")
If Len(bLd) = 0 Or Len(bLd) > 5 Then Exit Sub
If bLd = "x" Or bLd = "X" Then Exit Sub
RenumberThem = False
BoldThem = False
UnderlineThem = False
CenterThem = False
StarsCenter = False
For e = 1 To Len(bLd)
s = Mid(bLd, e, 1)
If s = "C" Or s = "c" Then CenterThem = True
If s = "R" Or s = "r" Then RenumberThem = True
If s = "U" Or s = "u" Then UnderlineThem = True
If s = "S" Or s = "s" Then StarsCenter = True
If s = "B" Or s = "b" Then BoldThem = True
Next e
aa = Selection.End
Selection.HomeKey Unit:=wdStory
oldText = Selection.Find.Text
'Ch = 2
Ch = InputBox("First Chapter number", , 2)
10:
With Selection.Find '===========================
.Text = "[" 'Here is what you'd want to modify
.Forward = True 'To help it identify your Chapters
.Execute 'mine start with [
End With
s = Selection.End
Selection.MoveRight
With Selection.Find
.Text = "]" 'and end with ]
.Forward = True
.Execute
End With '=============================
If Selection.Find.Found = False Then GoTo 30
e = Selection.End
If Abs(ek - e) < 900 Then
Ch = Val(Left(Right(ActiveDocument.Range(s, e), 3), 2))
Ch = InputBox("An error has been called" + Chr(13) + _
"X to abort" + Chr(13) + _
"Enter a number and it'll renumber starting there", , Ch)
If Ch = "x" Or Ch = "X" Then GoTo 30
End If
'GoTo 30
ActiveDocument.Range(s - 1, e).Select
If RenumberThem Then
If Left(Selection.Text, 2) = "[C" _
Or Left(Selection.Text, 2) = "[c" Then _
Selection.Text = "[Chapter" + Str(Ch) + "]"
End If
If UnderlineThem Then
Selection.Font.Underline = wdUnderlineSingle
Else
Selection.Font.Underline = wdUnderlineNone
End If
If BoldThem Then
Selection.Font.Bold = True
Else
Selection.Font.Bold = False
End If
If CenterThem Then Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
'
Selection.MoveDown
Ch = Ch + 1
If Ch > 200 Then GoTo 30
GoTo 10
'
30:
If StarsCenter = True Then Call CenterStars
Selection.End = aa
Selection.Start = aa
Selection.Find.Text = oldText
End Sub
'
'--------------------------------------------------------------------------------------

No comments:

Post a Comment