|
- Sub 合同生成()
- On Error Resume Next
- Range("A2:A42").Select
- Range("A1:E42").Sort Key1:=Range("A2"), Order1:=xlAscending, Header:= _
- xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
- SortMethod:=xlPinYin, DataOption1:=xlSortNormal
- Dim A
- Dim arr, i As Integer, n As Integer, m As Integer, strarr As String
- Dim trow As Long
- Dim WordAppX As New Word.Application
- Dim WordDocX As Word.Document
- Dim WordTableX As Word.Table
- Dim WordRowX As Word.Row
- Dim vsrange As Word.Range
- Dim myPath As String, Fn$
- trow = Sheets("数据库").Range("A65536").End(xlUp).Row '行数
- myPath = ThisWorkbook.Path
- Set WordAppX = New Word.Application
- WordAppX.Visible = False
- arr = Sheets("数据库").Range("a2:f" & trow) '数组
- For m = 1 To trow - 1
- Set WordDocX = WordAppX.Documents.Add(myPath & "\模版.doc")
- If strarr <> arr(m, 1) & arr(m, 2) & arr(m, 3) Then
- strarr = arr(m, 1) & arr(m, 2) & arr(m, 3)
- Fn = ""
- For n = m To trow - 1
- If strarr = arr(n, 1) & arr(n, 2) & arr(n, 3) Then
- Set WordTableX = WordDocX.Tables(1)
- i = i + 1
- WordTableX.Rows.Add
-
- WordTableX.Cell(i + 1, 1).Range.Text = arr(n, 1)
- WordTableX.Cell(i + 1, 2).Range.Text = arr(n, 2)
- WordTableX.Cell(i + 1, 3).Range.Text = arr(n, 3)
- WordTableX.Cell(i + 1, 4).Range.Text = arr(n, 4)
- WordTableX.Cell(i + 1, 5).Range.Text = arr(n, 5)
- If Fn <> "" Then
- Fn = Fn & "-" & arr(n, 6)
- Else
- Fn = arr(n, 6)
- End If
- Else
- Exit For
- End If
- Next n
- If InStr(Fn, "-") > 0 Then
- If InStr(Fn, "-") <> InStrRev(Fn, "-") Then
- Fn = Left(Fn, InStr(Fn, "-")) & Right(Fn, Len(Fn) - InStrRev(Fn, "-"))
- End If
- End If
- WordDocX.SaveAs myPath & "" & Fn & ".doc"
- Set vsrange = Nothing
- Set WordTableX = Nothing
- WordDocX.Close
- i = 0
- End If
- Next m
-
- WordAppX.Quit
- Set WordDocX = Nothing
- Set WordAppX = Nothing
- End Sub
复制代码 |
|