|
Sub 按模板生成文档()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim i%, k%, x%, arr, d, krr, kr, myPath$, wdApp, wdD
Dim oFso
Set oFso = CreateObject("Scripting.FileSystemObject")
myPath = ThisWorkbook.Path & "\"
Set wdApp = CreateObject("word.application")
wdApp.Visible = True
arr = Sheets("Sheet1").Range("A1:e" & Cells(Rows.Count, 1).End(xlUp).Row)
For i = 2 To UBound(arr)
If Trim(arr(i, 1)) <> "" Then
FileCopy myPath & "简化测评模板.docx", myPath & "生成的文件\" & arr(i, 1) & "-" & arr(i, 2) & ".docx"
Set wdD = wdApp.Documents.Open(myPath & "生成的文件\" & arr(i, 1) & "-" & arr(i, 2) & ".docx")
For j = 1 To 2
With wdApp
.Selection.HomeKey unit:=6 'wdStory '光标置于文件首
If .Selection.Find.Execute(arr(1, j)) Then '查找到指定字符串
.Selection.Text = arr(i, j) '替换字符串
End If
End With
Next j
With wdD.Tables(1)
For j = 3 To UBound(arr, 2)
.Cell(j - 1, 2).Range.Text = arr(i, j)
Next j
End With
wdD.Save
wdD.Close
End If
Next i
wdApp.Quit
Set wdD = Nothing
Set wdApp = Nothing
Application.DisplayAlerts = True
Application.ScreenUpdating = False
MsgBox "ok!"
End Sub
|
|