|
Option Explicit
Sub test()
Dim ar, br, i&, j&, wdApp As Object, strFileName$, strPath$, strSaveName$
strPath = ThisWorkbook.Path & "\"
strFileName = strPath & "培训报名表(模板).doc"
If Dir(strFileName) = "" Then MsgBox "模板文件不存在,请检查!", vbExclamation: Exit Sub
Application.ScreenUpdating = False
ar = Worksheets("数据").[A1].CurrentRegion.Value
br = [{4,2;10,3;12,4;17,7;8,9}]
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err <> 0 Then
Set wdApp = CreateObject("Word.Application")
End If
For i = 2 To UBound(ar)
With wdApp.documents.Open(strFileName)
strSaveName = strPath & ar(i, 2)
With .tables(1)
For j = 1 To UBound(br)
.Range.Cells(br(j, 1)).Range.Text = ar(i, br(j, 2))
Next j
.Range.Cells(6).Range.Text = Mid(ar(i, 4), 7, 8)
End With
.SaveAs strSaveName: .Close
End With
Next i
If Err <> 0 Then wdApp.Quit
Set wdApp = Nothing
Application.ScreenUpdating = True
Beep
End Sub
|
评分
-
1
查看全部评分
-
|