ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[求助] word vba 怎么设置续上节

[复制链接]

TA的精华主题

TA的得分主题

发表于 2019-6-15 12:02 | 显示全部楼层 |阅读模式
小生编了代码 但是在本机上运行得不到正确结果

本机系统 win10 64bit    vba:2016 32bit

渴望实现:(手动将光标置于正文第1节)用vba设置从正文第2节开始续前节(页眉 页脚)  同时 页码也要续前节(不能新节重新编号)

已有代码的问题:运行并不能保证续前节  有些则可以  一直找不到原因


TA的精华主题

TA的得分主题

 楼主| 发表于 2019-6-15 12:03 | 显示全部楼层
刚刚忘了附件 如下

求助.zip

476.53 KB, 下载次数: 16

TA的精华主题

TA的得分主题

发表于 2019-6-15 12:37 来自手机 | 显示全部楼层
jurdy001 发表于 2019-6-15 12:03
刚刚忘了附件 如下

很简单吧!         

TA的精华主题

TA的得分主题

 楼主| 发表于 2019-6-15 12:42 来自手机 | 显示全部楼层
老师 我编的代码就是不行  麻烦老师帮我看看哪里不对呀

TA的精华主题

TA的得分主题

发表于 2019-6-15 12:53 | 显示全部楼层
jurdy001 发表于 2019-6-15 12:03
刚刚忘了附件 如下
  1. Sub main()
  2.     Dim fag As Boolean, sec As Section
  3.     If ActiveDocument.Sections.Count = 1 Then Exit Sub
  4.     For Each sec In ActiveDocument.Sections
  5.         If Not fag Then
  6. '            设置好页眉
  7. '            设置好页脚
  8. '            设置好页码(包括设置好起始页码)
  9.             fag = True
  10.         Else
  11. '            页眉,设置与上一节相同
  12. '            页脚,设置与上一节相同
  13. '            页码,设置续前节
  14.         End If
  15.     Next
  16. End Sub
复制代码


TA的精华主题

TA的得分主题

 楼主| 发表于 2019-6-15 13:36 | 显示全部楼层

Sub 设置链接页眉页脚且页码续上节()
Application.ScreenUpdating = False

Dim q As String
Dim n As Integer
Dim m As Integer
Dim i As Integer

q = MsgBox("该操作必须要将光标停留在正文的第1节!" & Chr(10) & "请确认是否如此?", vbYesNo, "系统询问")
If q = vbYes Then

n = Selection.Information(wdActiveEndSectionNumber)
m = ActiveDocument.Sections.Count


For i = n + 1 To m

With ActiveDocument.Sections(i)

    .Headers(wdHeaderFooterPrimary).PageNumbers.RestartNumberingAtSection = False
    .Footers(wdHeaderFooterPrimary).PageNumbers.RestartNumberingAtSection = False
   
    .Headers(wdHeaderFooterPrimary).LinkToPrevious = True
    .Footers(wdHeaderFooterPrimary).LinkToPrevious = True


End With

    With ActiveDocument.Sections(i).PageSetup


        .PageWidth = ActiveDocument.Sections(n).PageSetup.PageWidth
        .PageHeight = ActiveDocument.Sections(n).PageSetup.PageHeight

        .TopMargin = ActiveDocument.Sections(n).PageSetup.TopMargin
        .BottomMargin = ActiveDocument.Sections(n).PageSetup.BottomMargin
        .LeftMargin = ActiveDocument.Sections(n).PageSetup.LeftMargin
        .RightMargin = ActiveDocument.Sections(n).PageSetup.RightMargin

        .HeaderDistance = ActiveDocument.Sections(n).PageSetup.HeaderDistance
        .FooterDistance = ActiveDocument.Sections(n).PageSetup.FooterDistance

    End With
   

   
Next


MsgBox "设置完毕!"
End If
Application.ScreenUpdating = True
End Sub


老师 这是我的代码  运行就是不可靠

您那个 我运行好像也不行
(不好意思 刚刚出了些状况 附件并没有代码 )

TA的精华主题

TA的得分主题

 楼主| 发表于 2019-6-15 14:18 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册

老师您好  您的代码我这边也不行  麻烦您看看我的代码哪里有缺陷
Sub 设置链接页眉页脚且页码续上节()
Application.ScreenUpdating = False

Dim q As String
Dim n As Integer
Dim m As Integer
Dim i As Integer

q = MsgBox("该操作必须要将光标停留在正文的第1节!" & Chr(10) & "请确认是否如此?", vbYesNo, "系统询问")
If q = vbYes Then
n = Selection.Information(wdActiveEndSectionNumber)
m = ActiveDocument.Sections.Count

For i = n + 1 To m
    With ActiveDocument.Sections(i)
   
        .Headers(wdHeaderFooterPrimary).PageNumbers.RestartNumberingAtSection = False
        .Footers(wdHeaderFooterPrimary).PageNumbers.RestartNumberingAtSection = False
        
        .Headers(wdHeaderFooterPrimary).LinkToPrevious = True
        .Footers(wdHeaderFooterPrimary).LinkToPrevious = True
   
    End With

    With ActiveDocument.Sections(i).PageSetup

        .PageWidth = ActiveDocument.Sections(n).PageSetup.PageWidth
        .PageHeight = ActiveDocument.Sections(n).PageSetup.PageHeight

        .TopMargin = ActiveDocument.Sections(n).PageSetup.TopMargin
        .BottomMargin = ActiveDocument.Sections(n).PageSetup.BottomMargin
        .LeftMargin = ActiveDocument.Sections(n).PageSetup.LeftMargin
        .RightMargin = ActiveDocument.Sections(n).PageSetup.RightMargin

        .HeaderDistance = ActiveDocument.Sections(n).PageSetup.HeaderDistance
        .FooterDistance = ActiveDocument.Sections(n).PageSetup.FooterDistance

    End With
  
Next
MsgBox "设置完毕!"
End If
Application.ScreenUpdating = True
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2019-6-15 15:03 | 显示全部楼层
本帖最后由 jurdy001 于 2019-6-15 17:33 编辑

本条回复已被删除

TA的精华主题

TA的得分主题

发表于 2019-6-15 18:16 来自手机 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2019-6-15 19:29 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册


Sub 续前节()
    Application.ScreenUpdating = False
    Dim oSection As Section, oHeaderFooter As HeaderFooter
    For Each oSection In ActiveDocument.Sections
        For Each oHeaderFooter In oSection.Headers
            oHeaderFooter.LinkToPrevious = True
        Next
        For Each oHeaderFooter In oSection.Footers
            oHeaderFooter.LinkToPrevious = True
        Next
        With oSection.Footers(wdHeaderFooterPrimary).PageNumbers
            .RestartNumberingAtSection = False
        End With
    Next
    Application.ScreenUpdating = True
End Sub

评分

1

查看全部评分

您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

关闭

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

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

GMT+8, 2024-4-25 05:19 , Processed in 0.045657 second(s), 13 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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