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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23418243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 02:57:47  来源:igfitidea点击:

How can I programmatically change "Don't add space between paragraphs of the same style"?

vbams-wordword-vba

提问by Homer

I'm trying to programmatically change "Don't add space between paragraphs of the same style." To approach the problem, I recorded a macro during which I opened the Paragraph dialog box (Page Layout > Paragraph), checked the checkbox (don't add space) and a macro during which I unchecked the checkbox (add space). Neither affects "Don't add space between paragraphs of the same style" . . . and they have identical code:

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

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

The code produced by the macro recorder is long, so I reduced it to a minimal version that I've verified also fails to affect "Don't add space between paragraphs of the same style":

宏记录器生成的代码很长,所以我将其缩减为我已经验证过的最小版本也无法影响“不要在相同样式的段落之间添加空格”:

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

I looked at the documentation for ParagraphFormatand searched for a relevant property but found nothing that works. How can I programmatically change "Don't add space between paragraphs of the same style"?

我查看了 ParagraphFormat 的文档并搜索了相关属性,但没有找到任何有用的内容。如何以编程方式更改“不要在相同样式的段落之间添加空格”?

回答by Kazimierz Jawor

This property is connected with Style, not with Paragraph (which suggests window title where you set this property). This is code which you look for:

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

ActiveDocument.Styles("Normal").NoSpaceBetweenParagraphsOfSameStyle = False
ActiveDocument.Styles("Normal").NoSpaceBetweenParagraphsOfSameStyle = True

回答by Homer

The macro recorder recognizes changing spacing but not "Don't add space between paragraphs of the same style" (Page Layout > Paragraph). To change paragraph formatting without modifying a built-in style (or creating a new style), I can use Selection.Style:

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

Selection.Style.NoSpaceBetweenParagraphsOfSameStyle = False

or fall back to the built-in dialog:

或者回到内置对话框:

With Dialogs(wdDialogFormatParagraph)
    .Before = 12
    .After = 12
    .NoSpaceBetweenParagraphsOfSameStyle = False
    .Execute
End With

回答by Kunal Bindal

winword.ActiveDocument.Styles["Normal"].NoSpaceBetweenParagraphsOfSameStyle = true;
winword.ActiveDocument.Styles["List Paragraph"].NoSpaceBetweenParagraphsOfSameStyle = false;

On word doc press Alt+Ctl+Shift+S to check all the styles

在 word doc 上按 Alt+Ctl+Shift+S 检查所有样式