VBA设置word格式(页面,段落,表格,图片)

Sub glkCurrentDocPageSetup()

 Dim glkDoc As Document ‘定义变量
 Set glkDoc = Application.ActiveDocument ‘设置为当前文档
 With glkDoc ‘设置文档参数
 With .PageSetup ‘设置【页面设置】参数
.Orientation = wdOrientPortrait ‘页面方向为纵向
.TopMargin = CentimetersToPoints(3) ‘上边距为3cm
.BottomMargin = CentimetersToPoints(3) ‘下边距为3cm
.LeftMargin = CentimetersToPoints(2.6) ‘左边距为2.6cm
.RightMargin = CentimetersToPoints(2.3) ‘右边距为2.6com
.Gutter = CentimetersToPoints(0) ‘装订线0cm
.HeaderDistance = CentimetersToPoints(1.5) ‘页眉1.5cm
.FooterDistance = CentimetersToPoints(2)  ‘页脚2cm
.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 = 30 ‘行距30磅
.Alignment = wdAlignParagraphJustify ‘段落设置为两端对齐
.WidowControl = False ‘不勾选“孤行控制”
.KeepWithNext = False ‘不勾选“与下段同页”
.KeepTogether = False ‘不勾选“段中不分页”
.PageBreakBefore = False ‘不勾选“段前同页”
.NoLineNumber = False ‘不勾选“取消行号”
.Hyphenation = True ‘不勾选“允许西文在单词中间换行”
.FirstLineIndent = CentimetersToPoints(2) ‘首行缩进2cm
.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 .Styles(“标题 1”).Font
    .Color = wdColorBlack
    .Bold = False ‘标题不再加粗
    .Size = 22 ‘三号
    .Name = “宋体”
    End With
    
     With .Styles(“标题 2”).Font
    .Color = wdColorBlack
    .Bold = False ‘黑体一般不再加粗
    .Size = 16 ‘三号
    .Name = “楷体”
    End With
    With .Styles(“正文”).Font    ‘设置正文普通文字格式
    .Color = wdColorBlack
    .Bold = False
    .Size = 10
    .Name = “宋体”
    End With
 End With ‘结束文档参数设置
  
 
End Sub
Sub 表格处理()
    ‘功能:光标在表格中处理当前表格;否则处理所有表格!
    Application.ScreenUpdating = False  ‘关闭屏幕刷新
    Application.DisplayAlerts = False  ‘关闭提示
    On Error Resume Next  ‘忽略错误
    ‘***************************************************************************
    Dim mytable As Table, i As Long
    If Selection.Information(wdWithInTable) = True Then i = 1
    For Each mytable In ActiveDocument.Tables
        If i = 1 Then Set mytable = Selection.Tables(1)
        With mytable
            ‘取消底色
            .Shading.ForegroundPatternColor = wdColorAutomatic
            .Shading.BackgroundPatternColor = wdColorAutomatic
            Options.DefaultHighlightColorIndex = wdNoHighlight
            .Range.HighlightColorIndex = wdNoHighlight
            .Style = “表格主题”
            
            ‘单元格边距
            .TopPadding = PixelsToPoints(0, True) ‘设置上边距为0
            .BottomPadding = PixelsToPoints(0, True) ‘设置下边距为0
            .LeftPadding = PixelsToPoints(0, True)  ‘设置左边距为0
            .RightPadding = PixelsToPoints(0, True) ‘设置右边距为0
            .Spacing = PixelsToPoints(0, True) ‘允许单元格间距为0
            .AllowPageBreaks = True ‘允许断页
            ‘.AllowAutoFit = True ‘允许自动重调尺寸
            
            ‘设置边框
            .Borders(wdBorderLeft).LineStyle = wdLineStyleNone
            .Borders(wdBorderRight).LineStyle = wdLineStyleNone
            .Borders(wdBorderTop).LineStyle = wdLineStyleThinThickMedGap
            .Borders(wdBorderTop).LineWidth = wdLineWidth225pt
            .Borders(wdBorderBottom).LineStyle = wdLineStyleThickThinMedGap
            .Borders(wdBorderBottom).LineWidth = wdLineWidth225pt
            
            With .Rows
                .WrapAroundText = False ‘取消文字环绕
                .Alignment = wdAlignRowCenter ‘表水平居中  wdAlignRowLeft ‘左对齐
                .AllowBreakAcrossPages = False ‘不允许行断页
                .HeightRule = wdRowHeightExactly ‘行高设为最小值   wdRowHeightAuto ‘行高设为自动
                .Height = CentimetersToPoints(0) ‘上面缩进量为0
                .LeftIndent = CentimetersToPoints(0) ‘左面缩进量为0
            End With
            
            With .Range
                With .Font ‘字体格式
                    .Name = “宋体”
                    .Name = “Times New Roman”
                    .Color = wdColorAutomatic ‘自动字体颜色
                    .Size = 12
                    .Kerning = 0
                    .DisableCharacterSpaceGrid = True
                End With
                
                With .ParagraphFormat ‘段落格式
                    .CharacterUnitFirstLineIndent = 0 ‘取消首行缩进
                    .FirstLineIndent = CentimetersToPoints(0) ‘取消首行缩进
                    .LineSpacingRule = wdLineSpaceSingle ‘单倍行距  wdLineSpaceExactly ‘行距固定值
                    ‘.LineSpacing = 20 ‘设置行间距为20磅,配合行距固定值
                    .Alignment = wdAlignParagraphCenter ‘单元格水平居中
                    .AutoAdjustRightIndent = False
                    .DisableLineHeightGrid = True
                End With
                
                .Cells.VerticalAlignment = wdCellAlignVerticalCenter  ‘单元格垂直居中
                
            End With
            
            ‘设置首行格式
            .Cell(1, 1).Select ‘ 选中第一个单元格
            With Selection
                .SelectRow ‘选中当前行
                Selection.Rows.HeadingFormat = wdToggle ‘自动标题行重复
                .Range.Font.Bold = True ‘表头加粗黑体
                .Shading.ForegroundPatternColor = wdColorAutomatic ‘首行自动颜色
                .Shading.BackgroundPatternColor = -603923969 ‘首行底纹填充
            End With
            
            ‘自动调整表格
            .Columns.PreferredWidthType = wdPreferredWidthAuto
            .AutoFitBehavior (wdAutoFitContent) ‘根据内容调整表格
            .AutoFitBehavior (wdAutoFitWindow) ‘根据窗口调整表格
            
        End With
        
        If i = 1 Then Exit For
    Next
    ‘***************************************************************************
    Err.Clear: On Error GoTo 0 ‘恢复错误捕捉
    Application.DisplayAlerts = True  ‘开启提示
    Application.ScreenUpdating = True   ‘开启屏幕刷新
End Sub

‘设置图片大小
Dim Shap As InlineShape

For Each Shap In ActiveDocument.InlineShapes

If Shap.Type = wdInlineShapePicture Then

Shap.LockAspectRatio = msoFalse ‘不锁定纵横比

Shap.Width = CentimetersToPoints(10) ‘宽10CM

Shap.Height = CentimetersToPoints(7) ‘高7CM

End If

Next
‘设置页码
Sub sutAddPageNum1()
   With ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary)
       Set rng = .Range
       rng.Text = “第 “
       rng.Font.Size = 16 ‘三号
       rng.Collapse wdCollapseEnd
       ActiveDocument.Fields.Add rng, wdFieldPage, “Page”
       Set rng = .Range
       rng.Collapse wdCollapseEnd
       rng.Text = ” 页 / 共 “
       rng.Collapse wdCollapseEnd
       ActiveDocument.Fields.Add rng, wdFieldNumPages, “Pages”
       Set rng = .Range
       rng.Collapse wdCollapseEnd
       rng.Text = ” 页 “
       .Range.Fields.Update
       .Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
   End With
End Sub
 

    原文作者:variation8
    原文地址: https://blog.csdn.net/qq_31151123/article/details/121919748
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞