转到每行的末尾和括号内的粗体文本
Go to end of each line and bold text inside parenthesis
我需要在 MS Word 中自动格式化每行末尾的特定单词。由于 Word 宏的限制,我无法录制宏来完成这项工作,因此我必须在此处 post。我只需要执行以下操作:-
- 检查每一行的开头 (
- 开始选择括号内的文字(包括括号)直到找到句末
- 将文本格式设置为粗体
- 这样做直到文件结束
- 例外:不要格式化已经加粗和加下划线的标题。
我该怎么做?或者请纠正我的代码,因为它什么也没做。
Sub m1()
'
' m1 Macro
'
'
Dim i As Integer
With Selection.Find
For i = 1 To lastRow
.Forward = True
.ClearFormatting
.MatchCase = False
.Wrap = wdFindContinue
.Execute FindText:="("
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Font.Bold = wdToggle
Selection.Font.BoldBi = wdToggle
Next
End With
End Sub
MS Word 不支持线条。以下 VBA 代码将查找以括号结尾的段落,并将它们与内容一起加粗。
Sub Bold_ending_parentheses()
Dim par As Word.Paragraph
Dim str As String
Dim closes As Byte
Dim opens As Long
For Each par In ActiveDocument.Paragraphs
str = StrReverse(par.Range.Text)
closes = InStr(Left(str, 4), ")")
If closes Then
opens = InStr(str, "(")
If opens Then
With par.Range
.Find.Text = StrReverse(Mid(str, closes, opens - closes + 1))
Do
.Find.Execute
If .Find.Found Then .Font.Bold = True
Loop While .Find.Found
End With
End If
End If
Next
End Sub
编辑:
使用查找和替换的示例。使用这个,括号中的所有文本都是粗体(不考虑在 paragraph/line 末尾的要求):
Sub ApplyBoldWithinParentheses()
With ActiveDocument.Content.Find
.ClearFormatting
.Text = "[(]*[)]"
.Replacement.Font.Bold = True
.Format = True
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End Sub
如果您知道文档 body 文本的样式始终是特定的指定样式而不是标题样式,则您可能不需要宏。像这样设置查找和替换:
如果难以从屏幕剪辑中读取通配符代码,则为:[(]*[)]
我终于成功构建了这段代码:
Sub m1()
Selection.HomeKey Unit:=wdStory
With Selection.Find
Do While .Execute(FindText:="(", Forward:=True, MatchWildcards:=False) = True
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Font.Bold = wdToggle
Selection.Font.BoldBi = wdToggle
Selection.EndKey Unit:=wdLine
Loop
End With
End Sub
成功了! :) 感谢支持!
我需要在 MS Word 中自动格式化每行末尾的特定单词。由于 Word 宏的限制,我无法录制宏来完成这项工作,因此我必须在此处 post。我只需要执行以下操作:-
- 检查每一行的开头 (
- 开始选择括号内的文字(包括括号)直到找到句末
- 将文本格式设置为粗体
- 这样做直到文件结束
- 例外:不要格式化已经加粗和加下划线的标题。
我该怎么做?或者请纠正我的代码,因为它什么也没做。
Sub m1()
'
' m1 Macro
'
'
Dim i As Integer
With Selection.Find
For i = 1 To lastRow
.Forward = True
.ClearFormatting
.MatchCase = False
.Wrap = wdFindContinue
.Execute FindText:="("
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Font.Bold = wdToggle
Selection.Font.BoldBi = wdToggle
Next
End With
End Sub
MS Word 不支持线条。以下 VBA 代码将查找以括号结尾的段落,并将它们与内容一起加粗。
Sub Bold_ending_parentheses()
Dim par As Word.Paragraph
Dim str As String
Dim closes As Byte
Dim opens As Long
For Each par In ActiveDocument.Paragraphs
str = StrReverse(par.Range.Text)
closes = InStr(Left(str, 4), ")")
If closes Then
opens = InStr(str, "(")
If opens Then
With par.Range
.Find.Text = StrReverse(Mid(str, closes, opens - closes + 1))
Do
.Find.Execute
If .Find.Found Then .Font.Bold = True
Loop While .Find.Found
End With
End If
End If
Next
End Sub
编辑:
使用查找和替换的示例。使用这个,括号中的所有文本都是粗体(不考虑在 paragraph/line 末尾的要求):
Sub ApplyBoldWithinParentheses()
With ActiveDocument.Content.Find
.ClearFormatting
.Text = "[(]*[)]"
.Replacement.Font.Bold = True
.Format = True
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End Sub
如果您知道文档 body 文本的样式始终是特定的指定样式而不是标题样式,则您可能不需要宏。像这样设置查找和替换:
如果难以从屏幕剪辑中读取通配符代码,则为:[(]*[)]
我终于成功构建了这段代码:
Sub m1()
Selection.HomeKey Unit:=wdStory
With Selection.Find
Do While .Execute(FindText:="(", Forward:=True, MatchWildcards:=False) = True
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Font.Bold = wdToggle
Selection.Font.BoldBi = wdToggle
Selection.EndKey Unit:=wdLine
Loop
End With
End Sub
成功了! :) 感谢支持!