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
 

  • 3
    点赞
  • 83
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Excel VBA操作Word是指通过Excel的VBA编程语言来控制和操作Word应用程序。下面是Excel VBA操作Word的入门篇。 首先,我们需要在Excel中引用Word对象库,这样才能使用Word的相关功能。打开Excel的VBA编辑器,点击菜单栏的"工具",选择"引用",然后勾选"Microsoft Word XX.X Object Library",点击确定。 接下来,我们需要声明一个Word应用程序对象,并创建一个新的Word文档。使用下面的代码可以完成这个任务: ``` Dim wordApp As Object '声明一个Word应用程序对象 Dim wordDoc As Object '声明一个Word文档对象 Set wordApp = CreateObject("Word.Application") '创建一个新的Word应用程序对象 wordApp.Visible = True '设置Word应用程序可见 Set wordDoc = wordApp.Documents.Add '创建一个新的Word文档 ``` 然后,我们可以向Word文档中插入文字、设置字体、样式、段落格式等。使用下面的代码可以实现这些操作: ``` wordDoc.Content.Text = "这是一个用Excel VBA操作Word的示例文档" '向文档插入文字 wordDoc.Content.Font.Name = "Arial" '设置字体 wordDoc.Content.Font.Size = 12 '设置字号 wordDoc.Paragraphs(1).Alignment = 1 '设置段落居中对齐 ``` 此外,我们还可以向Word文档中插入图片表格、超链接等。使用下面的代码可以实现这些操作: ``` wordDoc.InlineShapes.AddPicture "C:\image.jpg" '插入一张图片 wordDoc.Tables.Add Range:=wordDoc.Range, NumRows:=3, NumColumns:=3 '插入一个3行3列的表格 wordDoc.Hyperlinks.Add wordDoc.Range, Address:="http://www.example.com", TextToDisplay:="点击访问示例网站" '插入一个超链接 ``` 最后,我们需要关闭Word文档和应用程序对象以释放资源。使用下面的代码可以完成这个任务: ``` wordDoc.SaveAs "C:\example.docx" '将文档保存为docx格式 wordDoc.Close '关闭文档 wordApp.Quit '关闭Word应用程序 ``` 以上就是Excel VBA操作Word的入门篇内容。通过这些基础知识,我们可以进一步探索更多复杂的Word操作,如格式控制、文本替换、邮件合并等。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

variation8

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值