|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Option Explicit
Sub test()
Dim ar, br(), r&, i&, j&, k&, wdApp As Object, strFileName$, strPath$, strSaveName$
strPath = ThisWorkbook.Path & "\"
strFileName = strPath & "文字日报.doc"
If Dir(strFileName) = "" Then MsgBox "指定的文件不存在,请检查!", vbExclamation: Exit Sub
Application.ScreenUpdating = True
ar = [A1].CurrentRegion.Value
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err <> 0 Then
Set wdApp = CreateObject("Word.Application")
End If
For i = 2 To UBound(ar)
With wdApp.documents.Open(strFileName)
strSaveName = strPath & ar(i, 2)
For j = 1 To UBound(ar, 2)
Erase br(): r = 0
With .Content.Find
.ClearFormatting
.Text = "数据" & Format(j, "000")
.Forward = True
Do While .Execute
r = r + 1
ReDim Preserve br(1 To r)
Set br(r) = wdApp.ActiveDocument.Range(.Parent.Start, .Parent.End)
Loop
End With
If r Then
For k = 1 To UBound(br)
br(k).Text = ar(i, j)
Next k
End If
Next j
.SaveAs strSaveName: .Close
End With
Next i
If Err <> 0 Then wdApp.Quit
Set wdApp = Nothing
Application.ScreenUpdating = True
Beep
End Sub
|
|