批量设置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文件
.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(2.2) '上边距为2.2cm
.BottomMargin = CentimetersToPoints(2.2) '下边距为2.2cm
.LeftMargin = CentimetersToPoints(2.5) '左边距为2.5cm
.RightMargin = CentimetersToPoints(2.5) '右边距为2.5com
.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
With .Content.ParagraphFormat '段落格式设置
.LeftIndent = CentimetersToPoints(0) '左缩进0cm
.RightIndent = CentimetersToPoints(0) '右缩进0cm
.SpaceBefore = 0 '段前间距0cm
.SpaceBeforeAuto = False '段前间距不设为“自动”
.SpaceAfter = 0 '段后间距0cm
.SpaceAfterAuto = False '段后间距不设为“自动”
.LineSpacingRule = wdLineSpaceExactly
.LineSpacing = 24 '行距24磅
.Alignment = wdAlignParagraphJustify '段落设置为两端对齐
.WidowControl = False '不勾选“孤行控制”
.KeepWithNext = False '不勾选“与下段同页”
.KeepTogether = False '不勾选“段中不分页”
.PageBreakBefore = False '不勾选“段前同页”
.NoLineNumber = False '不勾选“取消行号”
.Hyphenation = True '不勾选“允许西文在单词中间换行”
.FirstLineIndent = CentimetersToPoints(0) '首行缩进0cm
.OutlineLevel = wdOutlineLevelBodyText '大纲级别为“正文文本”
.CharacterUnitLeftIndent = 0 '段落左缩进0cm
.CharacterUnitRightIndent = 0 '段落右缩进0cm
.CharacterUnitFirstLineIndent = 0 '特殊格式为“无”
.LineUnitBefore = 0 '段前间距为0
.LineUnitAfter = 0 '段后间距为0
.AutoAdjustRightIndent = True '自动调整段落的右缩进
.DisableLineHeightGrid = False '勾选“如果定义了文档网格,则对齐网格”,即指定段落中的字符与行网格对齐
.FarEastLineBreakControl = True '将东亚语言文字的换行规则应用于指定的段落
.WordWrap = True '在指定段落或文本框的西文单词中间断字换行
.HangingPunctuation = True '指定段落中的标点将可以溢出边界
.HalfWidthPunctuationOnTopOfLine = False
.AddSpaceBetweenFarEastAndAlpha = True '自动在指定段落的中文文字和拉丁文字之间添加空格。
.AddSpaceBetweenFarEastAndDigit = True '自动在指定段落中的中文文字与数字之间添加空格
.BaseLineAlignment = wdBaselineAlignAuto '自动调整基线字体对齐方式
End With
''''''''''新增部分
With .Content
With .Font
.NameFarEast = "宋体"
.NameAscii = "Times New Roman"
.Size = 12
End With
With .Paragraphs.First
.Range.Font.Size = 16
.Alignment = wdAlignParagraphCenter
End With
End With
.Close True
End With
Next
Application.ScreenUpdating = True
End If
End With
MsgBox "格式化文档操作设置完毕!", vbInformation
End Sub

你可能感兴趣的:(VBA)