vbams-wordparagraph

New paragraph alignment style


As you know, Word offers several paragraph alignment options. When aligning a paragraph to both sides, (CTRL+J), all lines (except the last) are attached to the left and right margins. The last line remains aligned to one side, (usually, according to the direction of the writing and the language). I want to build a macro that will enable a new paragraph alignment style, like this: All lines will be aligned to both sides, including the last line, if it has more than one word. To do this, I want to build code that will increase the font size of " " (the space) by half a point, along the last line only, as long as the last line does not wrap to a new line. Thanks for the ideas.

The model I was thinking about (and stuck with...) refers to the following conditions: The cursor is already on the last line. The code will count the number of spaces to increase, will increase them by half a point each time. It will count the number of spaces in the current row - if they are less than the previous count - a one-time reduction will be performed. By the way, I tried again and again to use https://chat.openai.com/ - and I didn't succeed.


Solution

  • Is the CTRL+Shift+J is just what you were thinking about?

    enter image description here

    20230616 updated

    Sub New_paragraph_alignment_style_Aligned_to_both_sides_and_the_last_to_the_center()
        Dim p As paragraph, ur As UndoRecord, lns As Lines, ln As Line, recs As Rectangles, rec As Rectangle
        Set ur = Word.Application.UndoRecord
        ur.StartCustomRecord "New_paragraph_alignment_style_Aligned_to_both_sides_and_the_last_to_the_center"
        For Each p In Selection.Paragraphs
            'Set p = Selection.Paragraphs(1)
            With p.Range
                .ParagraphFormat.Alignment = wdAlignParagraphDistribute
                Set recs = .Document.ActiveWindow.Panes(1).Pages(.Information(wdActiveEndPageNumber)).Rectangles
                For Each rec In recs
                    If rec.Range = p.Range Then
                        Exit For 'Stop
                    End If
                Next rec
                Set lns = rec.Lines
                If lns.Count > 1 Then
                    Set ln = lns(lns.Count)
                    .ParagraphFormat.TabStops.Add Position:=CentimetersToPoints(0.75), Alignment:=wdAlignTabCenter
                    .Document.Range(lns(lns.Count).Range.Characters(1).Start, lns(lns.Count).Range.Characters(1).Start).InsertAfter vbTab
                    '.Document.Range(lns(lns.Count).Range.Characters(1).Start, lns(lns.Count).Range.Characters(1).Start).InsertAlignmentTab 1, 0
                End If
            End With
        Next p
        ur.EndCustomRecord
    End Sub
    

    Original text:

    enter image description here

    After running the code above

    enter image description here

    20230618 updated Fix the bug :

    When a paragraph is across the page, there is an error. Because the line above: Set recs = .Document.ActiveWindow.Panes(1).Pages(.Information(wdActiveEndPageNumber)).Rectangles

    Sub New_paragraph_alignment_style_Aligned_to_both_sides_and_the_last_to_the_center()
        Dim p As paragraph, ur As UndoRecord, lns As Lines, ln As Line, recs As Rectangles, rec As Rectangle
        Set ur = Word.Application.UndoRecord
        ur.StartCustomRecord "New_paragraph_alignment_style_Aligned_to_both_sides_and_the_last_to_the_center"
        For Each p In Selection.Paragraphs
            'Set p = Selection.Paragraphs(1)
            With p.Range
                .ParagraphFormat.Alignment = wdAlignParagraphDistribute
                Set recs = .Document.ActiveWindow.Panes(1).Pages(.Information(wdActiveEndPageNumber)).Rectangles
                For Each rec In recs
                    If rec.Range = p.Range Then
                        Exit For
                    End If
                Next rec
                
                Rem When a paragraph is across the page, there is an error. Because the line above: Set recs = .Document.ActiveWindow.Panes(1).Pages(.Information(wdActiveEndPageNumber)).Rectangles
                If rec Is Nothing Then
                    Set lns = recs(1).Lines
                Else
                    Set lns = rec.Lines
                End If
                If lns.Count > 1 Or rec Is Nothing Then
                    Set ln = lns(lns.Count)
                    .ParagraphFormat.TabStops.Add Position:=CentimetersToPoints(0.75), Alignment:=wdAlignTabCenter
                    Rem However, please NOTE: Insert a VBTab character will inevitably affect the results of finding content text
                    .Document.Range(lns(lns.Count).Range.Characters(1).Start, lns(lns.Count).Range.Characters(1).Start).InsertAfter vbTab
                    '.Document.Range(lns(lns.Count).Range.Characters(1).Start, lns(lns.Count).Range.Characters(1).Start).InsertAlignmentTab 1, 0
                End If
    
            End With
        Next p
        ur.EndCustomRecord
    End Sub