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

时间:2014-05-01 23:12:16

标签: 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

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

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

我查看了the documentation for ParagraphFormat并搜索了相关的财产,但发现没有任何效果。如何以编程方式更改"不要在相同样式的段落之间添加空格"?

4 个答案:

答案 0 :(得分:8)

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

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

答案 1 :(得分:6)

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

Selection.Style.NoSpaceBetweenParagraphsOfSameStyle = False

或回退到内置对话框:

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

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

如果有人偶然发现此问题并正在寻找C#示例,那么这对我有用。希望它可以帮助其他人。

        string signaturesPath = Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + @"\Microsoft\Signatures\";
        Directory.CreateDirectory(signaturesPath + "Test");

        Word.Application oWord = new Word.Application();
        //oWord.Visible = true;
        Word.Document oDoc = oWord.Documents.Add();

        //Insert a paragraph at the beginning of the document.
        Word.Paragraph paragraph1 = oDoc.Content.Paragraphs.Add();

        object oStyleName1 = Word.WdBuiltinStyle.wdStyleNormal;
        //NoSpaceBetweenParagraphsOfSameStyle set on style then assign to doc 
        oWord.ActiveDocument.Styles[oStyleName1].NoSpaceBetweenParagraphsOfSameStyle = true;
       
        //Setting style on paragraph here
        paragraph1.Format.set_Style(oStyleName1);
        paragraph1.Range.Font.Bold = 1;
        paragraph1.Range.InsertAfter("Testing 123");

        //Save as htm
        object htmlFormat = (int)Word.WdSaveFormat.wdFormatFilteredHTML;
        oDoc.SaveAs2(signaturesPath + @"\test.htm", htmlFormat);

        oWord.Quit();