|
-
Jan 3rd, 2011, 08:57 AM
#1
Thread Starter
New Member
Inserting Page numbers in Word through VB6
Hi,
I am having difficulty getting a piece of code to put page 1 of X, page 2 of X etc automatically through VB. At the moment it just puts the last page X of X down.
I need to amend different documents automatically:-
This is the code that I have used:-
Sub GetHeadersFooters(sFile As String, strfilename As String, text As String, strProjName As String)
Dim oApp As Word.Application
Dim oDoc As Word.Document
Dim oSec As Word.Section
Dim oPageStart As Word.Range
Dim iPage As Integer, iTotalPages As Integer, iSection As Integer
Dim sHeader As String, sFooter As String
Dim strRest As String
Dim pic As Word.Shape
'Open the document
Set oApp = New Word.Application
Set oDoc = oApp.Documents.Open(sFile)
iTotalPages = oDoc.ComputeStatistics(wdStatisticPages)
oApp.Visible = True
'Retrieve the headers and footers on each page
With oDoc
iSection = 0
For iPage = 1 To iTotalPages
'Go to the page represented by the page number iPage and
'retrieve its section
Set oPageStart = oDoc.GoTo(What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=iPage)
Set oSec = oPageStart.Sections(1)
'If this is a different section than the one in the previous
'iteration and it has a first page header/.footer, then
'retrieve the first page header/footer for this section.
'Otherwise, retrieve the primary header/footer for this section
If (iSection < oSec.Index) And _
(oSec.PageSetup.DifferentFirstPageHeaderFooter) Then
sFooter = ""
sHeader = ""
sHeader = oSec.Headers(wdHeaderFooterFirstPage).Range.text
sHeader = " Operations and Maintenance Manual for: " & strProjName
sHeader = oSec.Headers(wdHeaderFooterPrimary).Range.text
sFooter = oSec.Footers(wdHeaderFooterPrimary).Range.text
sFooter = strfilename & " " & text & " Page " & iPage & " of " & iTotalPages
oSec.Headers(wdHeaderFooterPrimary).Range.text = sHeader
oSec.Footers(wdHeaderFooterPrimary).Range.text = sFooter
Else
sFooter = ""
sHeader = ""
sHeader = oSec.Headers(wdHeaderFooterFirstPage).Range.text
sHeader = " Operations and Maintenance Manual for: " & strProjName
sFooter = oSec.Footers(wdHeaderFooterPrimary).Range.text
sFooter = strfilename & " " & text & " Page " & iPage & " of " & iTotalPages
oSec.Headers(wdHeaderFooterPrimary).Range.text = sHeader
oSec.Footers(wdHeaderFooterPrimary).Range.text = sFooter
End If
iSection = oSec.Index
Next
End With
'Make Word visible to compare the document with the results in the
oDoc.Save
oApp.Quit
End Sub
Thanks for any help.
-
Jan 3rd, 2011, 09:14 AM
#2
Re: Inserting Page numbers in Word through VB6
Welcome to the VBF!
Just used the macro-recorder to get this:
Code:
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldPage
With Selection.HeaderFooter.PageNumbers
.NumberStyle = wdPageNumberStyleArabic
.HeadingLevelForChapter = 0
.IncludeChapterNumber = False
.ChapterPageSeparator = wdSeparatorHyphen
.RestartNumberingAtSection = False
.StartingNumber = 0
End With
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeText Text:="of "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldNumPages
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|