英文:
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'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 "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
- Before
- After
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论