首页 > 解决方案 > 如何在VBA中的第一行之前添加间距?

问题描述

我正在尝试在自动生成的 word 文档中的一个段落之前添加间距。我只需要该特定段落的第一行之前的间距。问题出在第一个空行之后的下一行。

With Wapp
    With .Selection
        .TypeParagraph
        'Add line spacing
        .ParagraphFormat.SpaceBefore = 0
        .ParagraphFormat.SpaceAfter = 0
        .TypeText St1
        .TypeParagraph

        .ParagraphFormat.SpaceBefore = 36
        .BoldRun
        'Centers text
        .ParagraphFormat.Alignment = 1
        .TypeText St2
        .BoldRun
        .ParagraphFormat.SpaceBefore = 0

        .TypeText VBA.vbNewLine
        .TypeText St3
        .TypeText VBA.vbNewLine
        .ParagraphFormat.Alignment = 1
    End With
End With

标签: vbams-wordspacingparagraph

解决方案


交换了两行代码

.TypeText VBA.vbNewLine

.ParagraphFormat.SpaceBefore = 0

似乎将 .ParagraphFormat 放在 .TypeParagraph 或 VBA.vbNewLine 之后很重要,然后它开始按我的意愿工作。

With Wapp
    With .Selection
        .TypeParagraph
        'Add line spacing
        .ParagraphFormat.SpaceBefore = 0
        .ParagraphFormat.SpaceAfter = 0
        .TypeText St1
        .TypeParagraph
        'Starts spacing
        .ParagraphFormat.SpaceBefore = 6
        .BoldRun
        'Centers text
        .ParagraphFormat.Alignment = 1
        .TypeText St2
        .BoldRun
        .TypeText VBA.vbNewLine
        'Ends spacing
        .ParagraphFormat.SpaceBefore = 0
        .TypeText St3
        .TypeText VBA.vbNewLine
        .ParagraphFormat.Alignment = 1
    End With
End With

推荐阅读