问题描述
我有以下vba代码
Sub pageNumber(objOutputDoc As Object)
' With objOutputDoc.Sections(objOutputDoc.Sections.Count)
' .Footers(wdHeaderFooterPrimary).Range.Text = vbTab & vbTab & "Page "
' .Footers(wdHeaderFooterPrimary).PageNumbers.Add FirstPage:=True
' End With
Dim i As Long,bAdd As Boolean: bAdd = True
With objOutputDoc.Sections(objOutputDoc.Sections.Count).Footers(wdHeaderFooterPrimary).Range 'First.Headers(wdHeaderFooterPrimary).Range
For i = 1 To .Fields.Count
If .Fields(i).Type = wdFieldPage Then
bAdd = False: Exit For
End If
Next
If bAdd = True Then
.InsertAfter vbTab & vbTab & "Page "
.Fields.Add .Characters.Last,wdFieldEmpty,"PAGE",False
.InsertAfter " of "
.Fields.Add .Characters.Last,"NUMPAGES",False
End If
End With
End Sub
上面的vba代码自动将Y的第X页添加到我的Word文档中。我的Word文档包含27个页面,所以当我运行vba时,页面编号将插入第1页,共27页,第2页,共27页,依此类推。我遇到的问题是我的最后一页,即第27页,我已经插入在最后一页的底部第1页,共1页。当我运行vba时,现在第27页的第27页插入了最后一页,现在我有第1页的第1页和第27页的27。我希望倒数第二页是Page 26之26。
解决方法
提供的第1至26页位于同一部分,您可以使用SECTIONPAGES
而不是NUMPAGES
来实现目标。
然后在以下部分中,您需要重新启动页码:
With objOutputDoc.Sections(objOutputDoc.Sections.Count).HeaderFooter.PageNumbers
.RestartNumberingAtSection = True
.StartingNumber = 1
End With