VBA设置文档格式

Sub FormatDocument()
    
    ' 设置整个文档的字体、大小、颜色
    ActiveDocument.Select: Selection.ClearFormatting '文档格式清除
    ActiveDocument.Content.Font.Name = "宋体"
    ActiveDocument.Content.Font.Size = 12
    ActiveDocument.Content.Font.ColorIndex = wdBlack '黑色
    
    ' 设置整个文档的行距
    ActiveDocument.Content.ParagraphFormat.LineSpacingRule = wdLineSpaceSingle
    
    ' 设置整个文档的页边距
    ActiveDocument.PageSetup.TopMargin = CentimetersToPoints(1.27)
    ActiveDocument.PageSetup.BottomMargin = CentimetersToPoints(1.27)
    ActiveDocument.PageSetup.LeftMargin = CentimetersToPoints(1.27)
    ActiveDocument.PageSetup.RightMargin = CentimetersToPoints(1.27)
    ActiveDocument.PageSetup.PageWidth = CentimetersToPoints(21)   '页面宽度
    ActiveDocument.PageSetup.PageHeight = CentimetersToPoints(29.7)  '页面高度
    
    ' 设置页面方向
    'ActiveDocument.PageSetup.Orientation = wdOrientLandscape   '横向
    ActiveDocument.PageSetup.Orientation = wdOrientPortrait     '竖向
    
    ' 设置文档的页眉和页脚
    ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Text = " "
    ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range.Text = " "
    ActiveDocument.PageSetup.HeaderDistance = 22.677166
    ActiveDocument.PageSetup.FooterDistance = 22.677166
    ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.ParagraphFormat.Borders(wdBorderBottom).LineStyle = wdLineStyleNone '删除页眉横线
    
    '插入页码
    ActiveDocument.Sections(ActiveDocument.Sections.Count).Footers(wdHeaderFooterPrimary).Range.Select

End Sub

posted @ 2023-06-07 03:07  镜花缘  阅读(126)  评论(0编辑  收藏  举报