|
Option Explicit
Sub test2()
Dim ar, br, i&, j&, wdApp As Object, strFileName$, strPath$, strSaveName$, Msg&
strPath = ThisWorkbook.Path & "\"
strFileName = strPath & "培训记录模板.docx"
If Dir(strFileName) = "" Then MsgBox "模板文件不存在,请检查!", vbExclamation: Exit Sub
Application.ScreenUpdating = False
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err <> 0 Then
Set wdApp = CreateObject("Word.Application")
End If
ar = [A1].CurrentRegion.Value
br = Array(6, 8, 4, 10, 18, 14, 12, 2, 16)
With wdApp.Documents.Add
strSaveName = ThisWorkbook.Path & "\生成文档"
For i = 2 To UBound(ar)
With wdApp.Documents.Open(strFileName)
With .tables(1)
For j = 0 To UBound(br)
.Range.Cells(br(j)).Range.Text = ar(i, j + 2)
Next j
End With
.Range(.Content.Start, .Content.End - 1).Copy
.Close False
End With
wdApp.Selection.Paste
Next i
.SaveAs2 strSaveName: .Close
End With
If Err <> 0 Then wdApp.Quit
Set wdApp = Nothing
Application.ScreenUpdating = True
Beep
End Sub
|
评分
-
2
查看全部评分
-
|