Word 中的宏为文档中长度小于 X 个字符的每个段落加下划线
Macro in Word to Underline each paragraph in document less than X characters long
我有好几页的word文档。文档中有很多行是短标题,然后是回车return,然后是描述性的段落。不是头版新闻。
例如
Condition Subsequent
A condition subsequent is often used in a legal context as a marker bringing an end to one's legal rights or duties. A condition subsequent may be either an event or a state of affairs that must either (1) occur or (2) fail to continue to occur.
这种事情一直持续到长文档的最后,有 100 多个标题 - 需要加下划线!
我已经使用此代码查找所有 行 少于 100 个字符的下划线,这是有效的,但如果段落的最后一行少于 100 个字符,也有下划线,这是我不想要的:
Sub Underline_Header()
Dim numOfLines As Integer
numOfLines = ActiveDocument.BuiltInDocumentProperties("NUMBER OF LINES")
Selection.HomeKey Unit:=wdStory
For x1 = 1 To numOfLines
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
char_count = Len(Selection.Range.Text)
If char_count < 100 Then
Selection.Font.Underline = True
End If
Selection.MoveDown Unit:=wdLine, Count:=1
Next x1
End Sub
但是当我尝试这个(如下)查找 段落 并计算 段落 中的字符数时,Word 会抛出一个下面突出显示的两行错误:
Sub Underline_Header()
Dim numOfParagraphs As Integer
numOfParagraphs = ActiveDocument.BuiltInDocumentProperties("NUMBER OF PARAGRAPHS")
Selection.HomeKey Unit:=wdStory
For x1 = 1 To numOfParagraphs
*>>Selection.HomeKey Unit:=wdParagraph
>>Selection.EndKey Unit:=wdParagraph, Extend:=wdExtend*
char_count = Len(Selection.Range.Text)
If char_count < 100 Then
Selection.Font.Underline = True
End If
Selection.MoveDown Unit:=wdParagraph, Count:=1
Next x1
End Sub
找到编辑解决方案
为了子孙后代...
此代码查找所有少于 100 个字符的段落(假定为标题)并在它们下划线:
Sub Underline_Header()
Dim numOfParagraphs As Integer
numOfParagraphs = ActiveDocument.BuiltInDocumentProperties("NUMBER OF PARAGRAPHS")
Selection.HomeKey Unit:=wdStory
For x1 = 1 To numOfParagraphs
Selection.Paragraphs(1).Range.Select
char_count = Len(Selection.Paragraphs(1).Range)
If char_count < 100 Then
Selection.Font.Underline = True
End If
Selection.MoveDown Unit:=wdParagraph, Count:=1
Next x1
End Sub
fwiw
我有好几页的word文档。文档中有很多行是短标题,然后是回车return,然后是描述性的段落。不是头版新闻。
例如
Condition Subsequent
A condition subsequent is often used in a legal context as a marker bringing an end to one's legal rights or duties. A condition subsequent may be either an event or a state of affairs that must either (1) occur or (2) fail to continue to occur.
这种事情一直持续到长文档的最后,有 100 多个标题 - 需要加下划线!
我已经使用此代码查找所有 行 少于 100 个字符的下划线,这是有效的,但如果段落的最后一行少于 100 个字符,也有下划线,这是我不想要的:
Sub Underline_Header()
Dim numOfLines As Integer
numOfLines = ActiveDocument.BuiltInDocumentProperties("NUMBER OF LINES")
Selection.HomeKey Unit:=wdStory
For x1 = 1 To numOfLines
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
char_count = Len(Selection.Range.Text)
If char_count < 100 Then
Selection.Font.Underline = True
End If
Selection.MoveDown Unit:=wdLine, Count:=1
Next x1
End Sub
但是当我尝试这个(如下)查找 段落 并计算 段落 中的字符数时,Word 会抛出一个下面突出显示的两行错误:
Sub Underline_Header()
Dim numOfParagraphs As Integer
numOfParagraphs = ActiveDocument.BuiltInDocumentProperties("NUMBER OF PARAGRAPHS")
Selection.HomeKey Unit:=wdStory
For x1 = 1 To numOfParagraphs
*>>Selection.HomeKey Unit:=wdParagraph
>>Selection.EndKey Unit:=wdParagraph, Extend:=wdExtend*
char_count = Len(Selection.Range.Text)
If char_count < 100 Then
Selection.Font.Underline = True
End If
Selection.MoveDown Unit:=wdParagraph, Count:=1
Next x1
End Sub
找到编辑解决方案
为了子孙后代...
此代码查找所有少于 100 个字符的段落(假定为标题)并在它们下划线:
Sub Underline_Header()
Dim numOfParagraphs As Integer
numOfParagraphs = ActiveDocument.BuiltInDocumentProperties("NUMBER OF PARAGRAPHS")
Selection.HomeKey Unit:=wdStory
For x1 = 1 To numOfParagraphs
Selection.Paragraphs(1).Range.Select
char_count = Len(Selection.Paragraphs(1).Range)
If char_count < 100 Then
Selection.Font.Underline = True
End If
Selection.MoveDown Unit:=wdParagraph, Count:=1
Next x1
End Sub
fwiw