ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[求助] 如何遍历页眉页脚,取消链接到前一页。并在横向页面在左边插入页码?

[复制链接]

TA的精华主题

TA的得分主题

发表于 2021-2-17 14:31 | 显示全部楼层 |阅读模式
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
    Dim oDoc As Document
    Set oDoc = Word.ActiveDocument
    Dim oSec As Section
    With oDoc
        '先遍历所有的节对象
        For Each oSec In .Sections
            With oSec
                With .Headers(wdHeaderFooterPrimary)
                .LinkToPrevious = False    '对页眉执行相关的操作
                End With
                With .Footers(wdHeaderFooterPrimary)
          .LinkToPrevious = False     '对页脚执行相关的操作
                End With
            End With
        Next
    End With

网上找来一段代码,自己修改了一下,链接到上一节好象不能全部取消。 请高手们看一下。

例子.rar

19.53 KB, 下载次数: 133

TA的精华主题

TA的得分主题

发表于 2021-2-18 12:22 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
楼主,你提供的附件,昨晚我 2 个半小时没搞定!刚才我又想了一下,按照你的要求,可以不用代码,假设在 Word 2019 中,点击:插入选项卡——页脚——编辑页脚——光标定位在横向页码上,再点击:开始选项卡——左对齐——OK!就这么简单!我觉得就达到了你的目的。用代码反而不易搞定。

TA的精华主题

TA的得分主题

发表于 2021-2-18 15:27 | 显示全部楼层
有奇偶不同的时候 要区别处理

TA的精华主题

TA的得分主题

 楼主| 发表于 2021-2-18 17:04 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
本帖最后由 小声说话666 于 2021-2-18 18:59 编辑
413191246se 发表于 2021-2-18 12:22
楼主,你提供的附件,昨晚我 2 个半小时没搞定!刚才我又想了一下,按照你的要求,可以不用代码,假设在 Wo ...
由于审核 重复回贴了 。。

TA的精华主题

TA的得分主题

 楼主| 发表于 2021-2-18 17:09 | 显示全部楼层
413191246se 发表于 2021-2-18 12:22
楼主,你提供的附件,昨晚我 2 个半小时没搞定!刚才我又想了一下,按照你的要求,可以不用代码,假设在 Wo ...

手工操作我录了一个视频,录制宏不能重动作。 我的思路是反向遍历所有页眉页脚断开链接到上一页。删除所有横向页码,再在所有横向页插入页码。
插入横向页码视频请下载观看(因为文件分奇偶页,须把单页补全为奇偶页全有才能操作,要不会出现还有页眉页脚链接上一页的情况):下载:https://wwa.lanzous.com/idmY4lt1uab 密码:hue3

另:我使用的是WPS2016 没有安装WORD

TA的精华主题

TA的得分主题

 楼主| 发表于 2021-2-18 17:16 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
ming0018 发表于 2021-2-18 15:27
有奇偶不同的时候 要区别处理

就是,奇偶页和首页不同,还有插入目录后节不同,真让人头大。

TA的精华主题

TA的得分主题

发表于 2021-2-18 17:29 | 显示全部楼层
小声说话666 发表于 2021-2-18 17:16
就是,奇偶页和首页不同,还有插入目录后节不同,真让人头大。

弄清楚原理 才能真正玩转word

TA的精华主题

TA的得分主题

发表于 2021-2-24 17:13 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2021-2-24 17:33 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
  1. Sub 插入页码_外侧1()
  2.     Dim s As Section
  3.     Dim n As String
  4.     Dim asection As Section
  5.    
  6.    Application.ScreenUpdating = False '关闭屏幕刷新
  7.     On Error Resume Next  '设置错误处理
  8.     Dim 节 As Section
  9.     Dim 总节数  As Long, 计数器 As Long
  10.    
  11.     If Documents.Count <> 0 Then
  12.             总节数 = ActiveDocument.Sections.Count
  13.     Else
  14.             Exit Sub '如果没有打开文档直接退出
  15.     End If

  16.     For 计数器 = 2 To 总节数
  17.         With ActiveDocument
  18.                 If .Sections(计数器).PageSetup.Orientation <> .Sections(计数器 - 1).PageSetup.Orientation Or wdOrientLandscape Then
  19.                   
  20.                     .Sections(计数器).Footers(wdHeaderFooterPrimary).LinkToPrevious = False
  21.                     .Sections(计数器).Footers(wdHeaderFooterEvenPages).LinkToPrevious = False
  22.                     
  23.                     '如果本节页面方向和前节页面方向不同,则关闭本节同前开关设置
  24.                 End If
  25.         End With
  26.     Next 计数器  '取消所有节的同前开关设置
  27.    
  28.     For Each asection In ActiveDocument.Sections

  29.        With asection.Headers(wdHeaderFooterPrimary)
  30.             .Range.Delete
  31.             .Range.ParagraphFormat.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
  32.        End With
  33.     Next

  34. '删除页脚
  35. For Each asection In ActiveDocument.Sections
  36.    
  37.    With asection.Footers(wdHeaderFooterPrimary)
  38.         .Range.Delete
  39.         .Range.ParagraphFormat.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
  40.    End With
  41.    With asection.Footers(wdHeaderFooterEvenPages)
  42.         .Range.Delete
  43.         .Range.ParagraphFormat.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
  44.    End With
  45. Next

  46.     n = InputBox("请输入章节号(不需要章节请输入一个空格):  " & vbCr & " ")
  47.    
  48.     For Each s In ActiveDocument.Sections
  49.         s.Footers(1).Range.Delete
  50.         With s.Footers(1).Range.Sections(1).Headers(1).PageNumbers
  51.             .NumberStyle = wdPageNumberStyleArabic
  52.             .HeadingLevelForChapter = 0
  53.             .IncludeChapterNumber = False
  54.             .ChapterPageSeparator = wdSeparatorHyphen
  55.             .RestartNumberingAtSection = False
  56.             .StartingNumber = 0
  57.         End With
  58.       
  59.         s.Footers(1).PageNumbers.Add 4, True
  60.       
  61.          With s.Footers(wdHeaderFooterPrimary).Range.Frames(1).Range
  62.             .Select
  63.             With Selection
  64.             .InsertBefore n
  65.                 With .Font
  66.                     .Name = "宋体"
  67.                     .Name = "Times New Roman"
  68.                     .Size = 9
  69.                 End With
  70.                 .ParagraphFormat.CharacterUnitRightIndent = 1.5
  71.             End With
  72.         End With
  73.         With s.Footers(wdHeaderFooterEvenPages).Range.Frames(1).Range
  74.             .Select
  75.             With Selection
  76.            
  77.             .InsertBefore n
  78.                 With .Font
  79.                     .Name = "宋体"
  80.                     .Name = "Times New Roman"
  81.                     .Size = 9
  82.                 End With
  83.                 .ParagraphFormat.CharacterUnitRightIndent = 1.5
  84.             End With
  85.         End With

  86.     Next
  87.     ActiveWindow.ActivePane.Close
  88.     ActiveWindow.View.Type = wdPrintView
  89.    
  90.     For 计数器 = 2 To 总节数
  91.        ActiveDocument.Sections(计数器).PageSetup.OddAndEvenPagesHeaderFooter = True
  92.     If ActiveDocument.Sections(计数器).PageSetup.Orientation = wdOrientLandscape Then
  93.     '如果是横向页面,则进行调整
  94.         With ActiveDocument.Sections(计数器).Footers(wdHeaderFooterPrimary).Range.Frames(1)
  95.             
  96.             .Width = CentimetersToPoints(0.42)
  97.             .HeightRule = wdFrameAtLeast
  98.             .Height = CentimetersToPoints(1.05)
  99.            
  100.             .HorizontalPosition = MillimetersToPoints(16)                                '页码距短边15毫米,左下位置
  101.             .RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
  102.             .VerticalPosition = MillimetersToPoints(188)
  103.             .RelativeVerticalPosition = wdRelativeVerticalPositionPage
  104.             .HorizontalDistanceFromText = MillimetersToPoints(0)
  105.             .VerticalDistanceFromText = MillimetersToPoints(0)

  106.             .Range.Orientation = wdTextOrientationDownward  '调整页码文字方向
  107.         End With
  108.         
  109.         With ActiveDocument.Sections(计数器).Footers(wdHeaderFooterEvenPages).Range.Frames(1)
  110.             
  111.             .Width = CentimetersToPoints(0.42)
  112.             .HeightRule = wdFrameAtLeast
  113.             .Height = CentimetersToPoints(1.05)
  114.          
  115.             .HorizontalPosition = MillimetersToPoints(16)                                '页码距短边15毫米,右下位置
  116.             .RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
  117.             .VerticalPosition = MillimetersToPoints(15)
  118.             .RelativeVerticalPosition = wdRelativeVerticalPositionPage
  119.             .HorizontalDistanceFromText = MillimetersToPoints(0)
  120.             .VerticalDistanceFromText = MillimetersToPoints(0)
  121.             
  122.             .Range.Orientation = wdTextOrientationDownward  '调整页码文字方向
  123.         End With
  124.     End If
  125.   
  126.         ActiveDocument.Sections(计数器).Headers(wdHeaderFooterPrimary) _
  127.         .Range.ParagraphFormat.Borders(wdBorderBottom).LineStyle = wdLineStyleNone     '用于防止页眉出现横线
  128.            
  129.         ActiveDocument.Sections(计数器).Headers(wdHeaderFooterEvenPages) _
  130.         .Range.ParagraphFormat.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
  131.         
  132.         With ActiveDocument.Sections(计数器).Footers(wdHeaderFooterEvenPages).Range.Font
  133.             .Name = "Times New Roman"
  134.             .Size = 9
  135.         End With
  136.    
  137.     Next 计数器

  138.     Application.ScreenUpdating = True  '恢复屏幕刷新
  139. End Sub
复制代码

TA的精华主题

TA的得分主题

 楼主| 发表于 2021-2-24 23:42 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
19820713chen 发表于 2021-2-24 17:13
是要这样的效果吗?

公文 一字线的效果

公文一字线效果

公文一字线效果
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2024-4-16 12:26 , Processed in 0.046853 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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