|
Option Explicit
Sub test()
Dim ar, i&, j&, tRow&, wdApp As Word.Application, strFileName$, strPath$
strPath = ThisWorkbook.Path & "\"
strFileName = strPath & "证书.docx"
If Dir(strFileName) = "" Then MsgBox "指定的文件不存在,请检查!", vbExclamation: Exit Sub
ar = [A1].CurrentRegion.Value
If ActiveCell.Row = 1 Or ActiveCell.Row > UBound(ar) Then
MsgBox "请正确选择": Exit Sub
Else
tRow = ActiveCell.Row
End If
If ar(tRow, 3) = Empty Then MsgBox "姓名为空": Exit Sub
Application.ScreenUpdating = False
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err <> 0 Then
Set wdApp = New Word.Application
'wdApp.Visible = True
End If
With wdApp.documents.Open(strFileName)
strFileName = strPath & ar(tRow, 3)
For j = 1 To UBound(ar, 2)
With .Content.Find
.ClearFormatting
.Text = "数据" & Format(j, "000")
.Replacement.ClearFormatting
.Replacement.Text = ar(tRow, j)
.Execute Replace:=wdReplaceAll
End With
Next j
.SaveAs2 strFileName
.Close
End With
If Err <> 0 Then wdApp.Quit
Set wdApp = Nothing
Application.ScreenUpdating = True
Beep
End Sub
|
评分
-
1
查看全部评分
-
|