I need to replace all header and footer info in many Word documents.
The new data is entered via a UserForm
My code extract is as follows:
' ------------------------------------------------------------
Private Sub CommandButton1_Click()
Dim oApp As Word.Application
Dim oDoc As Word.Document
Dim oSec As Word.Section
Dim strTitle As String
Dim strVersion As String
Dim strClassification As String
Dim strPageInfo As String
Dim xx as Long
Dim yy As Long
strTitle = TextBox1
strVersion = TextBox2
strCopyright = TextBox3
strPath = TextBox4
strClassification = ComboBox1
ActiveDocument.Repaginate
yy = ActiveDocument.BuiltInDocumentProperties(wdPropertyPages)
MsgBox yy ' For testing
For Each oSec In ActiveDocument.Sections ' Delete all existing header and footer content
oSec.Headers(wdHeaderFooterEvenPages).Range.Text = ""
oSec.Headers(wdHeaderFooterFirstPage).Range.Text = ""
oSec.Headers(wdHeaderFooterPrimary).Range.Text = ""
oSec.Footers(wdHeaderFooterEvenPages).Range.Text = ""
oSec.Footers(wdHeaderFooterFirstPage).Range.Text = ""
oSec.Footers(wdHeaderFooterPrimary).Range.Text = ""
Next oSec
For Each oSec In ActiveDocument.Sections ' Write new header and footer content
oSec.Headers(wdHeaderFooterPrimary).Range.Font.Name = "Arial"
oSec.Headers(wdHeaderFooterPrimary).Range.Font.Size = "20"
oSec.Headers(wdHeaderFooterPrimary).Range.Font.Color = wdColorDarkBlue
oSec.Headers(wdHeaderFooterPrimary).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
oSec.Headers(wdHeaderFooterPrimary).Range.Text = strPath
strPageInfo = "Page xx of " & yy
' oSec.Footers(wdHeaderFooterPrimary).PageNumbers.Add
oSec.Footers(wdHeaderFooterPrimary).Range.Font.Name = "Arial"
oSec.Footers(wdHeaderFooterPrimary).Range.Font.Size = "10"
oSec.Footers(wdHeaderFooterPrimary).Range.Font.Color = wdColorDarkBlue
oSec.Footers(wdHeaderFooterPrimary).Range.Text = strTitle & vbTab & strVersion & vbTab & strPageInfo & vbCr _
& strCopyright & vbTab & vbTab & strClassification
oSec.Footers(wdHeaderFooterPrimary).Range.Borders.Enable = True
Next oSec
Unload Me
End Sub
' ------------------------------------------------------------
Everything works OK, except for the "Page xx of yy" part. The value of 'yy' is calculated correctly.
How do I calculate and print the 'xx' part?