|
Sub 合同草拟()
Dim wordApp As Object
Set wordApp = CreateObject("Word.Application") '---这里创建了一个空的Word.Application对象
wordApp.Visible = True
Dim wordDoc As Object
Set wordDoc = wordApp.Documents
Dim newDoc As Object
wordDoc.Open "C:\Users\10127\Desktop\VBA-合同草拟APP开发\P-模板-8.4 技术服务合同(2023版).docx"
wordApp.Selection.WholeStory '---Selection.WholeStory、WordDoc.Selection.WholeStory均不行,可见selection对象是直接挂接在Word.Application对象下的
wordApp.Selection.Copy
'wordDoc.Add
'wordApp.Selection.Paste
Dim exfdRng As Range
Dim colCTname, colCTBunit, colCTmoney, colCTrate, colCTdraf, rowCTdraf As Integer
Dim firstAddress As String
With Worksheets("服务合同").UsedRange
Set exfdRng = .Find("项目名称", LookIn:=xlValues)
If Not exfdRng Is Nothing Then
exfdRng.Select
colCTname = exfdRng.Column
End If
Set exfdRng = .Find("乙方单位", LookIn:=xlValues)
If Not exfdRng Is Nothing Then
exfdRng.Select
colCTBunit = exfdRng.Column
End If
Set exfdRng = .Find("中标金额(含税)", LookIn:=xlValues)
If Not exfdRng Is Nothing Then
exfdRng.Select
colCTmoney = exfdRng.Column
End If
Set exfdRng = .Find("中标税率", LookIn:=xlValues)
If Not exfdRng Is Nothing Then
exfdRng.Select
colCTrate = exfdRng.Column
End If
Set exfdRng = .Find("合同编号", LookIn:=xlValues)
If Not exfdRng Is Nothing Then
exfdRng.Select
colCTdraf = exfdRng.Column
rowCTdraf = exfdRng.Row
End If
End With
Dim j As Variant
j = ThisWorkbook.Worksheets("服务合同").Cells(rowCTdraf, colCTdraf).Value
Do While ThisWorkbook.Worksheets("服务合同").Cells(rowCTdraf, colCTdraf).Value <> ""
If ThisWorkbook.Worksheets("服务合同").Cells(rowCTdraf, colCTdraf).Value = "草拟" Then
Set newDoc = wordDoc.Add
wordApp.Selection.Paste
newDoc.Activat
With newDoc.Content.Find
.ClearFormatting
.Text = "$项目名称$"
.Replacement.ClearFormatting
.Replacement.Text = "123"
.Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue
End With
End If
rowCTdraf = rowCTdraf + 1
Loop
End Sub
以上是测试代码,标红的部分没有成功替换。使用的是后绑定。vba使用WPS自带的VBA。
麻烦大佬帮忙指导,感谢啦。
|
|