查找资料,替换成功的只有替换页面内容的文字
Sub docs()Dim wordapp Set wordapp = CreateObject("Word.Application") With wordapp .documents.Open (ThisWorkbook.Path & "test.docx") .Visible = True .activedocument.content.Find.Execute FindText:=text1, ReplaceWith:=text2, Replace:=2 .documents.Close End With wordapp.Quit Set wordapp = Nothing End Sub
加上写C#用过DOM API 测试成功
Sub docs() Dim wordapp Set wordapp = CreateObject("Word.Application") With wordapp .documents.Open (ThisWorkbook.Path & "test.docx") .Visible = True .ActiveWindow.ActivePane.View.SeekView = 9 .Selection.Find.Execute FindText:=text1, ReplaceWith:=text2, Replace:=2 .ActiveWindow.ActivePane.View.SeekView = 0 .documents.Close End With wordapp.Quit Set wordapp = Nothing End Sub