|
Option Explicit
Sub test()
Dim ar, i&, wdApp As Word.Application, strFileName$, strPath$
strPath = ThisWorkbook.Path & "\"
strFileName = strPath & "WORD文件.docx"
If Dir(strFileName) = "" Then MsgBox "WORD文件不存在,请检查!", vbExclamation: Exit Sub
Application.ScreenUpdating = False
ar = [A1].CurrentRegion.Value
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)
With .Content.Find
.ClearFormatting
.Text = Chr(32)
.Replacement.ClearFormatting
.Replacement.Text = ""
.Forward = True
.Execute Replace:=wdReplaceAll
End With
For i = 2 To UBound(ar)
With .Content.Find
.ClearFormatting
.Text = ar(i, 1)
.Forward = True
.Execute
If .Found = True Then .Parent.InsertAfter ar(i, 2)
End With
Next i
.SaveAs2 strPath & "TEST"
End With
If Err <> 0 Then wdApp.Quit
Set wdApp = Nothing
Application.ScreenUpdating = True
Beep
End Sub
|
|