VBA - WPS 修改段落、表格、標題的宏

Himmelbleu發表於2024-03-08
file:[修改表格樣式,邊框和內邊框,以及表格內的段落樣式,WPS]
Sub ChangeTableStyle()
    Dim tbl As Table
    Dim para As Paragraph
    For Each tbl In ActiveDocument.Tables
        With tbl
            ' 設定表格邊框樣式
            .Borders.Enable = True
            .Borders.InsideLineStyle = wdLineStyleSingle
            .Borders.OutsideLineStyle = wdLineStyleSingle
            .Borders.OutsideLineWidth = wdLineWidth150pt ' 適中粗度
            .Borders.InsideLineWidth = wdLineWidth0100pt ' 適中粗度
            
            ' 設定表格居中
            .Rows.Alignment = wdAlignRowCenter
            .Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
            For Each Cell In tbl.Range.Cells
                Cell.Range.ParagraphFormat.Alignment = wdAlignParagraphCenter ' 水平居中
                Cell.Range.Cells.VerticalAlignment = wdCellAlignVerticalCenter ' 垂直居中
             Next Cell
            
            ' 設定表格內文字樣式
            .Range.Font.Size = 16
            .Range.Font.Bold = False
            .Range.Font.Italic = False
            .Range.Font.Name = "仿宋_GB2312"
            .Range.ParagraphFormat.LineSpacing = 12 ' 單倍行距
            .Range.ParagraphFormat.FirstLineIndent = 0
            .Range.ParagraphFormat.SpaceBefore = 0
            .Range.ParagraphFormat.SpaceAfter = 0
        End With
    Next tbl

End Sub
file:[修改段落樣式,除表格內的段落樣式,WPS]
Sub ChangeParagraphStyle()
    Dim para As Paragraph
    
    ' 遍歷所有段落
    For Each para In ActiveDocument.Paragraphs
        With para
            ' 判斷是否為表格段落
            If .Range.Tables.Count = 0 Then
                ' 修改非表格段落的樣式
                ' 修改段落樣式,比如將字型顏色設定為紅色

                .Range.Font.Color = wdColorBlack
                ' 設定其他樣式,比如字型大小、加粗、斜體等
                .Range.Font.Size = 16
                .Range.Font.Bold = False
                .Range.Font.Italic = False
                .Range.Font.Name = "仿宋_GB2312"
                .Format.CharacterUnitFirstLineIndent = 2  ' 此處設為2個字元
                .Format.SpaceBefore = 0
                .Format.SpaceAfter = 0
                .Format.LineSpacingRule = wdLineSpaceExactly
                .Format.LineSpacing = 28
            End If
        End With
    Next para
End Sub
file:[修改標題一的樣式,WPS]
Sub ChangeTitle1Style()
    Dim para As Paragraph

    ' 遍歷所有段落
    For Each para In ActiveDocument.Paragraphs
        With para
            ' 判斷是否為表格段落
            If .Style = "標題 1" Then
                .Range.Font.Color = wdColorBlack
                ' 設定其他樣式,比如字型大小、加粗、斜體等
                .Range.Font.Size = 16
                .Range.Font.Bold = True
                .Range.Font.Italic = False
                .Range.Font.Name = "仿宋_GB2312"
                .Format.CharacterUnitFirstLineIndent = 2  ' 此處設為2個字元
                .Format.SpaceBefore = 0
                .Format.SpaceAfter = 0
                .Format.LineSpacingRule = wdLineSpaceExactly
                .Format.LineSpacing = 28
             End If
        End With
    Next para
End Sub

相關文章