|
Sub 批量创建模板文件()
Application.ScreenUpdating = False '关闭屏幕刷新
arr = Sheets(1).UsedRange '表格数据位置
'设置一个WORD对象
Set wdapp = CreateObject("word.application")
'模板路径
f = ThisWorkbook.Path & "\例外续卡申请审批表模板.doc"
'WORD设置可见
wdapp.Visible = False
'打开word文档
Set wdoc = wdapp.Documents.Open(f)
'开始替换word数据
For j = 4 To UBound(arr)
If Len(arr(j, 2)) > 0 Then
'word的第一张表格中,第一行,第二列,替换为EXCEL中的第J行,第二个单元格内容
wdoc.Tables(1).Cell(1, 2) = arr(j, 1)
wdoc.Tables(1).Cell(1, 4) = arr(j, 2)
wdoc.Tables(1).Cell(2, 2) = arr(j, 3)
wdoc.Tables(1).Cell(3, 2) = arr(j, 4) & "元"
wdoc.Tables(1).Cell(3, 4) = arr(j, 5)
'开始替换模板预置变量文本
With wdapp.Application.Selection
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
With .Find
.Text = "K95"
.Replacement.Text = Range("B2")
End With
.Find.Execute Replace:=wdReplaceAll
With .Find
.Text = "K96"
.Replacement.Text = Range("C2")
End With
.Find.Execute Replace:=wdReplaceAll
With .Find
.Text = "K97"
.Replacement.Text = Range("D2")
End With
.Find.Execute Replace:=wdReplaceAll
With .Find
.Text = "K98"
.Replacement.Text = Range("F2")
End With
.Find.Execute Replace:=wdReplaceAll
With .Find
.Text = "K99"
.Replacement.Text = Range("G2")
End With
.Find.Execute Replace:=wdReplaceAll
End With
'文件另存
wdoc.SaveAs ThisWorkbook.Path & "\" & arr(j, 1) & "例外续卡申请审批表" & ".doc"
End If
Next j
wdoc.Close
Application.ScreenUpdating = False
If 判断 = 0 Then
i = MsgBox("已生成完毕!", 0 + 48 + 256 + 0, "提示:")
End If
End Sub
|
-
|