+ Reply to Thread
Results 1 to 12 of 12

Replace word in MS Word with varable from MS Excel

  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:

    ---

    Please Login or Register  to view this content.
    ---

    Any help is appreciated! Thanks in advance!

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

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Replace word in MS Word with varable from Excel

    Welcome to the forum, odo.

    Please take a few minutes to read the forum rules, and then edit your post to add code tags.
    Entia non sunt multiplicanda sine necessitate

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

    Re: Replace word in MS Word with varable from MS Excel

    BUMP, no reply...

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Replace word in MS Word with varable from MS Excel

    Are you automating this from within Word, or from within Excel? If from within Excel, Selection refers to the selection in Excel.

  5. #5
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Replace word in MS Word with varable from MS Excel

    Notice how I set a reference to the word object.

    You used late binding so word variables like wdReplaceAll would mean nothing. You can use early binding, late binding or a combination of the two. Since I set the object, intellisense works even though the code uses late binding's CreateObject(). In Word's VBA Immediate Window, type ?wdReplaceAll and press enter to see the value of 2.

    Notice how I prefixed appWrd to Selection as shg explained.

    Please Login or Register  to view this content.
    Last edited by Kenneth Hobson; 05-05-2009 at 02:55 PM.

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Replace word in MS Word with varable from MS Excel

    Kenneth, you're using late binding. The OP is using early binding.

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

    Re: Replace word in MS Word with varable from MS Excel

    Quote Originally Posted by shg View Post
    Are you automating this from within Word, or from within Excel? If from within Excel, Selection refers to the selection in Excel.
    Hi, thanks for your response. I am not really proficient in the technical terms for VBA.. I am writing this code in Excel, I did a macro in Word and copied that code and pasted it into my Excel code.

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

    Re: Replace word in MS Word with varable from MS Excel

    Quote Originally Posted by Kenneth Hobson View Post
    Notice how I set a reference to the word object.

    You used late binding so word variables like wdReplaceAll would mean nothing. You can use early binding, late binding or a combination of the two. Since I set the object, intellisense works even though the code uses late binding's CreateObject(). In Word's VBA Immediate Window, type ?wdReplaceAll and press enter to see the value of 2.

    Notice how I prefixed appWrd to Selection as shg explained.

    Please Login or Register  to view this content.
    Hi! Thanks for your reply. I am writing this in Excel, I will play around with your code to see whats going on. I'll check out what early binding/late binding is. Is this code for Excel or Word? I'm a little confused by what you mean "In Word's VBA Immediate Window, type ?wdReplaceAll and press enter to see the value of 2."
    Last edited by odo; 05-05-2009 at 03:08 PM. Reason: For Clarification

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Replace word in MS Word with varable from MS Excel

    Try this:
    Please Login or Register  to view this content.
    You MUST set reference to Microsoft Word xx.x Object Library
    Last edited by shg; 05-05-2009 at 03:14 PM. Reason: Forgot the wdApp, lol ...

  10. #10
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Replace word in MS Word with varable from MS Excel

    In this example, I show how to do either early or late binding. I also put a link to a Microsoft article that explains the two methods.

    The late bound method is shown first. Comment that section out and uncomment the previous and set the reference to use it.

    For development, one generally uses early binding. For production use, most will use late binding so that they don't have to worry about the reference. Each method has advantages and disadvantages.
    Please Login or Register  to view this content.

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

    Re: Replace word in MS Word with varable from MS Excel

    Shg, Thank you! the code worked. I am currently integrating it into the rest of my program~

    Kenneth, Thank you too! I will try to switch my code to use late binding so that reference doesnt always have to be set first.

    Appreciate your guys's help~

    odo-

  12. #12
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Replace word in MS Word with varable from MS Excel

    As Kenneth point out, early and late binding both have their advantages.

    Early binding is faster at run-time, and you get IntelliSense.

    Late binding allows Excel to choose from several versions of a library, which is convenient if you're deploying an application to users who may have various versions of (in this case) Word.

+ Reply to Thread

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