|
将代码改成下面这样
- Sub test()
- Dim d As Object
- Dim wordapp As Object
- Dim mydoc As Word.Document
- Dim mytab As Word.Table
- Dim i%, j%
- Dim mypath$, myname$
- Dim arr, brr, crr(), vs
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- Set d = CreateObject("scripting.dictionary")
- Set wordapp = CreateObject("word.application")
- vs = Array(0, 1, 2, 3, 5, 6, 7, 8, 9, 4)
- mypath = ThisWorkbook.Path & ""
- If Dir(mypath & "农村土地家庭承包合同.doc") = "" Then
- MsgBox mypath & "农村土地家庭承包合同.doc不存在!"
- Exit Sub
- End If
- Set mydoc = wordapp.Documents.Open(mypath & "农村土地家庭承包合同.doc")
- wordapp.Visible = True
- With Worksheets("sheet1")
- r = .Cells(.Rows.Count, 4).End(xlUp).Row
- arr = .Range("a3:p" & r)
- For i = 1 To UBound(arr)
- If Left(arr(i, 6), 1) = "." Then
- arr(i, 6) = "0" & arr(i, 6)
- End If
- If Len(arr(i, 1)) <> 0 Then
- xm = arr(i, 1)
- Set d(arr(i, 1)) = CreateObject("scripting.dictionary")
- d(arr(i, 1))("权利人") = arr(i, 2)
- d(arr(i, 1))("宗地数") = arr(i, 3)
- d(arr(i, 1))("面积") = arr(i, 11)
- d(arr(i, 1))("合同编码") = arr(i, 12)
- End If
- If Not d(xm).Exists("地块") Then
- m = 1
- ReDim brr(1 To 9, 1 To m)
- Else
- brr = d(xm)("地块")
- m = UBound(brr, 2) + 1
- ReDim Preserve brr(1 To 9, 1 To m)
- End If
- For j = 1 To 9
- If j = 8 Then
- brr(j, m) = arr(i, 15)
- ElseIf j = 9 Then
- brr(j, m) = arr(i, 16)
- Else
- brr(j, m) = arr(i, j + 3)
- End If
- Next
- d(xm)("地块") = brr
- Next
- End With
- With Worksheets("sheet2")
- r = .Cells(.Rows.Count, 1).End(xlUp).Row
- arr = .Range("a2:e" & r)
- For i = 1 To UBound(arr)
- If d.Exists(arr(i, 1)) Then
- If Not d(arr(i, 1)).Exists("家庭") Then
- m = 1
- ReDim brr(1 To 4, 1 To m)
- Else
- brr = d(arr(i, 1))("家庭")
- m = UBound(brr, 2) + 1
- ReDim Preserve brr(1 To 4, 1 To m)
- End If
- For j = 1 To 4
- brr(j, m) = arr(i, j + 1)
- Next
- d(arr(i, 1))("家庭") = brr
- End If
- Next
- End With
- For Each aa In d.Keys
- For Each ss In Array("地块", "家庭")
- If d(aa).Exists(ss) Then
- brr = d(aa)(ss)
- ReDim crr(1 To UBound(brr, 2), 1 To UBound(brr))
- For i = 1 To UBound(brr)
- For j = 1 To UBound(brr, 2)
- crr(j, i) = brr(i, j)
- Next
- Next
- d(aa)(ss) = crr
- End If
- Next
- Next
- kk = d(1).Keys
- tt = d(1).Items
- With mydoc
- For Each aa In d.Keys
- .Fields(1).Result.Text = d(aa)("合同编码")
- .Fields(2).Result.Text = d(aa)("权利人")
- .Fields(3).Result.Text = UBound(d(aa)("家庭"))
- .Fields(4).Result.Text = d(aa)("宗地数")
- .Fields(5).Result.Text = d(aa)("面积")
- For j = 1 To 5
- .Fields(j).ShowCodes = False
- Next
- With .Tables(1)
- For i = 2 To .Rows.Count
- For j = 1 To .Columns.Count
- .Cell(i, j).Range.Text = ""
- Next
- Next
- If d(aa).Exists("家庭") Then
- brr = d(aa)("家庭")
- m = 2
- n = 1
- For i = 1 To Application.Min(UBound(brr), 15) '合同家庭共有人人数
- .Cell(m, n).Range.Text = brr(i, 1)
- .Cell(m, n + 1).Range.Text = brr(i, 3)
- .Cell(m, n + 2).Range.Text = brr(i, 4)
- m = m + 1
- If m = 7 Then '合同家庭共有人表格行数
- m = 2
- n = n + 3
- End If
- Next
- End If
- End With
- With .Tables(2)
- For i = 3 To .Rows.Count
- For j = 1 To .Columns.Count
- .Cell(i, j).Range.Text = ""
- Next
- Next
- If d(aa).Exists("地块") Then
- brr = d(aa)("地块")
- For i = 1 To Application.Min(UBound(brr), 100)
- For k = 1 To UBound(vs)
- .Cell(i + 2, vs(k)).Range.Text = brr(i, k)
- Next
- Next
- End If
- End With
- .SaveAs Filename:=mypath & aa & "_" & d(aa)("权利人") & ".doc"
- Next
- End With
- mydoc.Close False
- wordapp.Quit
- End Sub
复制代码 |
|