美文网首页
Word VBA实现批量格式化多个word文件

Word VBA实现批量格式化多个word文件

作者: 舍无由 | 来源:发表于2019-01-30 22:38 被阅读22次

本程序转自批量格式设置word

作者半点忧伤

'请确保所需处理的word文档在同一文件夹下
'本程序代码在Word VBA中执行
Sub 批量格式设置()  '此代码为指定文件夹中所有选取的WORD文件的进行格式设置
Dim MyDialog As FileDialog, vrtSelectedItem As Variant, Doc As Document
' On Error Resume Next '忽略错误
'定义一个文件夹选取对话框
Set MyDialog = Application.FileDialog(msoFileDialogFilePicker)
With MyDialog
.Filters.Clear '清除所有文件筛选器中的项目
'原文为.Filters.Add "所有 WORD 文件", "*.doc", 1 '增加筛选器的项目为所有WORD文件
'舍无由改为了如下语句,以便同时能打开.doc和.docx文件
.Filters.Add "所有 WORD 文件", "*.doc;*.docx", 1 '增加筛选器的项目为所有WORD文件:.doc和.docx文件
.AllowMultiSelect = True '允许多项选择
If .Show = -1 Then '确定
Application.ScreenUpdating = False
For Each vrtSelectedItem In .SelectedItems '在所有选取项目中循环
Set Doc = Documents.Open(FileName:=vrtSelectedItem, Visible:=False)
With Doc
With .PageSetup '进行页面设置
.Orientation = wdOrientPortrait '页面方向为纵向
.TopMargin = CentimetersToPoints(4.1) '上边距为4.1cm
.BottomMargin = CentimetersToPoints(4.1) '下边距为4.1cm
.LeftMargin = CentimetersToPoints(3.05) '左边距为3.05cm
.RightMargin = CentimetersToPoints(3.05) '右边距为3.05com
.Gutter = CentimetersToPoints(0) '装订线0cm
.HeaderDistance = CentimetersToPoints(1.5)  '页眉1.5cm
.FooterDistance = CentimetersToPoints(1.75) '页脚1.75cm
.PageWidth = CentimetersToPoints(21) '纸张宽21cm
.PageHeight = CentimetersToPoints(29.7) '纸张高29.7cm
.SectionStart = wdSectionNewPage '节的起始位置:新建页
.OddAndEvenPagesHeaderFooter = False '不勾选“奇偶页不同”
.DifferentFirstPageHeaderFooter = False '不勾选“首页不同”
.VerticalAlignment = wdAlignVerticalTop '页面垂直对齐方式为“顶端对齐”
.SuppressEndnotes = False '不隐藏尾注
.MirrorMargins = False '不设置首页的内外边距
.BookFoldRevPrinting = False '不设置手动双面打印
.BookFoldPrintingSheets = 1 '默认打印份数为1
.GutterPos = wdGutterPosLeft '装订线位于左侧
.LayoutMode = wdLayoutModeLineGrid '版式模式为“只指定行网格”
End With
.Close True
End With
            Next
            Application.ScreenUpdating = True
        End If
    End With
    MsgBox "格式化文档操作设置完毕!", vbInformation
End Sub

2019年1月30日,将上述代码复制到Word VBA中,舍无由在Word 2013中亲测没有问题,可以实现多个Word文件的格式化。

相关文章

网友评论

      本文标题:Word VBA实现批量格式化多个word文件

      本文链接:https://www.haomeiwen.com/subject/yyjhsqtx.html