Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50090

Need help with Excel-to-Word find and replace macro based on word filename

$
0
0
Forgive me if this is a dumb question or not possible, I'm very new to all of this...

Basically, I want to run a macro from Excel that will open all Word documents in a specified folder and run a find-and-replace on them based on the names of the Word documents. The names of the Word documents will be present in Excel cells. So basically, I want to be able to point the macro to a folder and have it open every Word document within, and if, for example, the file name matches the contents of cell A1, have it replace a pre-specified term wherever it appears in that Word file with the contents of cell B1. I'm trying to get it to a simple find-and-replace on just one file before I get into the opening multiple files and using the file name as criteria parts, and I can't even get that to work. What I have so far is

Code:

Sub FR()
    Dim WordApp As Object
    Dim WordDoc As Word.Document
    Dim WordContent As Word.Range
    '------------------------------------------------------------------------
    Set WordApp = CreateObject("Word.Application")
    WordApp.Visible = True
    Set WordDoc = WordApp.Documents.Open("c:\test\test1.docx")
    Set WordContent = WordDoc.Content
    '------------------------------------------------------------------------
    '- FIND & REPLACE
    With WordContent.Find
        .Forward = True
        .ClearFormatting
        .MatchCase = False
        .Wrap = wdFindContinue
        .Text = "ADJUSTOR"
        .Replacement = Range("B1")
        .Execute Replace:=wdReplaceAll
    End With
    '------------------------------------------------------------------------
    Set WordDoc = Nothing
    Set WordApp = Nothing
    Beep
End Sub

I'm getting error 438: object doesn't support this property or method. Any help would be greatly appreciated.


Moderator's Note: Welcome to the forum, you have to put code tags around codes. Select the code then hit the "#". I'll do it for now. Thank you.

Viewing all articles
Browse latest Browse all 50090

Trending Articles