参考代码
Dim app As New MSWord.Application
Dim app2 As New MSWord.Application
try
Dim fileName = ProjectPath & "页眉模板.doc"
Dim doc As Object = app.Documents.Open(fileName)
app.ActiveWindow.Selection.WholeStory
Dim r = Tables("表A").Current
If app.ActiveWindow.Selection.Find.Execute("[名称]") Then
app.ActiveWindow.Selection.TypeText(Text:=r("名称"))
End If
If app.ActiveWindow.Selection.Find.Execute("[发布日期]") Then
app.ActiveWindow.Selection.TypeText(Text:=r("发布日期"))
End If
If app.ActiveWindow.Selection.Find.Execute("[实施日期]") Then
app.ActiveWindow.Selection.TypeText(Text:=r("实施日期"))
End If
app.ActiveWindow.Selection.WholeStory
If app.ActiveWindow.Selection.Find.Execute("[版本]") Then
app.ActiveWindow.Selection.TypeText(Text:=r("版本"))
End If
doc.saved = True
Dim count = doc.Characters.Count
Dim rng As MSWord.Range = Doc.Range(Start:=0, End:=count)
rng.Select() '全选
rng.Copy() '拷贝
Dim doc2 As Object = app2.Documents.Open(ProjectPath & "未加页眉.doc")
app2.ActiveWindow.ActivePane.View.SeekView = MSWord.WdSeekView.wdSeekCurrentPageHeader
app2.ActiveWindow.Selection.WholeStory
app2.ActiveWindow.Selection.paste
app2.ActiveWindow.ActivePane.View.SeekView = MSWord.WdSeekView.wdSeekMainDocument
app2.Visible = True
app.quit
catch ex As exception
msgbox(ex.message)
app.Quit
app2.quit
finally
'app.Quit
'app2.quit
End try