我有一个单词文档,是一个采访的转录。主持人评论以粗体显示,受访者评论不粗体。这是一个长期连续的粗体和非粗体文本。我需要添加回车符,以便主持人和受访者问题之间有一个空白行。我发现下面的代码在特定文本之间插入回车符,但我不知道如何更改它以在粗体和非粗体文本之间插入。任何帮助是极大的赞赏!
Sub Test()
ActiveDocument.Paragraphs(1).Range.Text = "Foo" & Chr(11) & "Bar"
End Sub
这就是我提出的,它使用一个子在粗体文本后插入一个中断,然后调用另一个子来对非粗体文本执行相同操作。我使用常量'vbCrLf'代表Visual Basic回车换行,它等于Chr(13)+ Chr(10),我相信这是在文档中插入换行符时的兼容性的最佳实践,而不是字符(11)。
Sub InsertBreakAfterBold()
'Select entire document
Selection.WholeStory
'Make each .Method belong to Selection.Find for readability
With Selection.Find
'Set search criteria for bold font
.Font.Bold = True
'Find next occurrence
.Execute
'Each time bold text is found add a line break to the end of it then find the next one
Do While .Found
Selection.Text = Selection.Text + vbCrLf
.Execute
Loop
End With
'Repeat process for nonbold text
Call InsertBreakAfterNonbold
End Sub
Sub InsertBreakAfterNonbold()
Selection.WholeStory
With Selection.Find
.Font.Bold = False
.Execute
Do While .Found
Selection.Text = Selection.Text + vbCrLf
.Execute
Loop
End With
End Sub
微软的VBA参考是我最大的资源:https://docs.microsoft.com/en-us/office/vba/api/overview/word