|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Sub 批量委托书()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim wdApp As Object
Set wdApp = CreateObject("Word.Application") '新建Word对象
Dim wdd
t = Timer
Dim ar As Variant
lj = ThisWorkbook.Path
mb = lj & "\委托书 .docx"
With ActiveSheet
r = .Cells(Rows.Count, 1).End(xlUp).Row
ar = .Range("a1:a" & r)
End With
wdApp.Visible = True
For i = 2 To UBound(ar)
If Len(Trim(ar(i, 1))) > 0 Then
wj = ar(i, 1)
FileCopy mb, lj & "\" & wj & ".docx"
Set wdd = wdApp.Documents.Open(lj & "\" & wj & ".docx")
With wdApp.Selection
.HomeKey unit:=6 '光标置于文件首
If .Find.Execute("数据A") Then '查找到指定字符串
.Font.Color = wdColorAutomatic '字符为自动颜色
.Text = ar(i, 1) '替换字符串
End If
End With
wdd.Save
wdd.Close True
Set wdd = Nothing
End If
Next i
wdApp.Quit
Set wdApp = Nothing
Application.DisplayAlerts = True
Application.ScreenUpdating = True
MsgBox "耗时:" & Format(Timer - t, "0.00") & "秒"
End Sub
|
|