如何以编程方式更改"不要在相同样式的段落之间添加空格"?

Hom*_*mer 5 vba ms-word word-vba

我正在尝试以编程方式更改"不要在相同样式的段落之间添加空格".为了解决这个问题,我录制了一个宏,在此期间我打开了段落对话框(页面布局>段落),选中了复选框(不添加空格)和宏,在此期间我取消选中复选框(添加空格).两者都不影响"不要在相同样式的段落之间添加空格"...他们有相同的代码:

Sub AddSpaceBetweenParagraphsOfSameStyle()
'
' AddSpaceBetweenParagraphsOfSameStyle Macro
' Add space between paragraphs of the same style.
'
    With Selection.ParagraphFormat
        .LeftIndent = InchesToPoints(0.5)
        .RightIndent = InchesToPoints(0)
        .SpaceBefore = 12
        .SpaceBeforeAuto = False
        .SpaceAfter = 12
        .SpaceAfterAuto = False
        .LineSpacingRule = wdLineSpaceMultiple
        .LineSpacing = LinesToPoints(1)
        .Alignment = wdAlignParagraphLeft
        .WidowControl = True
        .KeepWithNext = False
        .KeepTogether = False
        .PageBreakBefore = False
        .NoLineNumber = False
        .Hyphenation = True
        .FirstLineIndent = InchesToPoints(-0.25)
        .OutlineLevel = wdOutlineLevelBodyText
        .CharacterUnitLeftIndent = 0
        .CharacterUnitRightIndent = 0
        .CharacterUnitFirstLineIndent = 0
        .LineUnitBefore = 0
        .LineUnitAfter = 0
        .MirrorIndents = False
        .TextboxTightWrap = wdTightNone
    End With
End Sub

Sub RemoveSpaceBetweenParagraphsOfSameStyle()
'
' RemoveSpaceBetweenParagraphsOfSameStyle Macro
' Remove space between paragraphs of the same style.
'
    With Selection.ParagraphFormat
        .LeftIndent = InchesToPoints(0.5)
        .RightIndent = InchesToPoints(0)
        .SpaceBefore = 12
        .SpaceBeforeAuto = False
        .SpaceAfter = 12
        .SpaceAfterAuto = False
        .LineSpacingRule = wdLineSpaceMultiple
        .LineSpacing = LinesToPoints(1)
        .Alignment = wdAlignParagraphLeft
        .WidowControl = True
        .KeepWithNext = False
        .KeepTogether = False
        .PageBreakBefore = False
        .NoLineNumber = False
        .Hyphenation = True
        .FirstLineIndent = InchesToPoints(-0.25)
        .OutlineLevel = wdOutlineLevelBodyText
        .CharacterUnitLeftIndent = 0
        .CharacterUnitRightIndent = 0
        .CharacterUnitFirstLineIndent = 0
        .LineUnitBefore = 0
        .LineUnitAfter = 0
        .MirrorIndents = False
        .TextboxTightWrap = wdTightNone
    End With
End Sub
Run Code Online (Sandbox Code Playgroud)

由宏录制器生成的代码很长,因此我将其缩减为最小版本,我已经验证也不会影响"不要在相同样式的段落之间添加空格":

Sub AddSpaceBetweenParagraphsOfSameStyle()
'
' AddSpaceBetweenParagraphsOfSameStyle Macro
' Add space between paragraphs of the same style.
'
End Sub

Sub RemoveSpaceBetweenParagraphsOfSameStyle()
'
' RemoveSpaceBetweenParagraphsOfSameStyle Macro
' Remove space between paragraphs of the same style.
'
End Sub
Run Code Online (Sandbox Code Playgroud)

我查看了ParagraphFormat的文档并搜索了相关的属性但发现没有任何效果.如何以编程方式更改"不要在相同样式的段落之间添加空格"?

Kaz*_*wor 9

此属性与Style相关联,而不是与Paragraph(建议您设置此属性的窗口标题)相关联.这是您寻找的代码:

ActiveDocument.Styles("Normal").NoSpaceBetweenParagraphsOfSameStyle = False
ActiveDocument.Styles("Normal").NoSpaceBetweenParagraphsOfSameStyle = True
Run Code Online (Sandbox Code Playgroud)


Hom*_*mer 7

宏录制器识别更改间距,但不识别"不要在相同样式的段落之间添加空格"(页面布局>段落).要在不修改内置样式(或创建新样式)的情况下更改段落格式,我可以使用Selection.Style:

Selection.Style.NoSpaceBetweenParagraphsOfSameStyle = False
Run Code Online (Sandbox Code Playgroud)

或者回到内置对话框:

With Dialogs(wdDialogFormatParagraph)
    .Before = 12
    .After = 12
    .NoSpaceBetweenParagraphsOfSameStyle = False
    .Execute
End With
Run Code Online (Sandbox Code Playgroud)