VB replace text in a word document header and footer

Find information, replace only the success of the replacement text page content

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

Plus write C # test successfully used the 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:="visit_name", ReplaceWith:=Sheet1.Cells(1, i).Value, Replace:=2
        .ActiveWindow.ActivePane.View.SeekView = 0
        .documents.Close
    End With
  wordapp.Quit
  Set wordapp = Nothing
End Sub  

 

Guess you like

Origin www.cnblogs.com/rn-05181226-rw/p/11721252.html