|
Sub 生成购销合同()
Dim appWD, doc As Object, f, arr, i, j, r
Dim rn As Range
With Sheets("外销报关出口明细报表")
r = .Cells(Rows.Count, 4).End(xlUp).Row
ws = .Range("a4:h" & r).Find("合计", , , , , , , 1).Row
ar = .Range("a4:aa" & ws)
End With
rr = Array(4, 26, 27, 24, 23)
Set appWD = CreateObject("Word.Application")
f = ThisWorkbook.Path & "\购销合同模版.docx"
For i = 2 To UBound(ar)
If ar(i, 4) <> "" Then
ht = ar(i, 4)
FileCopy f, ThisWorkbook.Path & "\购销合同\购销合同" & ht & ".docx"
Set doc = appWD.Documents.Open(ThisWorkbook.Path & "\购销合同\购销合同" & ht & ".docx")
appWD.Visible = True
m = 0
With appWD
For s = 0 To UBound(rr)
m = m + 1
lh = rr(s)
Str1 = "sj" & m
.Selection.HomeKey Unit:=6 '光标置于文件首
If .Selection.Find.Execute(Str1) Then '查找到指定字符串
.Selection.Font.Color = wdColorAutomatic '字符为自动颜色
.Selection.Text = ar(i, lh) '替换字符串
End If
Next s
.ActiveDocument.Tables(1).Cell(2, 2).Range = ar(i, 4)
.ActiveDocument.Tables(1).Cell(2, 3).Range = ar(i, 8)
.ActiveDocument.Tables(1).Cell(2, 5).Range = ar(i, 9)
.ActiveDocument.Tables(1).Cell(4, 4).Range = ar(i, 9)
End With
doc.Close True
End If
Next i
appWD.Quit
MsgBox "ok!"
End Sub
|
|