ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[求助] 用VBA 批量给文档每节设置不同的页眉页脚

[复制链接]

TA的精华主题

TA的得分主题

发表于 2018-12-31 10:08 | 显示全部楼层 |阅读模式
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
要求:
文档共有12页,每节标头不同,要求批量给不同的节插入不同的页眉和页脚。
例如:
第一节的页眉文字为:第一节标头
第一节的页脚为:第一节标头 & 第1页(或 第2页,第3页……)
模拟效果见附件中的“目标.docm”
附件如下

批量插入页眉页脚.rar

123.8 KB, 下载次数: 159

TA的精华主题

TA的得分主题

 楼主| 发表于 2018-12-31 15:15 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
再请大侠一看,给与指点。

TA的精华主题

TA的得分主题

发表于 2019-1-1 12:10 | 显示全部楼层
魏老师:页眉和页脚这块,我也不太会。我从《VBA从入门到精通第二版》(PDF)这本电子书里截图了页眉和页脚相关知识,你有空看看吧,看能否帮上忙。
VBA-页眉和页脚.rar (631.56 KB, 下载次数: 237)

TA的精华主题

TA的得分主题

 楼主| 发表于 2019-1-1 14:59 | 显示全部楼层
413191246se 发表于 2019-1-1 12:10
魏老师:页眉和页脚这块,我也不太会。我从《VBA从入门到精通第二版》(PDF)这本电子书里截图了页眉和页脚 ...

无论怎样还是感谢你,谢谢你的热心。

TA的精华主题

TA的得分主题

 楼主| 发表于 2019-1-1 15:37 | 显示全部楼层
我自己借鉴别人的代码,琢磨了一下,还是没有达到理想结果。代码如下:
Sub 批量设置页眉页脚()
For i = 1 To ActiveDocument.Sections.Count
    If i Mod 2 <> 0 Then
      With ActiveDocument.Sections(i)
          .Headers(1).LinkToPrevious = False
          With .Headers(wdHeaderFooterPrimary).Range
            .Text = ActiveDocument.Sections(i).Range.Text
          End With
          .Footers(1).LinkToPrevious = False
          With .Footers(wdHeaderFooterPrimary).Range '.Frames(1).Range
            .InsertBefore ActiveDocument.Sections(i).Range.Text
            .InsertBefore "第"
            .InsertAfter "页"
            .Font.NameAscii = .Font.NameFarEast
            .ParagraphFormat.RightIndent = 14
            .ParagraphFormat.LeftIndent = 14
          End With
       End With
     End If
Next
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2019-1-1 16:13 | 显示全部楼层
再改为下列代码,好一点点,还是没有达到理想效果,再次请求达人指点。
Sub 批量设置页眉页脚()
    For Each oSec In ActiveDocument.Sections
         Set myRange = oSec.Headers(wdHeaderFooterPrimary).Range
         myRange.Delete '删除页眉中的内容
         myRange.ParagraphFormat.Borders(wdBorderBottom).LineStyle = wdLineStyleNone '段落下边框线
         Set rng = oSec.Footers(wdHeaderFooterPrimary).Range
         rng.Delete '删除页脚中的内容
    Next
For i = 1 To ActiveDocument.Sections.Count
    If i Mod 2 <> 0 Then
      With ActiveDocument.Sections(i)
          With .Headers(wdHeaderFooterPrimary)
            .LinkToPrevious = False
            .Range.Text = Left(ActiveDocument.Sections(i).Range.Text, Len(ActiveDocument.Sections(i).Range.Text) - 2)
          End With
          With .Footers(wdHeaderFooterPrimary)
               .LinkToPrevious = False
             With .Range  '.Frames(1)
                  .InsertBefore Left(ActiveDocument.Sections(i).Range.Text, Len(ActiveDocument.Sections(i).Range.Text) - 2)
                  .InsertBefore "第"
                  .InsertAfter "页"
                  .Font.NameAscii = .Font.NameFarEast
                  .ParagraphFormat.RightIndent = 14
                  .ParagraphFormat.LeftIndent = 14
            End With
          End With
       End With
     End If
Next
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2019-1-1 16:25 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
再次优化,但还是没有页码显示。再次求救大侠指点。

Sub 批量设置页眉页脚()
    For Each oSec In ActiveDocument.Sections
         Set myRange = oSec.Headers(wdHeaderFooterPrimary).Range
         myRange.Delete '删除页眉中的内容
         myRange.ParagraphFormat.Borders(wdBorderBottom).LineStyle = wdLineStyleNone '段落下边框线
         Set rng = oSec.Footers(wdHeaderFooterPrimary).Range
         rng.Delete '删除页脚中的内容
    Next
For i = 1 To ActiveDocument.Sections.Count
    If i Mod 2 <> 0 Then
      With ActiveDocument.Sections(i)
          With .Headers(wdHeaderFooterPrimary)
            .LinkToPrevious = False
            .Range.Text = Left(ActiveDocument.Sections(i).Range.Text, Len(ActiveDocument.Sections(i).Range.Text) - 2)
          End With
          With .Footers(wdHeaderFooterPrimary)
               .LinkToPrevious = False
             With .Range  '.Frames(1)
                  .InsertBefore "第"
                  .InsertBefore Left(ActiveDocument.Sections(i).Range.Text, Len(ActiveDocument.Sections(i).Range.Text) - 2)
                  .InsertAfter "页"
                  .Font.NameAscii = .Font.NameFarEast
                  .ParagraphFormat.RightIndent = 14
                  .ParagraphFormat.LeftIndent = 14
            End With
          End With
       End With
     End If
Next
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2019-1-1 16:38 | 显示全部楼层
再进一步优化,现在还有两个问题;
1、页眉中为什么含有分页符,如何做到其中不含呢?
2、为什么“页”跑到了前面去了,而且和后面的隔断了?
代码如下,请大侠进一步优化。
Sub 批量设置页眉页脚()
    For Each oSec In ActiveDocument.Sections
         Set myRange = oSec.Headers(wdHeaderFooterPrimary).Range
         myRange.Delete '删除页眉中的内容
         myRange.ParagraphFormat.Borders(wdBorderBottom).LineStyle = wdLineStyleNone '段落下边框线
         Set rng = oSec.Footers(wdHeaderFooterPrimary).Range
         rng.Delete '删除页脚中的内容
    Next
For i = 1 To ActiveDocument.Sections.Count
    If i Mod 2 <> 0 Then
      With ActiveDocument.Sections(i)
          With .Headers(wdHeaderFooterPrimary)
            .LinkToPrevious = False
            .Range.Text = Left(ActiveDocument.Sections(i).Range.Text, Len(ActiveDocument.Sections(i).Range.Text) - 2)
          End With
          With .Footers(wdHeaderFooterPrimary)
               .LinkToPrevious = False
               .PageNumbers.Add PageNumberAlignment:=wdAlignPageNumberRight, FirstPage:=True
             With .Range  '.Frames(1)
                  .InsertBefore "第"
                  .InsertBefore Left(ActiveDocument.Sections(i).Range.Text, Len(ActiveDocument.Sections(i).Range.Text) - 2)
                  .InsertAfter "页"
                  .Font.NameAscii = .Font.NameFarEast
                  .ParagraphFormat.RightIndent = 14
                  .ParagraphFormat.LeftIndent = 14
            End With
          End With
       End With
     End If
Next
End Sub

TA的精华主题

TA的得分主题

发表于 2019-1-1 19:37 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2019-1-1 19:50 | 显示全部楼层
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

关闭

最新热点上一条 /1 下一条

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

GMT+8, 2024-4-26 05:15 , Processed in 0.043264 second(s), 15 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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