+ -
当前位置:首页 → 问答吧 → 如何优化这段添加页码的代码

如何优化这段添加页码的代码

时间:2010-08-09

来源:互联网

这段添加页码的代码是我通过录制宏获得的,请教如何优化:
Sub 添加页码()

' 宏在 2010-8-9 由 tangqingfu 录制
    Application.ScreenUpdating = False
    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 = wdSeekCurrentPageHeader
    If Selection.HeaderFooter.IsHeader = True Then
        ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
    Else
        ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    End If
    ActiveDocument.AttachedTemplate.AutoTextEntries("- 页码 -").Insert Where:= _
        Selection.Range, RichText:=True
    Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    Application.ScreenUpdating = True
End Sub

作者: tangqingfu   发布时间: 2010-08-09

理论上,多用with!需求不明!

作者: shyrick   发布时间: 2010-08-09

这段代码放置于位于StartUp中的一个模板中,在打开任意文档时,单击此宏代码,均能在文档的页脚中插入页码,页码居中。
能否帮忙一下?

作者: tangqingfu   发布时间: 2010-08-09

复制内容到剪贴板
代码:
Sub tjym()
  ' 宏在 2010-8-9 由 kqbt 录制
  With ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).PageNumbers
    .Add PageNumberAlignment:=wdAlignPageNumberCenter, FirstPage:=True
    .NumberStyle = wdPageNumberStyleNumberInDash
  End With
End Sub

作者: kqbt   发布时间: 2010-08-09

谢谢kqbt兄,先感谢,再测试!

作者: tangqingfu   发布时间: 2010-08-09

引用:
原帖由 kqbt 于 2010-8-9 15:31 发表
Sub tjym()
  ' 宏在 2010-8-9 由 kqbt 录制
  With ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).PageNumbers
    .Add PageNumberAlignment:=wdAlignPageNumberCenter, FirstPage:=True
    .N ...
请教kqbt兄:
如果添加的页码为“第X页 共Y页”格式,常量应如何修改?

作者: tangqingfu   发布时间: 2010-08-09

引用:
原帖由 tangqingfu 于 2010-8-9 22:51 发表

请教kqbt兄:
如果添加的页码为“第X页 共Y页”格式,常量应如何修改?
请Qingfu兄参考:
复制内容到剪贴板
代码:
Sub tjym()
  Dim Rng As Range
  Set Rng = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range
  Rng.ParagraphFormat.Alignment = wdAlignParagraphCenter
  NormalTemplate.AutoTextEntries("第 X 页 共 Y 页").Insert Where:=Rng, RichText:=True
End Sub

作者: kqbt   发布时间: 2010-08-10

谢谢kqbt兄的大力帮忙,测试通过,收藏学习!

作者: tangqingfu   发布时间: 2010-08-10

收藏学习,谢谢

作者: linchangh   发布时间: 2010-08-10

收藏学习

作者: SSJJWW   发布时间: 2010-11-01