我正在寻找最简单的事情之一(但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
还有其他提示吗?
谢谢。
答案 0 :(得分:2)
好的,以下代码最终有效:
With objWord.ActiveDocument.Sections(Section)
.Footers(wdHeaderFooterPrimary).Range.Text = vbTab & "Page "
.Footers(wdHeaderFooterPrimary).PageNumbers.Add FirstPage:=True
End With
答案 1 :(得分:0)
这对我有用。它将在页脚中心添加“ ##页,共##”:
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