欢迎光临散文网 会员登陆 & 注册

wps 宏 自动排版

2023-10-28 15:00 作者:激仰的南瓜  | 我要投稿

Sub 自动程序()

    ' 设置页面布局和格式

    With ActiveDocument.PageSetup

        .Orientation = wdOrientPortrait ' 设置为纵向页面方向

        .TopMargin = CentimetersToPoints(2) ' 顶部页边距为2厘米

        .BottomMargin = CentimetersToPoints(2) ' 底部页边距为2厘米

        .LeftMargin = CentimetersToPoints(2) ' 左侧页边距为2厘米

        .RightMargin = CentimetersToPoints(2) ' 右侧页边距为2厘米

    End With

    

    ' 设置段落格式

    With ActiveDocument.Content.ParagraphFormat

        .Alignment = wdAlignParagraphJustify ' 对齐方式为两端对齐

        .LineSpacingRule = wdLineSpace1pt5 ' 行间距为1.5倍

        .SpaceAfter = 0 ' 段后间距为0

    End With

    

    ' 设置字体和字号

    With ActiveDocument.Content.Font

        .Name = "宋体" ' 设置字体为宋体

        .Size = 12 ' 设置字号为12磅

    End With

    

    ' 执行自动排版

    ActiveDocument.AutoFormat

    


MsgBox "宏指令执行完成", vbInformation



End Sub


Sub 图片排版()

    Dim shp As Shape

    

    ' 设置页面布局和格式

    With ActiveDocument.PageSetup

        .Orientation = wdOrientPortrait ' 设置为纵向页面方向

        .TopMargin = CentimetersToPoints(2) ' 顶部页边距为2厘米

        .BottomMargin = CentimetersToPoints(2) ' 底部页边距为2厘米

        .LeftMargin = CentimetersToPoints(2) ' 左侧页边距为2厘米

        .RightMargin = CentimetersToPoints(2) ' 右侧页边距为2厘米

    End With

    

    ' 设置段落格式

    With ActiveDocument.Content.ParagraphFormat

        .Alignment = wdAlignParagraphJustify ' 对齐方式为两端对齐

        .LineSpacingRule = wdLineSpace1pt5 ' 行间距为1.5倍

        .SpaceAfter = 0 ' 段后间距为0

    End With

    

    ' 设置字体和字号

    With ActiveDocument.Content.Font

        .Name = "宋体" ' 设置字体为宋体

        .Size = 12 ' 设置字号为12磅

    End With

    

    ' 遍历文档中的每个形状对象

    For Each shp In ActiveDocument.Shapes

        Select Case shp.Type

            Case msoTextBox ' 文本框对象

                ' 设置文本框的段落格式

                With shp.TextFrame.TextRange.ParagraphFormat

                    .Alignment = wdAlignParagraphJustify ' 对齐方式为两端对齐

                    .LineSpacingRule = wdLineSpace1pt5 ' 行间距为1.5倍

                    .SpaceAfter = 0 ' 段后间距为0

                End With

                

                ' 设置文本框的字体和字号

                With shp.TextFrame.TextRange.Font

                    .Name = "宋体" ' 设置字体为宋体

                    .Size = 12 ' 设置字号为12磅

                End With

            

            Case msoPicture ' 图片对象

                ' 调整图片的大小和位置

                shp.LockAspectRatio = msoFalse ' 可以调整宽高比

                shp.Width = CentimetersToPoints(10) ' 设置图片宽度为10厘米

                shp.Height = CentimetersToPoints(10) ' 设置图片高度为10厘米

                shp.Left = CentimetersToPoints(2) ' 设置图片左侧位置为2厘米

                shp.Top = CentimetersToPoints(2) ' 设置图片顶部位置为2厘米

        End Select

    Next shp

    

    ' 执行自动排版

    ActiveDocument.AutoFormat

    

    ' 显示消息框

    MsgBox "自动排版已完成!", vbInformation

End Sub

下面其他都可以就是表格不ok



Sub AutoLayout()

    Dim shp As Shape

    Dim tbl As Table

    

    ' 解除页面设置的锁定

    ActiveDocument.Sections(1).PageSetup.SectionStart = wdSectionContinuous

    

    ' 设置页面布局和格式

    With ActiveDocument.PageSetup

        .Orientation = wdOrientPortrait ' 设置为纵向页面方向

        .TopMargin = CentimetersToPoints(2) ' 顶部页边距为2厘米

        .BottomMargin = CentimetersToPoints(2) ' 底部页边距为2厘米

        .LeftMargin = CentimetersToPoints(2) ' 左侧页边距为2厘米

        .RightMargin = CentimetersToPoints(2) ' 右侧页边距为2厘米

    End With

    

    ' 设置段落格式

    With ActiveDocument.Content.ParagraphFormat

        .Alignment = wdAlignParagraphJustify ' 对齐方式为两端对齐

        .LineSpacingRule = wdLineSpace1pt5 ' 行间距为1.5倍

        .SpaceAfter = 0 ' 段后间距为0

    End With

    

    ' 设置字体和字号

    With ActiveDocument.Content.Font

        .Name = "宋体" ' 设置字体为宋体

        .Size = 12 ' 设置字号为12磅

    End With

    

    ' 遍历文档中的每个形状对象

    For Each shp In ActiveDocument.Shapes

        Select Case shp.Type

            Case msoTextBox ' 文本框对象

                ' 设置文本框的段落格式

                With shp.TextFrame.TextRange.ParagraphFormat

                    .Alignment = wdAlignParagraphJustify ' 对齐方式为两端对齐

                    .LineSpacingRule = wdLineSpace1pt5 ' 行间距为1.5倍

                    .SpaceAfter = 0 ' 段后间距为0

                End With

                

                ' 设置文本框的字体和字号

                With shp.TextFrame.TextRange.Font

                    .Name = "宋体" ' 设置字体为宋体

                    .Size = 12 ' 设置字号为12磅

                End With

            

            Case msoPicture ' 图片对象

                ' 调整图片的大小和位置

                shp.LockAspectRatio = msoFalse ' 可以调整宽高比

                shp.Width = CentimetersToPoints(10) ' 设置图片宽度为10厘米

                shp.Height = CentimetersToPoints(10) ' 设置图片高度为10厘米

                shp.Left = CentimetersToPoints(2) ' 设置图片左侧位置为2厘米

                shp.Top = CentimetersToPoints(2) ' 设置图片顶部位置为2厘米

        End Select

    Next shp

    

    ' 遍历文档中的每个表格对象

    For Each tbl In ActiveDocument.Tables

        ' 调整表格的宽度为页面宽度

        tbl.PreferredWidthType = wdPreferredWidthPercent

        tbl.PreferredWidth = 100

    Next tbl

    

    ' 执行自动排版

    ActiveDocument.AutoFormat

    

    ' 显示消息框

    MsgBox "自动排版已完成!", vbInformation

End Sub

下面是删除表格所有的表格的宏


以下是删除 Word 文档中所有表格的 WPS 宏示例:


```vba

Sub DeleteAllTables()

    Dim tbl As Object

    

    ' 确保当前文档不是只读模式

    If ActiveDocument.ReadOnly Then

        MsgBox "无法编辑只读文档。", vbExclamation

        Exit Sub

    End If

    

    ' 确认是否删除所有表格

    If MsgBox("是否确定删除文档中的所有表格?", vbQuestion + vbYesNo) = vbNo Then

        Exit Sub

    End If

    

    ' 循环删除所有表格

    For Each tbl In ActiveDocument.Tables

        tbl.Delete

    Next tbl

    

    ' 显示消息框

    MsgBox "已成功删除文档中的所有表格。", vbInformation

End Sub

```


请按照以下步骤使用上述宏:


1. 打开你想要删除表格的 Word 文档。

2. 按下 `Alt+F11` 进入 VBA 编辑器。

3. 创建一个新的宏模块,将以上代码粘贴到模块中。

4. 保存宏并关闭 VBA 编辑器。

5. 使用快捷键 `Alt+F8` 打开宏列表。

6. 选择 `DeleteAllTables` 宏并点击 "运行" 按钮。

7. 宏将删除文档中的所有表格,并显示一个消息框确认删除结果。


请注意,在运行宏之前,请确保已经保存好文档的备份,以防意外删除了不希望删除的内容。



wps 宏 自动排版的评论 (共 条)

分享到微博请遵守国家法律