新段落对齐样式

huangapple go评论63阅读模式
英文:

New paragraph alignment style

问题

正如你所知,Word提供了几种段落对齐选项。
当将段落两边对齐(CTRL+J)时,所有行(除了最后一行)都与左右边距对齐。最后一行保持与一侧对齐(通常是根据书写方向和语言的方向)。
我想要创建一个宏,可以启用一种新的段落对齐样式,如下所示:
所有行都将两边对齐,包括最后一行,如果最后一行有多于一个单词。
为此,我想要编写一段代码,只增加“ ”(空格)的字体大小半个点,仅在最后一行,只要最后一行不换行。
感谢您的建议。

我考虑的模型(并陷入困境...)涉及以下条件:
光标已经在最后一行。
代码将计算要增加的空格数量,
每次增加半个点。
它将计算当前行中的空格数量 - 如果它们少于先前的计数 - 将执行一次减少。
顺便说一句,我一次又一次地尝试使用https://chat.openai.com/ - 但我没有成功。

英文:

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.

答案1

得分: 1

CTRL+Shift+J 是你想的吗?

英文:

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

新段落对齐样式

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:

新段落对齐样式

After running the code above

新段落对齐样式

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

huangapple
  • 本文由 发表于 2023年5月28日 17:55:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76350891.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定