1个excel表格调用word文档,打开,替换word里面的文本,把B2替换成C2,B3替换成C3...
发布网友
发布时间:2022-05-11 02:36
我来回答
共1个回答
热心网友
时间:2024-03-01 04:49
'首先要在vba中引用Microsoft Word 1x.0 Object Library
Dim Wdapp As Word.Application
Dim WdDocument As Word.Document
Dim UserFile As String
Private Sub CommandButton1_Click()
Dim Mypath As String
Set Wdapp = New Word.Application
Mypath = ActiveWorkbook.Path & "\合同模板.doc"
Set WdDocument = Wdapp.Documents.Open(Mypath)
With WdDocument.Range.Find
.ClearFormatting
.Text = "B2"
.Replacement.ClearFormatting
.Replacement.Text = "C2"
.Execute Replace:=wdReplaceAll, Forward:=True, _
Wrap:=wdFindContinue
End With
With WdDocument.Range.Find
.ClearFormatting
.Text = "B3"
.Replacement.ClearFormatting
.Replacement.Text = "C3"
.Execute Replace:=wdReplaceAll, Forward:=True, _
Wrap:=wdFindContinue
End With
WdDocument.Save
WdDocument.Close
End Sub
追问B2替换成C2,B3替换成C3,B4替换成C4,B5替换成C5,直到有空值才停止。还有2个文件在一个文件夹,自动识别路径。批量替换文本.xlsm 合同模板.doc