|
- Sub test()
- Dim r%, i%
- Dim arr, brr
- Dim flg As Boolean
- Dim mypath$, myname$
- Dim xlapp As Excel.Application
- Dim wb As Excel.Workbook
- Dim ws As Excel.Worksheet
- Dim d As Object
- Set d = CreateObject("scripting.dictionary")
- On Error Resume Next
- Set xlapp = GetObject(, "excel.application")
- If Err Then
- flg = True
- Set xlapp = CreateObject("excel.application")
- End If
- On Error GoTo 0
- mypath = ThisDocument.Path & ""
- myname = "文档.xls"
- If Dir(mypath & myname) = "" Then
- MsgBox mypath & myname & "不存在!"
- Exit Sub
- End If
- Set wb = xlapp.Workbooks.Open(FileName:=mypath & myname)
- xlapp.Visible = True
- With wb
- With .Worksheets("文档")
- r = .Range("b1").End(xlDown).Row
- arr = .Range("a2:j" & r)
- For i = 1 To UBound(arr)
- If Not d.Exists(arr(i, 2)) Then
- Set d(arr(i, 2)) = CreateObject("scripting.dictionary")
- End If
- d(arr(i, 2))(i) = Empty
- Next
- End With
- .Close False
- End With
- With ThisDocument
- For Each aa In d.keys
- .Tables(1).Select
- Selection.MoveUp unit:=wdLine, Count:=1
- Selection.HomeKey unit:=wdLine
- Selection.EndKey unit:=wdLine, Extend:=wdExtend
- Selection.MoveEnd unit:=wdCharacter, Count:=-1
- Selection.Range.Text = "单位名称:" & aa
-
- With .Tables(1)
- If .Rows.Count > 1 Then
- For i = .Rows.Count To 2 Step -1
- .Rows(i).Delete
- Next
- End If
- .Rows(1).Range.Select
- Selection.InsertRowsBelow numrows:=d(aa).Count
- For i = 1 To .Rows.Count
- .Rows(i).Height = CentimetersToPoints(1)
- Next
- m = 1
- For Each bb In d(aa).keys
- m = m + 1
- For j = 3 To UBound(arr, 2)
- .Cell(m, j - 1).Range.Text = arr(bb, j)
- Next
- Next
- End With
- ThisDocument.PrintOut
- Next
- End With
- End Sub
复制代码 |
|