Programmatically change "Don't add space between paragraphs of the same style"

StackOverflow https://stackoverflow.com/questions/23418243

  •  13-07-2023
  •  | 
  •  

Pregunta

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 ParagraphFormat and 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"?

¿Fue útil?

Solución

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:

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

Otros consejos

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.NoSpaceBetweenParagraphsOfSameStyle = False

or fall back to the built-in dialog:

With Dialogs(wdDialogFormatParagraph)
    .Before = 12
    .After = 12
    .NoSpaceBetweenParagraphsOfSameStyle = False
    .Execute
End With
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

If anyone stumbles on this and is looking for a C# example, this is what worked for me. Hope it helps someone else.

        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();
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top