在MS Word中如何在顶部文本和底部日期之间添加一条水平线。

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

how to add a horizontal line between top text and bottom date in MS Word

问题

如何在MS Word中像下面这样,在顶部文本和底部日期之间添加一条水平线。实际上,我也无法在下面的示例中添加水平线。

2048
3/10/2023

英文:

how to add a horizontal line between top text and bottom date in MS word like below. actually, I could not add horizontal line also in the below sample.

     2048
  3/10/2023

答案1

得分: 0

以下是您要翻译的部分:

"Is this a question about code?

Is this what you want below? Thus press -, =, _, * three times, and then press Enter key you'll get it. If this is what just you want, then next time remember to ask on the Super User site, not here. Thanks.

use Character Symbols to achieve

Sub CharacterSymbols_how_to_add_a_horizontal_line_between_top_text_and_bottom_date_in_MS_Word()
    
    Dim line As String, charCount As Byte, i As Byte, a As Range, ur As UndoRecord
    
    If Selection.Type = wdSelectionIP Then
        MsgBox "Plz Select the range you want to...", vbExclamation
        Exit Sub
    Else
        If Selection.Paragraphs.Count <> 2 Then
            MsgBox "Only 2 paragraphs can be selected, Plz", vbCritical
            Exit Sub
        End If
    End If
    Set ur = Word.Application.UndoRecord
    ur.StartCustomRecord "CharacterSymbols_how_to_add_a_horizontal_line_between_top_text_and_bottom_date_in_MS_Word"
    
    Word.Application.ScreenUpdating = False
    
    With Selection
        charCount = VBA.IIf(.Paragraphs(1).Range.Characters.Count > .Paragraphs(2).Range.Characters.Count, .Paragraphs(1).Range.Characters.Count, .Paragraphs(2).Range.Characters.Count)
        
        Rem The length of the line: This applies to narrow-form characters such as English letters and numbers, but not to wide-form characters such as Chinese characters. No need to divide by 2 if the character is in wide one.
        'charCount = charCount / 2 - 1
        charCount = VBA.IIf(FullOrHalf(.Paragraphs(1).Range.Characters(1)) = 1, charCount / 2 - 1, charCount) ' temporarily according to the first character
        For i = 1 To charCount
            line = line + "─"
        Next
        Set a = .Paragraphs(2).Range.Characters(1)
        a.InsertBefore Chr(13)
        .SetRange a.Start, a.Start
        .TypeText line
        With .Paragraphs(1).Range.ParagraphFormat
            .LineSpacingRule = wdLineSpaceExactly
            .LineSpacing = VBA.Int(a.Font.Size / 2)
            .BaseLineAlignment = wdBaselineAlignCenter
        End With
        While .Paragraphs(1).Next.Range.Information(wdFirstCharacterLineNumber) - .Paragraphs(1).Range.Information(wdFirstCharacterLineNumber) > 1
            .Paragraphs(1).Range.Characters(1).Text = VBA.vbNullString
        Wend
    End With
    
    ur.EndCustomRecord
    Word.Application.ScreenUpdating = True
End Sub

Rem https://zhuanlan.zhihu.com/p/600306305
Public Function FullOrHalf(ByVal str As String) As Integer
    Dim strLocal As String
    Debug.Assert Len(str) = 1
    If Len(str) <> 1 Then
        FullOrHalf = -1
        Exit Function
    End If
    strLocal = StrConv(str, vbFromUnicode)
    If Len(str) * 2 = LenB(strLocal) Then
        FullOrHalf = 2 ' wide
    ElseIf Len(str) = LenB(strLocal) Then
        FullOrHalf = 1 ' narrow
    Else
        FullOrHalf = 0 ' error
    End If
End Function
```"

<details>
<summary>英文:</summary>

Is this a question about code? 

Is this what you want below? Thus press -, =, _, * three times, and then press Enter key you&#39;ll get it. If this is what just you want, then next time remember to ask on the [Super User](https://superuser.com/questions/tagged/microsoft-word) site, not here. Thanks.

[![enter image description here][1]][1]


## use Character Symbols to achieve

```vba
Sub CharacterSymbols_how_to_add_a_horizontal_line_between_top_text_and_bottom_date_in_MS_Word()
    
    Dim line As String, charCount As Byte, i As Byte, a As Range, ur As UndoRecord
    
    If Selection.Type = wdSelectionIP Then
        MsgBox &quot;Plz Select the range you want to...&quot;, vbExclamation
        Exit Sub
    Else
        If Selection.Paragraphs.Count &lt;&gt; 2 Then
            MsgBox &quot;Only 2 paragraphs can be selected, Plz&quot;, vbCritical
            Exit Sub
        End If
    End If
    Set ur = Word.Application.UndoRecord
    ur.StartCustomRecord &quot;CharacterSymbols_how_to_add_a_horizontal_line_between_top_text_and_bottom_date_in_MS_Word&quot;
    
    Word.Application.ScreenUpdating = False
    
    With Selection
        charCount = VBA.IIf(.Paragraphs(1).Range.Characters.Count &gt; .Paragraphs(2).Range.Characters.Count, .Paragraphs(1).Range.Characters.Count, .Paragraphs(2).Range.Characters.Count)
        
        Rem The length of the line: This applies to narrow-form characters such as English letters and numbers, but not to wide-form characters such as Chinese characters. No need to divide by 2 if the character is in wide one.
        &#39;charCount = charCount / 2 - 1
        charCount = VBA.IIf(FullOrHalf(.Paragraphs(1).Range.Characters(1)) = 1, charCount / 2 - 1, charCount) &#39; temporarily according to the first character
        For i = 1 To charCount
            line = line + &quot;─&quot;
        Next
        Set a = .Paragraphs(2).Range.Characters(1)
        a.InsertBefore Chr(13)
        .SetRange a.Start, a.Start
        .TypeText line
        With .Paragraphs(1).Range.ParagraphFormat
            .LineSpacingRule = wdLineSpaceExactly
            .LineSpacing = VBA.Int(a.Font.Size / 2)
            .BaseLineAlignment = wdBaselineAlignCenter
        End With
        While .Paragraphs(1).Next.Range.Information(wdFirstCharacterLineNumber) - .Paragraphs(1).Range.Information(wdFirstCharacterLineNumber) &gt; 1
            .Paragraphs(1).Range.Characters(1).Text = VBA.vbNullString
        Wend
    End With
    
    ur.EndCustomRecord
    Word.Application.ScreenUpdating = True
End Sub

Rem https://zhuanlan.zhihu.com/p/600306305
Public Function FullOrHalf(ByVal str As String) As Integer
    Dim strLocal As String
    Debug.Assert Len(str) = 1
    If Len(str) &lt;&gt; 1 Then
        FullOrHalf = -1
        Exit Function
    End If
    strLocal = StrConv(str, vbFromUnicode)
    If Len(str) * 2 = LenB(strLocal) Then
        FullOrHalf = 2 &#39; wide
    ElseIf Len(str) = LenB(strLocal) Then
        FullOrHalf = 1 &#39; narrow
    Else
        FullOrHalf = 0 &#39; error
    End If
End Function
  • Before

在MS Word中如何在顶部文本和底部日期之间添加一条水平线。

  • After

在MS Word中如何在顶部文本和底部日期之间添加一条水平线。

huangapple
  • 本文由 发表于 2023年7月6日 16:46:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76627041.html
匿名

发表评论

匿名网友

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

确定