ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[求助] 以下代码为何不能批量执行修改选定文件夹内的页边距

[复制链接]

TA的精华主题

TA的得分主题

发表于 2019-4-28 16:03 | 显示全部楼层 |阅读模式
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
本帖最后由 qinhuan66 于 2019-4-28 16:18 编辑
  1. 以下代码为何不能批量执行修改选定文件夹内所有WORD文档的页边距
复制代码
测试.rar (53.78 KB, 下载次数: 4)
  1. Sub 修改文件()
  2. Dim fol As Object, doc As Document, pa$, f$
  3.     Dim h As HeaderFooter, se As Section, s$, T$
  4.     Application.ScreenUpdating = False
  5.     Set fol = CreateObject("Shell.Application").BrowseForFolder(0, "选取目标文件夹", 0)
  6.     If Not fol Is Nothing Then pa$ = fol.Items.Item.Path Else MsgBox "请选目标文件夹": Exit Sub
  7.   '-----------------------------------------------------------------------------------------------
  8.     Call 页边距
  9.   '-----------------------------------------------------------------------------------------------
  10. End Sub

  11. Sub 页边距()
  12. '修改页边距
  13. Dim s As Section
  14.     For Each s In ActiveDocument.Sections
  15.         With s.PageSetup
  16.             .TopMargin = CentimetersToPoints(2.7)
  17.             .BottomMargin = CentimetersToPoints(3.5)
  18.         End With
  19.     Next
  20. End Sub
复制代码

TA的精华主题

TA的得分主题

发表于 2019-4-29 00:04 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
楼主,你的代码并未打开文档,故无法设置页边距。请试试以下代码(请将重要资料打包备份好试验):
  1. Sub test批量设置页边距()
  2. 'code by 捷克人
  3.     On Error Resume Next
  4.     Dim fd As FileDialog, p$, doc As Document, n&
  5.     Set fd = Application.FileDialog(msoFileDialogFolderPicker)
  6.     If fd.Show = -1 Then p = fd.SelectedItems(1) Else End
  7.     Set fd = Nothing
  8.     If MsgBox("是否处理文件夹 " & p & " ?", 4 + 48) = vbNo Then End
  9.     Dim FileNameWithPath As Variant, ListOfFilenamesWithParh As New Collection
  10.     Call FileSearchByHavrda(ListOfFilenamesWithParh, p, "*.doc", True)
  11.     For Each FileNameWithPath In ListOfFilenamesWithParh
  12.         Set doc = Documents.Open(FileName:=FileNameWithPath, Visible:=False)
  13.         With doc
  14.             With .PageSetup
  15.                 .TopMargin = CentimetersToPoints(2.7)
  16.                 .BottomMargin = CentimetersToPoints(3.5)
  17.             End With
  18.             .Close savechanges:=wdSaveChanges
  19.         End With
  20.         n = n + 1
  21.     Next FileNameWithPath
  22.     If ListOfFilenamesWithParh.Count = 0 Then MsgBox "File not found!"
  23.     MsgBox "处理完毕!共处理 " & n & " 个文档!", 0 + 48
  24. End Sub
  25. Sub FileSearchByHavrda(pFoundFiles As Collection, pPath As String, pMask As String, pIncludeSubdirectories As Boolean)
  26.     Dim DirFile As String, CollectionItem As Variant, SubDirCollection As New Collection
  27.     pPath = Trim(pPath)
  28.     If Right(pPath, 1) <> "" Then pPath = pPath & ""
  29.     DirFile = Dir(pPath & pMask)
  30.     Do While DirFile <> ""
  31.         pFoundFiles.Add pPath & DirFile
  32.         DirFile = Dir
  33.     Loop
  34.     If Not pIncludeSubdirectories Then Exit Sub
  35.     DirFile = Dir(pPath & "*", vbDirectory)
  36.     Do While DirFile <> ""
  37.         If DirFile <> "." And DirFile <> ".." Then If ((GetAttr(pPath & DirFile) And vbDirectory) = 16) Then SubDirCollection.Add pPath & DirFile
  38.         DirFile = Dir
  39.     Loop
  40.     For Each CollectionItem In SubDirCollection
  41.          Call FileSearchByHavrda(pFoundFiles, CStr(CollectionItem), pMask, pIncludeSubdirectories)
  42.     Next
  43. End Sub
复制代码

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2019-4-29 00:08 | 显示全部楼层
楼主,如果每节均设置为相同的页边距,其实可以设置整个文档为某个页边距,何必单独设置“节”呢!?这一点我也糊涂了。以下代码仅供参考(页边距也是录制宏,但精简了,分纵向、横向):
  1. Sub PaperSetup()
  2.     Dim s As Section
  3.     For Each s In ActiveDocument.Sections
  4.         With s.PageSetup
  5.             If .Orientation = wdOrientPortrait Then
  6.                 .TopMargin = CentimetersToPoints(2.54)
  7.                 .BottomMargin = CentimetersToPoints(2.54)
  8.                 .LeftMargin = CentimetersToPoints(3.17)
  9.                 .RightMargin = CentimetersToPoints(3.17)
  10.                 .PageWidth = CentimetersToPoints(21)
  11.                 .PageHeight = CentimetersToPoints(29.7)
  12.             Else
  13.                 .TopMargin = CentimetersToPoints(2.5)
  14.                 .BottomMargin = CentimetersToPoints(2.5)
  15.                 .LeftMargin = CentimetersToPoints(2.54)
  16.                 .RightMargin = CentimetersToPoints(2.54)
  17.                 .PageWidth = CentimetersToPoints(29.7)
  18.                 .PageHeight = CentimetersToPoints(21)
  19.             End If
  20.             .HeaderDistance = CentimetersToPoints(1.5)
  21.             .FooterDistance = CentimetersToPoints(1.75)
  22.         End With
  23.     Next
  24. End Sub
复制代码

评分

2

查看全部评分

TA的精华主题

TA的得分主题

发表于 2019-4-29 00:16 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
楼主,请注意:必须双击文件夹,直到打开你要的文件夹后,按“确定”。另外,请做好文档备份。
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

关闭

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

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

GMT+8, 2024-4-25 20:15 , Processed in 0.036018 second(s), 17 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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