Sub 填充word()
Application.ScreenUpdating = False
Dim i%, d, myPath$, wdApp, wdD
Set wdApp = CreateObject("word.application")
Set d = CreateObject("scripting.dictionary")
myPath = ThisWorkbook.Path & "\"
ar = Sheet1.[a1].CurrentRegion
For i = 2 To UBound(ar)
If Trim(ar(i, 2)) <> "" Then
d(Trim(ar(i, 2))) = ""
End If
Next i
For Each k In d.keys
n = 0
ReDim br(1 To UBound(ar), 1 To 16)
For i = 2 To UBound(ar)
If Trim(ar(i, 2)) = k Then
n = n + 1
br(n, 1) = ar(i, 5)
br(n, 2) = ar(i, 8)
br(n, 3) = ar(i, 9)
br(n, 4) = ar(i, 10)
br(n, 5) = ar(i, 12)
br(n, 6) = ar(i, 13)
br(n, 7) = ar(i, 14)
br(n, 8) = ar(i, 15)
If Trim(ar(i, 16)) = "是" Then
br(n, 9) = ar(i, 17)
br(n, 10) = ar(i, 18)
ElseIf Trim(ar(i, 16)) = "否" Then
br(n, 9) = ""
br(n, 10) = ""
br(n, 11) = "否"
End If
br(n, 12) = ar(i, 19)
br(n, 13) = ar(i, 20)
br(n, 14) = ar(i, 21)
br(n, 15) = ar(i, 22)
br(n, 16) = ar(i, 23)
zz = ar(i, 11)
hz = ar(i, 5)
End If
Next i
FileCopy myPath & "调查表模板.docx", myPath & "生成的文件\" & hz & "调查表.docx"
Set wdD = wdApp.Documents.Open(myPath & "生成的文件\" & hz & "调查表.docx")
wdApp.Visible = False
With wdD.Tables(1)
For i = 1 To n
For j = 1 To UBound(br, 2)
.Cell(i + 5, j).Range.Text = br(i, j)
Next j
Next i
End With
wdD.Save
wdD.Close
Next k
wdApp.Quit
Set wdD = Nothing
Set wdApp = Nothing
Application.ScreenUpdating = True
MsgBox "ok!"
End Sub
|