ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

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

[复制链接]

TA的精华主题

TA的得分主题

 楼主| 发表于 2021-2-24 23:59 | 显示全部楼层

我使用的WPS2016, 由于单位要求不能使用WORD,故一直使用WPS。
发贴前就是参考 http://club.excelhome.net/thread-933750-2-1.html 里的代码想修改一下,但在WPS中没达到我要的效果。
无奈只因是小小白一枚,只会抄抄改改,大部分代码还看不懂,故再发贴求助。

目前进展:
去掉所有节链接主要是:
wdHeaderFooterPrimary
wdHeaderFooterEvenPages
我目前有点感觉,似懂非懂已经搞定。
但在WPS里 遍历横向页面往文本框里插入页码域本人还不会操作。还望指点



9楼代码,我运行后效果如下:

方向不对

方向不对

TA的精华主题

TA的得分主题

发表于 2021-2-25 09:38 | 显示全部楼层
这样的效果?
image.png

TA的精华主题

TA的得分主题

发表于 2021-2-25 09:40 | 显示全部楼层
只能用在word,wps可能用不了!
  1. Sub 插入页码_左右()
  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.    
  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.            
  65.              .InsertBefore "— "
  66.             .InsertAfter " —"
  67.                 With .Font
  68.                     .Name = "宋体"
  69.                     .Name = "Times New Roman"
  70.                     .Size = 9
  71.                 End With
  72.                 .ParagraphFormat.CharacterUnitRightIndent = 1.5
  73.             End With
  74.         End With
  75.         With s.Footers(wdHeaderFooterEvenPages).Range.Frames(1).Range
  76.             .Select
  77.             With Selection
  78.            
  79.             .InsertBefore "— "
  80.             .InsertAfter " —"
  81.                 With .Font
  82.                     .Name = "宋体"
  83.                     .Name = "Times New Roman"
  84.                     .Size = 9
  85.                 End With
  86.                 .ParagraphFormat.CharacterUnitRightIndent = 1.5
  87.             End With
  88.         End With

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

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

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

TA的精华主题

TA的得分主题

 楼主| 发表于 2021-2-25 21:50 | 显示全部楼层
19820713chen 发表于 2021-2-25 09:40
只能用在word,wps可能用不了!

多谢大神代码。

经WPS测试,未达效果。

可能是WPS不支持     .Range.Orientation = wdTextOrientationDownward  '调整页码文字方向

如果是插入竖文本框就不用调整文字方向了。
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2024-4-27 04:11 , Processed in 0.035907 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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