PDA

View Full Version : Insert total Pages in Footer



MelbDerm
03-25-2010, 08:06 PM
Hi there,

Im trying to create a footer in a Word template. The problem is that the footer must include the page number and total Pages in the format "page num/total Pages", e.g "1/4"

Here is what I have so far. I dont see to able to place the forward slash between the 2 fields. I get the original text (strFooterText) and "/22".


For i = 1 To ActiveDocument.Sections.Count
Set oRange = ActiveDocument.Sections(i).Footers(wdHeaderFooterPrimary).Range

With oRange
.ParagraphFormat.Alignment = wdAlignParagraphLeft
.Text = strFooterText
.Collapse direction:=wdCollapseEnd
.Fields.Add oRange, wdFieldPage
.InsertAfter Text:="/"
.Collapse direction:=wdCollapseEnd
.Fields.Add oRange, wdFieldNumPages
.Fields.Update
.WholeStory
End With
Set oRange = Nothing
Next i

Any ideas?

Edit: VBA tags added to code

lucas
03-25-2010, 09:32 PM
This works for me:
Sub a()
Dim i As Long
Dim oRange As Range
For i = 1 To ActiveDocument.Sections.Count
Set oRange = ActiveDocument.Sections(i).Footers(wdHeaderFooterPrimary).Range
With oRange
.ParagraphFormat.Alignment = wdAlignParagraphLeft
'.Text = strFooterText
'.Collapse direction:=wdCollapseEnd
.Fields.Add oRange, wdFieldPage
.InsertAfter Text:="/"
.Collapse direction:=wdCollapseEnd
.Fields.Add oRange, wdFieldNumPages
.Fields.Update
.WholeStory
End With
Set oRange = Nothing
Next i
End Sub


Read the help files about the use of Collapse to help you understand what is happening with your original code.

I just noticed you are new to the forum. Welcome and I hope this helps you.

You can format your code for the forum by selecting it when posting and hitting the vba button.

macropod
03-25-2010, 11:05 PM
Since you're looping through all Sections, you really should check whether the current footer linked to the previous Section's footer. There is no point re-inserting the field code if it's already there courtesy of a link. For example:

Sub a()
Dim i As Long, oRange As Range
With ActiveDocument
For i = 1 To .Sections.Count
With .Sections(i).Footers(wdHeaderFooterPrimary)
If Not .LinkToPrevious Then
Set oRange = .Sections(i).Footers(wdHeaderFooterPrimary).Range
...
End If
End With
Set oRange = Nothing
Next i
End With
This raises a further issue: since you apparently want all Sections to have the same footer, why not simply insert the field code into the first Section, then ensure all subsequent Sections are linked to it?

MelbDerm
03-26-2010, 12:11 AM
thanks guys,

However, the main problem is that I am inserting some text in to a header and I want it to look like

NameOfDocument & vbTab & pageNum & "/" & totalPageNum

I can get the "/" in the footer but its not in the right place (its before the pageNum and totalPageNum). What do i need to do?
I been playing with it all day. I come from a java background so am new to VBA.

Thanks in advance.

Derm

macropod
03-26-2010, 02:49 AM
I am inserting some text in to a header
Huh? The code is for footers!

Be that as it may, try:

.Fields.Add .Characters.Last, wdFieldPage, , False
.Characters.Last.InsertAfter Text:="/"
.Fields.Add .Characters.Last, wdFieldNumPages, , False

MelbDerm
03-28-2010, 04:46 PM
hey guys,

Thanks for all the help :clap:

its all sorted now :hi: