|
Option Explicit
Option Compare Text
Sub test1()
Dim ar(), br, i&, j&, wdApp As Word.Application
Call vFilesListDg(ThisWorkbook.Path, ar(), i, "doc*", ThisWorkbook.Name)
If i = 0 Then Exit Sub
Application.ScreenUpdating = False
br = [A1].CurrentRegion.Value
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err <> 0 Then
Set wdApp = New Word.Application
End If
For j = 1 To UBound(ar)
With wdApp.documents.Open(ar(j))
For i = 2 To UBound(br)
With .Content.Find
.ClearFormatting
.Text = br(i, 1)
.Replacement.ClearFormatting
.Replacement.Text = br(i, 2)
.Execute Replace:=wdReplaceAll
End With
Next i
.Close True
End With
Next j
If Err <> 0 Then wdApp.Quit
Set wdApp = Nothing
Application.ScreenUpdating = True
Beep
End Sub
Function vFilesListDg(ByVal strPath$, ByRef ar(), Optional ByRef iGroup&, Optional ByVal strExtension$ = "*", _
Optional ByVal strExclude As String = "", Optional ByVal isFolder As Boolean)
Dim Fso As Object, objFold As Object, vFile, vFold
Set Fso = CreateObject("Scripting.FileSystemObject")
Set objFold = Fso.GetFolder(strPath)
If isFolder Then
For Each vFile In objFold.subfolders
iGroup = iGroup + 1
ReDim Preserve ar(1 To iGroup)
ar(iGroup) = vFile.Path
Next
Else
For Each vFile In objFold.Files
If Fso.GetExtensionName(vFile.Name) Like strExtension Then
If vFile.Name <> strExclude And Not vFile.Name Like "~$*" Then
iGroup = iGroup + 1
ReDim Preserve ar(1 To iGroup)
ar(iGroup) = vFile.Path
End If
End If
Next
End If
For Each vFold In objFold.subfolders
vFilesListDg vFold, ar, iGroup, strExtension, strExclude, isFolder
Next vFold
End Function
|
|