-1

我正在寻找几个小时来做​​一件最简单的事情(但对于 MS,事情从来都不是简单的......):如何使用 VBA 以编程方式添加我的 Word 页脚 'Page #'?
互联网上有无数种不同的方法,但都没有奏效。只是几个例子

此代码在 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

这段代码不允许我在之前添加像“page”这样的词:

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

任何额外的提示?
谢谢。

4

2 回答 2

2

好的,下面的代码终于可以工作了:

With objWord.ActiveDocument.Sections(Section)
    .Footers(wdHeaderFooterPrimary).Range.Text = vbTab & "Page "
    .Footers(wdHeaderFooterPrimary).PageNumbers.Add FirstPage:=True
End With
于 2014-07-02T06:45:31.870 回答
2

这对我有用。它将在页脚的中心添加“Page ## of ##”:

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
于 2018-07-09T15:33:51.547 回答