我正在使用 Excel VBA 从电子表格生成 Word 文档。作为最后一步的一部分,我想查找所有双段落并将其替换为单段落。
基本代码:
Dim objWord
Dim objDoc
Dim objSelection
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add
Set objSelection = objWord.Selection
objSelection.TypeText "Test"
objSelection.TypeText (vbCr)
objSelection.TypeText (vbCr)
objWord.Visible = True
查找和替换基本文本作品:
With objWord.ActiveDocument
Set myRange = .Content
With myRange.Find
.Execute FindText:="test", ReplaceWith:="apple", Replace:=2
End With
End With
查找和替换段落不起作用:
With objWord.ActiveDocument
Set myRange = .Content
With myRange.Find
.Execute FindText:="^^p", ReplaceWith:="^p", Replace:=2
End With
End With
最佳答案
改变
.执行 FindText:="^^p", ReplaceWith:="^p", Replace:=2
到
.执行 FindText:="^p^p", ReplaceWith:="^p", Replace:=2
关于VBA 查找和替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50025665/