running a macro on a list of files

smh

New Member
Joined
Apr 14, 2009
Messages
4
Hi, I need help running a macro on various word documents using a list of the file paths/names.
I have two .xls files - one with the macro (which I got off this forum, I think) and some text in two columns. The macro currently opens a specific word document, finds text from column A, and replaces it with text in column B. Instead of opening the specific word document and performing the find/replace, I want it to go through a list of about 400 documents and do the same thing. The paths/filenames of the documents are listed in a separate .xls file. Here is the macro:
Sub conversion()

Dim WordApp As Object
Dim DocWord As Word.Document
Set WordApp = CreateObject("Word.Application")
Set DocWord = WordApp.Documents.Open("C:\Files\Document.doc")
Set myRange = DocWord.Content

myRange.Find.Execute FindText:=Cells(2, 1), ReplaceWith:=Cells(2, 2).Text, _
Replace:=wdReplaceAll
myRange.Find.Execute FindText:=Cells(3, 1), ReplaceWith:=Cells(3, 2).Text, _
Replace:=wdReplaceAll
myRange.Find.Execute FindText:=Cells(4, 1), ReplaceWith:=Cells(4, 2).Text, _
Replace:=wdReplaceAll

DocWord.Close True
Set DocWord = Nothing
Set WordApp = Nothing
End Sub

If anyone could help I would be ever so grateful. Thank you!
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Try something like the following...

Code:
Sub ProcessFiles()
Dim wkbk As Workbook, wks As Worksheet, i As Long, LR As Long, strFile As String
    
With Excel.Application
    Set wkbk = .Workbooks("Book1")
    Set wks = wkbk.Sheets(Sheet1.Name)
End With

With wks
    LR = .Cells(.Rows.Count, 1).End(xlUp).Row
    For i = 2 To LR
        strFile = .Cells(i, 1) & .Cells(i, 2)
        Call conversion(strFile)
    Next i
End With


End Sub



Sub conversion(ByVal strFileFullName As String)
Dim actWkb As Workbook, actSht As Worksheet, i As Long

Dim WordApp As Object
Dim DocWord As Word.Document
Set WordApp = CreateObject("Word.Application")
Set DocWord = WordApp.Documents.Open(strFileFullName)
Set myRange = DocWord.Content

Set actWkb = ThisWorkbook
Set actSht = actWkb.ActiveSheet

With myrng
    For i = 2 To 4
        .Find.Execute FindText:=actSht.Cells(i, 1), ReplaceWith:=actSht.Cells(i, 2).Text, Replace:=wdReplaceAll
    Next i
End With

DocWord.Close True
Set DocWord = Nothing
Set WordApp = Nothing
End Sub
 
Upvote 0
Hi Bob, thank you so much for helping me. This looks like it will do exactly what I want. Would you be so kind as to let me know what I have to replace to tell it to look in the file where the doc names are listed? I know there must be something I need to change, because I get Run Time Error '9' - Subscript out of Range.

Many thanks!
SMH
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,194
Members
449,072
Latest member
DW Draft

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top