|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Sub test()
- Dim r%, i%
- Dim arr, brr
- Dim mypath, myname
- Dim wordapp As Object
- If Dir(ThisWorkbook.Path & "\培训记录模板.docx") = "" Then
- MsgBox ThisWorkbook.Path & "\培训记录模板.docx不存在!"
- Exit Sub
- End If
- With Worksheets("sheet1")
- .AutoFilterMode = False
- r = .Cells(.Rows.Count, 1).End(xlUp).Row
- arr = .Range("a2:j" & r)
- End With
- Set wordapp = CreateObject("word.application")
- wordapp.Visible = True
- Set mydoc = wordapp.Documents.Open(ThisWorkbook.Path & "\培训记录模板.docx")
- With mydoc
- .tables(1).Select
- wordapp.Selection.MoveDown wdLine, 1, wdMove
- wordapp.Selection.EndKey wdStory, wdExtend
- wordapp.Selection.Delete
- .tables(1).Select
- wordapp.Selection.MoveUp wdLine, 1, wdMove
- wordapp.Selection.EndKey wdLine, wdExtend
- wordapp.Selection.End = .tables(1).Range.End
- wordapp.Selection.Copy
- For i = 1 To UBound(arr) - 1
- With .Paragraphs.Last.Range
- .InsertParagraphAfter
- .Paste
- End With
- Next
- For i = 1 To UBound(arr)
- With .tables(i)
- .Cell(1, 2).Range.Text = arr(i, 9)
- .Cell(1, 4).Range.Text = arr(i, 4)
- .Cell(2, 2).Range.Text = arr(i, 2)
- .Cell(3, 2).Range.Text = arr(i, 3)
- .Cell(4, 2).Range.Text = arr(i, 5)
- .Cell(4, 4).Range.Text = arr(i, 8)
- .Cell(5, 2).Range.Text = arr(i, 7)
- .Cell(5, 4).Range.Text = arr(i, 10)
- .Cell(6, 2).Range.Text = arr(i, 6)
- End With
- Next
- .SaveAs2 ThisWorkbook.Path & "\培训记录.docx"
- .Close
- End With
- wordapp.Quit
- MsgBox "培训记录生成完毕!"
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|