ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 4754|回复: 3

[求助] 用vba插入奇偶页不同的页面问题

[复制链接]

TA的精华主题

TA的得分主题

发表于 2011-4-15 19:56 | 显示全部楼层 |阅读模式
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
假如一个文档,要插入奇偶页不同的页码,并且用vba实现,我录制了以下代码,如果页面多于2页,且光标定位于奇数页,这个代码好是可以运行,如果光标定位在偶数页就出错,或者只有一页时代码就无法运行,你可能会说等大于1页再运行这个代码,但我怕忘记加页码,喜欢新建文档后就加入页面,而且这个贴主要是想请教下,只有一页的情况下,怎么用vba插入奇偶页不同的页码,望高人指点
Sub Macro3()

    With ActiveDocument.PageSetup
        .OddAndEvenPagesHeaderFooter = True
    End With

    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

    Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
    With Selection.ParagraphFormat
        .CharacterUnitRightIndent = 1
    End With

    Selection.Font.Name = "Times New Roman"
    Selection.Font.Size = 14
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldPage

    ActiveWindow.ActivePane.View.NextHeaderFooter
    With Selection.ParagraphFormat
        .CharacterUnitLeftIndent = 1
    End With
    Selection.Font.Name = "Times New Roman"
    Selection.Font.Size = 14
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldPage
   
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

End Sub

TA的精华主题

TA的得分主题

发表于 2011-4-16 11:41 | 显示全部楼层
可试试如下代码:
  1. Sub test()
  2.     Application.ScreenUpdating = False
  3.     With ActiveDocument.Sections(1)
  4.         .PageSetup.OddAndEvenPagesHeaderFooter = True
  5.         With .Footers(wdHeaderFooterPrimary)
  6.             With .PageNumbers
  7.                 .Add PageNumberAlignment:=wdAlignPageNumberRight
  8.                 .NumberStyle = wdPageNumberStyleNumberInDash
  9.             End With
  10.             With .Range.Frames(1)
  11.                 .HorizontalPosition = wdFrameRight
  12.                 With .Range.ParagraphFormat
  13.                     .Alignment = wdAlignParagraphRight
  14.                     .CharacterUnitRightIndent = 1
  15.                 End With
  16.             End With
  17.         End With
  18.         With .Footers(wdHeaderFooterEvenPages).Range.Frames(1)
  19.             .HorizontalPosition = wdFrameLeft
  20.             With .Range.ParagraphFormat
  21.                 .Alignment = wdAlignParagraphLeft
  22.                 .CharacterUnitLeftIndent = 1
  23.             End With
  24.         End With
  25.     End With
  26.     Application.ScreenUpdating = True
  27. End Sub
复制代码

TA的精华主题

TA的得分主题

 楼主| 发表于 2011-4-16 14:45 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
能注视下 或简单说下原理吗,是可以实现了 但是我要修改其中的代码 明白原理容易操作,我想要我自己做的页码 不要默认的

[ 本帖最后由 tongyi 于 2011-4-16 15:22 编辑 ]

TA的精华主题

TA的得分主题

发表于 2017-10-17 08:38 | 显示全部楼层
tongyi 发表于 2011-4-16 14:45
能注视下 或简单说下原理吗,是可以实现了 但是我要修改其中的代码 明白原理容易操作,我想要我自己做的页 ...

.PageSetup.OddAndEvenPagesHeaderFooter = True
它表示奇偶页不同,所有重点就这一句
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-3-29 14:59 , Processed in 0.036421 second(s), 9 queries , Gzip On, Redis On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表