Results 1 to 12 of 12

Replace word in MS Word with varable from MS Excel

Threaded View

  1. #1
    Registered User
    Join Date
    05-01-2009
    Location
    Seattle
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    21

    Replace word in MS Word with varable from MS Excel

    Hi everyone, I'm hoping someone could help me with this issue I'm having:

    I am in the middle of automating a process here at work, the program takes a word, "pencil" for example, from excel. It will then open up a word document with content already in it (premade template). The program will then find all instances of a string, "placepencilhere" for example, and will replace that instance with the string from excel.

    Basically I want to be able to take a variable that has a stored string value from excel and use it to replace another variable in a word document.

    I tried recording a replace (ctrl+f, replace tab) macro, copying the code, and inserting it into the excel vba code.. but I get a error message. Here is what I have:

    ---

    Sub Replacing()
    
    Dim xxx As String
    
    xxx = "testing_template"
    replacethis = "Pencil"
    
    Dim wrdApp As Word.Application
    Dim wrdDoc As Word.Document
    
    Set wrdApp = CreateObject("Word.Application")
    wrdApp.Visible = True
    Set wrdDoc = wrdApp.Documents.Open("C:\Announcement_Templates\BE\" + xxx + ".doc")
        
    Selection.Find.ClearFormatting (HERE IS WHERE I GET THE ERROR MESSAGE, THIS SECTION OF THE CODE WAS COPIED FROM WORD VBA)
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
            .Text = "placepencilhere"
            .Replacement.Text = replacethis
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    ---

    Any help is appreciated! Thanks in advance!

    odo-
    Last edited by odo; 05-15-2009 at 12:25 AM. Reason: Conform with forum rules

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1