|
Sub 批量生成word()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim d As Object, dc As Object
Set d = CreateObject("scripting.dictionary")
Set dc = CreateObject("scripting.dictionary")
Dim wdApp As Object
Set wdApp = CreateObject("Word.Application") '新建Word对象
wdApp.Visible = True
lj = ThisWorkbook.Path & "\"
mb = lj & "模板.docx"
tt = Timer
Set ww = ThisWorkbook
With ww.Worksheets("Sheet1")
rs = .Cells(Rows.Count, 1).End(xlUp).Row
ar = .Range("a1:l" & rs)
End With
For i = 2 To UBound(ar) - 1 Step 2
mc = ar(i, 1) & "-" & ar(i + 1, 1)
FileCopy mb, lj & "\生成的表格\" & mc & ".docx"
Set wdd = wdApp.Documents.Open(lj & "\生成的表格\" & mc & ".docx")
For s = i To i + 1
For j = 1 To 12
zd = ar(s, j)
With wdApp.Selection
.HomeKey unit:=6 '光标置于文件首
If .Find.Execute("数据" & Format(j, "00")) Then '查找到指定字符串
.Font.Color = wdColorAutomatic '字符为自动颜色
.Text = zd '替换字符串
End If
End With
Next j
Next s
wdd.Close True
Next i
wdApp.Quit
Set wdd = Nothing
Set wdApp = Nothing
Application.ScreenUpdating = True
Application.DisplayAlerts = True
MsgBox "耗时:" & Format(Timer - tt, "0.00") & "秒"
End Sub
|
|