Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

VBA Macro to open and replace text in Word from Excel

  • 05-11-2008 12:43AM
    #1
    Registered Users, Registered Users 2 Posts: 1,086 ✭✭✭


    I am trying to make my excel macro open a word file and replace all occurrences of a certain string with another string.

    I have been able to open the word file fine but cannot replace all occurrences of a certain string with another string.
    file_path = "C:\word docs\Letter.doc"
    Set wrdApp = CreateObject("Word.Application")
    Set wrdDoc = wrdApp.Documents.Open(file_path)
    
    wrdApp.Visible = True
    

    Above is my open document code which works fine.

    I have been trying to use the code below for my replacement of text in the word document with no success.
    wrdApp.Selection.Find.ClearFormatting
    wrdApp.Selection.Find.Replacement.ClearFormatting
    With wrdApp.Selection.Find
        .Text = "find"
        .Replacement.Text = "replace"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    wrdApp.Selection.Find.Execute Replace:=wdReplaceAll
    

    Any help?:confused:


Comments

  • Registered Users, Registered Users 2 Posts: 901 ✭✭✭EL_Loco


    should you not be using the "wrdDoc" object as this is the file you've actually opened?

    Just a guess atm, I'll have a go myself. brb..........


  • Registered Users, Registered Users 2 Posts: 901 ✭✭✭EL_Loco


    it's a weird one alright. It opens the document and it highlights the first "find" string it comes across, but then doesn't execute the replacement. I took out the "with" statement to see if that would help but it didn't.

    It may all boil down to excel VBA not executing this line for a word doc. I ran it under a word macro and it worked.
    wrdApp.Selection.Find.Execute Replace:=wdReplaceAll
    

    Here's the code so far, as I say, still not quite there yet.
    You could try having an intermediate word file that had the macro in it and call that from your excel vba. A bit long winded but the replacement behaves itself through word at least.
    
    Sub interactWord()
            
    Dim wrdApp As Object
    Dim wrdDoc As Object
    
    
    Set wrdApp = CreateObject("Word.Application")
    Set wrdDoc = wrdApp.Documents.Open("C:\temp\letters1.doc")
    
    wrdDoc.Activate
    wrdApp.Visible = True
    
    
        wrdApp.Selection.Find.ClearFormatting
        wrdApp.Selection.Find.Replacement.ClearFormatting
        wrdApp.Selection.Find.Text = "Find"
        wrdApp.Selection.Find.Replacement.Text = "xx"
        wrdApp.Selection.Find.Forward = True
        wrdApp.Selection.Find.Wrap = wdFindContinue
        wrdApp.Selection.Find.Format = False
        wrdApp.Selection.Find.MatchCase = False
        wrdApp.Selection.Find.MatchWholeWord = False
        wrdApp.Selection.Find.MatchWildcards = False
        wrdApp.Selection.Find.MatchSoundsLike = False
        wrdApp.Selection.Find.MatchAllWordForms = False
        wrdApp.Selection.Find.Execute Replace:=wdReplaceAll
        'Set wrdDoc = Nothing
        'Set wrdApp = Nothing
    End Sub
    


  • Moderators, Politics Moderators, Paid Member Posts: 44,215 Mod ✭✭✭✭Seth Brundle


    I haven't really looked at it but might this be of any use...
    http://www.xtremevbtalk.com/showthread.php?t=125085

    Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/ .



  • Registered Users, Registered Users 2 Posts: 901 ✭✭✭EL_Loco


    in the visual basic editor in excel (press alt+F11)
    go to Tools, References
    and tick "Microsoft Word 9.0 object library"

    My code above worked after that.


  • Registered Users, Registered Users 2 Posts: 1,086 ✭✭✭Peter B


    EL_Loco wrote: »
    in the visual basic editor in excel (press alt+F11)
    go to Tools, References
    and tick "Microsoft Word 9.0 object library"

    My code above worked after that.

    Brilliant, that worked perfectly!

    Thanks


  • Advertisement
Advertisement