Hi
I am trying to insert page numbers into TextBoxes in the document header in the format Page X of Y. Unfortunately, I cannot just use insert page numbering via the headers (or footers) as the document already contains a header that must remain as is and the TextBoxes must be positioned above it. Anyway, I decided to insert the string "Page X of " and use a counter to replace X. When testing the Sub below, I run into an error on the line
I'd really appreciate any help to correct this.
Thanks
I am trying to insert page numbers into TextBoxes in the document header in the format Page X of Y. Unfortunately, I cannot just use insert page numbering via the headers (or footers) as the document already contains a header that must remain as is and the TextBoxes must be positioned above it. Anyway, I decided to insert the string "Page X of " and use a counter to replace X. When testing the Sub below, I run into an error on the line
VBA Code:
For Each counter In Selection.Range
VBA Code:
Sub ReplaceInTextBox()
Dim shp As Shape
Dim counter As Long
Selection.HomeKey Unit:=wdStory
a = ActiveDocument.BuiltInDocumentProperties("Number of Pages")
For i = 1 To a
Dim Box1 As Shape
Set Box1 = ActiveDocument.Shapes.AddTextbox( _
Orientation:=msoTextOrientationHorizontal, _
Left:=65, Top:=1, Width:=150, Height:=30)
Box1.TextFrame.TextRange.Text = ActiveDocument.Name & Chr(10) & "Page X of " & a
counter = 0
For Each shp In ActiveDocument.Shapes
Selection.Select
For Each counter In Selection.Range
counter = counter + 1
Next counter
Next
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "X"
.Replacement.Text = counter
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.GoTo What:=wdGoToPage, Which:=NextPage
End Sub
Thanks