-1

I am looking for hours for one of the simplest things to do (but with MS things are never simple...): How can I programmatically add in my Word footer 'Page #', using VBA ?
There are zillions of different ways on the internet but none is working. Just a couple of examples

This code fails at Fields.Add:

Sub pageNumber()
    ActiveDocument.Sections(ActiveDocument.Sections.Count) _
        .Headers(wdHeaderFooterPrimary).Range.Select
    With Selection
        .Paragraphs(1).Alignment = wdAlignParagraphCenter
        .TypeText Text:="Page "
        .Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
            "PAGE ", PreserveFormatting:=True
        .TypeText Text:=" of "
        .Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
            "NUMPAGES ", PreserveFormatting:=True
    End With
End Sub

This code doesn't allow me to add a word like 'page' before:

With ActiveDocument.Sections(1) 
 .Footers(wdHeaderFooterPrimary).PageNumbers.Add _ 
 PageNumberAlignment:=wdAlignPageNumberLeft, _ 
 FirstPage:=True 
End With

Any additional hint ?
Thanks.

Laurent Crivello
  • 3,809
  • 6
  • 45
  • 89
  • 1
    Can you expand upon "zillions of different ways on the internet but none is working" by adding code to your question for a specific way you have tried that didn't work? Perhaps we can help you troubleshoot it. – Blackhawk Jul 01 '14 at 16:58
  • Why don't you ask Word. All you have to do is record the steps. Tools menu - Macro - Record New Macro (Alt T, M, R or click the status bar). This is a basic skill for VBA programmers. – Noodles Jul 01 '14 at 18:49
  • Thanks, but record macro didn't help neither. As the copy happens in Excel and the Paste in Word, for some reason this gives me issue. Copying manually in Excel and pasting programmatically in Word fails, when doing the opposite works fine. I have updated the question with some tested (but failing) code. – Laurent Crivello Jul 01 '14 at 19:20
  • Please disregard above remark from me, as I confused with another question. However recording macro was not convincing neither as it required to used a specific template, and simply copy/pasting the code did not work neither. – Laurent Crivello Jul 01 '14 at 21:23

2 Answers2

2

OK, the following code finally works:

With objWord.ActiveDocument.Sections(Section)
    .Footers(wdHeaderFooterPrimary).Range.Text = vbTab & "Page "
    .Footers(wdHeaderFooterPrimary).PageNumbers.Add FirstPage:=True
End With
Laurent Crivello
  • 3,809
  • 6
  • 45
  • 89
  • I was using something like this in an excel VBA macro cleaning up word documents. For that case, I found two things: first, need to add the word object library, see https://support.office.com/en-us/article/Add-object-libraries-to-your-Visual-Basic-project-ed28a713-5401-41b0-90ed-b368f9ae2513 . Second, something like `Set WordDoc = WordApp.Documents.Open(File)`, then `With WordDoc.Sections(1) .Headers(wdHeaderFooterPrimary).PageNumbers.Add FirstPage:=False End With` works -- very similar to this answer. – Richard DiSalvo Jun 18 '17 at 23:13
2

This works for me. It will add "Page ## of ##" at the center of the footer:

Sub Insert_PageNumber()

If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
    ActiveWindow.Panes(2).Close
End If

If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
    ActivePane.View.Type = wdOutlineView Then
    ActiveWindow.ActivePane.View.Type = wdPrintView
End If

ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter

Selection.TypeText Text:="Page "

Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
    "PAGE  \* Arabic ", PreserveFormatting:=True

Selection.TypeText Text:=" of "

Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
    "NUMPAGES  ", PreserveFormatting:=True

ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

End Sub
d_or2000
  • 21
  • 2