|
Sub 生成word()
Application.ScreenUpdating = False
Dim ar As Variant
Dim i As Long, r As Long, rs As Long
Dim br()
Dim d As Object
Set d = CreateObject("scripting.dictionary")
Set wdApp = CreateObject("word.application")
wdApp.Visible = True
myPath = ThisWorkbook.Path & "\"
With Sheets("资料")
r = .Cells(Rows.Count, 1).End(xlUp).Row
If r < 2 Then MsgBox "数据源为空,请先导入数据!": End
ar = .Range("a1:h" & r)
End With
For i = 2 To UBound(ar)
If ar(i, 1) <> "" Then
If Not d.exists(ar(i, 1)) Then Set d(ar(i, 1)) = CreateObject("scripting.dictionary")
d(ar(i, 1))(i) = ""
End If
Next i
For Each k In d.keys
n = 0
ReDim br(1 To UBound(ar), 1 To UBound(ar, 2) - 1)
For Each kk In d(k).keys
n = n + 1
For j = 2 To UBound(ar, 2)
br(n, j - 1) = ar(kk, j)
Next j
Next kk
FileCopy myPath & "登记表.docx", myPath & "一户一表\" & k & ".docx"
Set wdD = wdApp.Documents.Open(myPath & "一户一表\" & k & ".docx")
With wdD.Tables(1)
.cell(2, 1).Range.Text = "户号:" & k
.cell(2, 7).Range.Text = "家庭总人口数:共 " & n & " 人"
For i = 1 To n
For j = 1 To 7
.cell(i + 3, j + 1).Range.Text = br(i, j)
Next j
Next i
End With
wdD.Save
wdD.Close True
Next k
wdApp.Quit
Set wdD = Nothing
Set wdApp = Nothing
Application.ScreenUpdating = False
MsgBox "ok!"
End Sub
|
|