VBA search replace text in word document from excel












-1















I am trying to make an excel document that I can paste a list of words to search and replace into. I want to use the excel document to search and replace these words in a word document.



I am having issues getting the code to function.



Sub SearchReplace()

Dim WordDoc As Object, N As Variant, i As Integer, j As Integer
i = Range("C2").Value 'pulls length of list from an excel function located in cell C2
N = Range("B4:C" & CStr(i + 3)).Value
Set WordDoc = CreateObject(Class:="Word.Application")
WordDoc.Visible = True
WordDoc.Documents.Open Filename:="C:WordTest.docm"
WordDoc.Activate
With WordDoc.ActiveDocument
For j = 1 To i
With .Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.MatchWildcards = False
.Wrap = wdFindContinue
.Text = N(1, j)
.Replacement.Text = N(2, j)
.Execute
End With
End With
Next j
End With
WordDoc.Quit
Set WordDoc = Nothing
End Sub


I corrected the code based on the recommendations, and I no longer get any errors. However the code does not appear to find and replace anything in the specified word document. I tried to simplify the code to check the find & replace portion by typing in a specific word "text" and a specific word "replace" to find and replace in a word document, which contains the word "text". This still result in no change. I removed the line that closed the document in case the issue was the document was not being saved after being updated, but that also was not successful, so I added WordDoc.Quit back into the code.



Sub SearchReplace()

Dim WordDoc As Object, N As Variant, i As Integer, j As Integer

i = Range("C2").Value 'pulls length of list from an excel function
located in cell C2
N = Range("B4:C" & CStr(i + 3)).Value
Set WordDoc = CreateObject(Class:="Word.Application")
WordDoc.Visible = True
WordDoc.Documents.Open Filename:="C:WordTest.docm"
WordDoc.Documents("WordTest.docm").Activate
With WordDoc.ActiveDocument
For j = 1 To i
With .Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.MatchWildcards = False
.Wrap = 1
.Text = "text" 'N(j, 1)
.Replacement.Text = "replace" 'N(j, 2)
.Execute 2
End With
End With
Next j
End With
WordDoc.Quit
Set WordDoc = Nothing
End Sub









share|improve this question




















  • 2





    Can you tell us where you get the first error message when you run the code and what is says?

    – rohrl77
    Nov 13 '18 at 19:00






  • 1





    Please describe in the issues in detail. We can't possibly reproduce your situation and problems based on the minimal information you provide.

    – Cindy Meister
    Nov 13 '18 at 19:53











  • Unless you've added a reference to the Word object library, Excel isn't going to know what the value of wdFindContinue is. You'll need to either add the reference, or define that constant. And use Option Explicit so you always know about this type of problem.

    – Tim Williams
    Nov 13 '18 at 20:11













  • I think you have your column and row indexes transposed. It should be N(j,1) and N(j,2).

    – Freeflow
    Nov 13 '18 at 20:29











  • Once I included Option Explicit I received an error on .Wrap=wdFindContinue. I do not understand how to define this in excel, would you please explain.

    – JHEngr
    Nov 13 '18 at 21:32
















-1















I am trying to make an excel document that I can paste a list of words to search and replace into. I want to use the excel document to search and replace these words in a word document.



I am having issues getting the code to function.



Sub SearchReplace()

Dim WordDoc As Object, N As Variant, i As Integer, j As Integer
i = Range("C2").Value 'pulls length of list from an excel function located in cell C2
N = Range("B4:C" & CStr(i + 3)).Value
Set WordDoc = CreateObject(Class:="Word.Application")
WordDoc.Visible = True
WordDoc.Documents.Open Filename:="C:WordTest.docm"
WordDoc.Activate
With WordDoc.ActiveDocument
For j = 1 To i
With .Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.MatchWildcards = False
.Wrap = wdFindContinue
.Text = N(1, j)
.Replacement.Text = N(2, j)
.Execute
End With
End With
Next j
End With
WordDoc.Quit
Set WordDoc = Nothing
End Sub


I corrected the code based on the recommendations, and I no longer get any errors. However the code does not appear to find and replace anything in the specified word document. I tried to simplify the code to check the find & replace portion by typing in a specific word "text" and a specific word "replace" to find and replace in a word document, which contains the word "text". This still result in no change. I removed the line that closed the document in case the issue was the document was not being saved after being updated, but that also was not successful, so I added WordDoc.Quit back into the code.



Sub SearchReplace()

Dim WordDoc As Object, N As Variant, i As Integer, j As Integer

i = Range("C2").Value 'pulls length of list from an excel function
located in cell C2
N = Range("B4:C" & CStr(i + 3)).Value
Set WordDoc = CreateObject(Class:="Word.Application")
WordDoc.Visible = True
WordDoc.Documents.Open Filename:="C:WordTest.docm"
WordDoc.Documents("WordTest.docm").Activate
With WordDoc.ActiveDocument
For j = 1 To i
With .Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.MatchWildcards = False
.Wrap = 1
.Text = "text" 'N(j, 1)
.Replacement.Text = "replace" 'N(j, 2)
.Execute 2
End With
End With
Next j
End With
WordDoc.Quit
Set WordDoc = Nothing
End Sub









share|improve this question




















  • 2





    Can you tell us where you get the first error message when you run the code and what is says?

    – rohrl77
    Nov 13 '18 at 19:00






  • 1





    Please describe in the issues in detail. We can't possibly reproduce your situation and problems based on the minimal information you provide.

    – Cindy Meister
    Nov 13 '18 at 19:53











  • Unless you've added a reference to the Word object library, Excel isn't going to know what the value of wdFindContinue is. You'll need to either add the reference, or define that constant. And use Option Explicit so you always know about this type of problem.

    – Tim Williams
    Nov 13 '18 at 20:11













  • I think you have your column and row indexes transposed. It should be N(j,1) and N(j,2).

    – Freeflow
    Nov 13 '18 at 20:29











  • Once I included Option Explicit I received an error on .Wrap=wdFindContinue. I do not understand how to define this in excel, would you please explain.

    – JHEngr
    Nov 13 '18 at 21:32














-1












-1








-1








I am trying to make an excel document that I can paste a list of words to search and replace into. I want to use the excel document to search and replace these words in a word document.



I am having issues getting the code to function.



Sub SearchReplace()

Dim WordDoc As Object, N As Variant, i As Integer, j As Integer
i = Range("C2").Value 'pulls length of list from an excel function located in cell C2
N = Range("B4:C" & CStr(i + 3)).Value
Set WordDoc = CreateObject(Class:="Word.Application")
WordDoc.Visible = True
WordDoc.Documents.Open Filename:="C:WordTest.docm"
WordDoc.Activate
With WordDoc.ActiveDocument
For j = 1 To i
With .Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.MatchWildcards = False
.Wrap = wdFindContinue
.Text = N(1, j)
.Replacement.Text = N(2, j)
.Execute
End With
End With
Next j
End With
WordDoc.Quit
Set WordDoc = Nothing
End Sub


I corrected the code based on the recommendations, and I no longer get any errors. However the code does not appear to find and replace anything in the specified word document. I tried to simplify the code to check the find & replace portion by typing in a specific word "text" and a specific word "replace" to find and replace in a word document, which contains the word "text". This still result in no change. I removed the line that closed the document in case the issue was the document was not being saved after being updated, but that also was not successful, so I added WordDoc.Quit back into the code.



Sub SearchReplace()

Dim WordDoc As Object, N As Variant, i As Integer, j As Integer

i = Range("C2").Value 'pulls length of list from an excel function
located in cell C2
N = Range("B4:C" & CStr(i + 3)).Value
Set WordDoc = CreateObject(Class:="Word.Application")
WordDoc.Visible = True
WordDoc.Documents.Open Filename:="C:WordTest.docm"
WordDoc.Documents("WordTest.docm").Activate
With WordDoc.ActiveDocument
For j = 1 To i
With .Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.MatchWildcards = False
.Wrap = 1
.Text = "text" 'N(j, 1)
.Replacement.Text = "replace" 'N(j, 2)
.Execute 2
End With
End With
Next j
End With
WordDoc.Quit
Set WordDoc = Nothing
End Sub









share|improve this question
















I am trying to make an excel document that I can paste a list of words to search and replace into. I want to use the excel document to search and replace these words in a word document.



I am having issues getting the code to function.



Sub SearchReplace()

Dim WordDoc As Object, N As Variant, i As Integer, j As Integer
i = Range("C2").Value 'pulls length of list from an excel function located in cell C2
N = Range("B4:C" & CStr(i + 3)).Value
Set WordDoc = CreateObject(Class:="Word.Application")
WordDoc.Visible = True
WordDoc.Documents.Open Filename:="C:WordTest.docm"
WordDoc.Activate
With WordDoc.ActiveDocument
For j = 1 To i
With .Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.MatchWildcards = False
.Wrap = wdFindContinue
.Text = N(1, j)
.Replacement.Text = N(2, j)
.Execute
End With
End With
Next j
End With
WordDoc.Quit
Set WordDoc = Nothing
End Sub


I corrected the code based on the recommendations, and I no longer get any errors. However the code does not appear to find and replace anything in the specified word document. I tried to simplify the code to check the find & replace portion by typing in a specific word "text" and a specific word "replace" to find and replace in a word document, which contains the word "text". This still result in no change. I removed the line that closed the document in case the issue was the document was not being saved after being updated, but that also was not successful, so I added WordDoc.Quit back into the code.



Sub SearchReplace()

Dim WordDoc As Object, N As Variant, i As Integer, j As Integer

i = Range("C2").Value 'pulls length of list from an excel function
located in cell C2
N = Range("B4:C" & CStr(i + 3)).Value
Set WordDoc = CreateObject(Class:="Word.Application")
WordDoc.Visible = True
WordDoc.Documents.Open Filename:="C:WordTest.docm"
WordDoc.Documents("WordTest.docm").Activate
With WordDoc.ActiveDocument
For j = 1 To i
With .Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.MatchWildcards = False
.Wrap = 1
.Text = "text" 'N(j, 1)
.Replacement.Text = "replace" 'N(j, 2)
.Execute 2
End With
End With
Next j
End With
WordDoc.Quit
Set WordDoc = Nothing
End Sub






excel vba ms-word






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 1:45







JHEngr

















asked Nov 13 '18 at 18:54









JHEngrJHEngr

11




11








  • 2





    Can you tell us where you get the first error message when you run the code and what is says?

    – rohrl77
    Nov 13 '18 at 19:00






  • 1





    Please describe in the issues in detail. We can't possibly reproduce your situation and problems based on the minimal information you provide.

    – Cindy Meister
    Nov 13 '18 at 19:53











  • Unless you've added a reference to the Word object library, Excel isn't going to know what the value of wdFindContinue is. You'll need to either add the reference, or define that constant. And use Option Explicit so you always know about this type of problem.

    – Tim Williams
    Nov 13 '18 at 20:11













  • I think you have your column and row indexes transposed. It should be N(j,1) and N(j,2).

    – Freeflow
    Nov 13 '18 at 20:29











  • Once I included Option Explicit I received an error on .Wrap=wdFindContinue. I do not understand how to define this in excel, would you please explain.

    – JHEngr
    Nov 13 '18 at 21:32














  • 2





    Can you tell us where you get the first error message when you run the code and what is says?

    – rohrl77
    Nov 13 '18 at 19:00






  • 1





    Please describe in the issues in detail. We can't possibly reproduce your situation and problems based on the minimal information you provide.

    – Cindy Meister
    Nov 13 '18 at 19:53











  • Unless you've added a reference to the Word object library, Excel isn't going to know what the value of wdFindContinue is. You'll need to either add the reference, or define that constant. And use Option Explicit so you always know about this type of problem.

    – Tim Williams
    Nov 13 '18 at 20:11













  • I think you have your column and row indexes transposed. It should be N(j,1) and N(j,2).

    – Freeflow
    Nov 13 '18 at 20:29











  • Once I included Option Explicit I received an error on .Wrap=wdFindContinue. I do not understand how to define this in excel, would you please explain.

    – JHEngr
    Nov 13 '18 at 21:32








2




2





Can you tell us where you get the first error message when you run the code and what is says?

– rohrl77
Nov 13 '18 at 19:00





Can you tell us where you get the first error message when you run the code and what is says?

– rohrl77
Nov 13 '18 at 19:00




1




1





Please describe in the issues in detail. We can't possibly reproduce your situation and problems based on the minimal information you provide.

– Cindy Meister
Nov 13 '18 at 19:53





Please describe in the issues in detail. We can't possibly reproduce your situation and problems based on the minimal information you provide.

– Cindy Meister
Nov 13 '18 at 19:53













Unless you've added a reference to the Word object library, Excel isn't going to know what the value of wdFindContinue is. You'll need to either add the reference, or define that constant. And use Option Explicit so you always know about this type of problem.

– Tim Williams
Nov 13 '18 at 20:11







Unless you've added a reference to the Word object library, Excel isn't going to know what the value of wdFindContinue is. You'll need to either add the reference, or define that constant. And use Option Explicit so you always know about this type of problem.

– Tim Williams
Nov 13 '18 at 20:11















I think you have your column and row indexes transposed. It should be N(j,1) and N(j,2).

– Freeflow
Nov 13 '18 at 20:29





I think you have your column and row indexes transposed. It should be N(j,1) and N(j,2).

– Freeflow
Nov 13 '18 at 20:29













Once I included Option Explicit I received an error on .Wrap=wdFindContinue. I do not understand how to define this in excel, would you please explain.

– JHEngr
Nov 13 '18 at 21:32





Once I included Option Explicit I received an error on .Wrap=wdFindContinue. I do not understand how to define this in excel, would you please explain.

– JHEngr
Nov 13 '18 at 21:32












2 Answers
2






active

oldest

votes


















0














The "Compile error: Variable not defined" problem is due to your use of late binding with a named Word constant. Either change



.Wrap = wdFindContinue


to:



.Wrap =  1


or insert:



Const wdFindContinue as Long = 1


at the top of your code.



In any event, as it stands, your existing code won't replace anything. Instead of:



.Execute


you need either:



.Execute 2


or:



.Execute wdReplaceAll


(to replace all instances) or:



.Execute 1


or:



.Execute wdReplaceOne


(to replace only the first instance)



As with wdFindContinue, you'd need to supplement wdReplaceAll or wdReplaceOne with:



Const wdReplaceAll as Long = 2


or:



Const wdReplaceOne as Long = 1


as appropriate.






share|improve this answer
























  • I made the adjustment and it runs without any errors. However it does not seem to find and replace any text. I tried adjusted the code to test it by simply entering in what to find and what to replace it with "Text" and "Changed" and added these words to the word document. It can't seem to find them and change them. I changed .Wrap=1 and .Execute 2

    – JHEngr
    Nov 14 '18 at 13:52













  • (comment removed)

    – JHEngr
    Nov 14 '18 at 13:53













  • How about posting your modified code so we can see what you're actually using?

    – macropod
    Nov 14 '18 at 21:23











  • Updated in the original post.

    – JHEngr
    Nov 19 '18 at 14:15











  • Since you don't save the changes before quitting, you're unlikely to ever see what the find/replace did.

    – macropod
    Nov 19 '18 at 20:40



















0














I was able to get the code to function. The code searches a specified Microsoft Word Document from Excel using a list of find replace strings from the excel sheet (B4:B5004 (find), C4:C5005 (replace)) stored in an array in the code.



To get the code to function I had to include the Microsoft Word 16 Object Library.



From the excel VBA editor window.
Tools > References then ensure Microsoft Word 16.0 Object Library is checked.



Option Explicit

Sub SearchReplace()

Dim WordApp As Object, WordDoc As Object, N As Variant, i As Integer, j As Integer
i = Range("C2").Value 'pulls length of list from an excel function located in cell C2 =COUNTIF(B4:B5005,"*")
N = Range("B4:C" & CStr(i + 3)).Value
Set WordApp = CreateObject(Class:="Word.Application")
Set WordDoc = WordApp.Documents.Open("C:WordTest.docm")
WordDoc.Visible = True
For j = 1 To i
With WordApp
With WordDoc.Content.Find
.Text = N(j, 1)
.Replacement.Text = N(j, 2)
.Wrap = wdFindContinue
.MatchWholeWord = True
.Execute Replace:=wdReplaceAll
End With
End With
Next j
WordApp.ActiveDocument.Save
WordApp.ActiveDocument.Close
WordApp.Quit

Set WordApp = Nothing
Set WordDoc = Nothing
End Sub





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287762%2fvba-search-replace-text-in-word-document-from-excel%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    The "Compile error: Variable not defined" problem is due to your use of late binding with a named Word constant. Either change



    .Wrap = wdFindContinue


    to:



    .Wrap =  1


    or insert:



    Const wdFindContinue as Long = 1


    at the top of your code.



    In any event, as it stands, your existing code won't replace anything. Instead of:



    .Execute


    you need either:



    .Execute 2


    or:



    .Execute wdReplaceAll


    (to replace all instances) or:



    .Execute 1


    or:



    .Execute wdReplaceOne


    (to replace only the first instance)



    As with wdFindContinue, you'd need to supplement wdReplaceAll or wdReplaceOne with:



    Const wdReplaceAll as Long = 2


    or:



    Const wdReplaceOne as Long = 1


    as appropriate.






    share|improve this answer
























    • I made the adjustment and it runs without any errors. However it does not seem to find and replace any text. I tried adjusted the code to test it by simply entering in what to find and what to replace it with "Text" and "Changed" and added these words to the word document. It can't seem to find them and change them. I changed .Wrap=1 and .Execute 2

      – JHEngr
      Nov 14 '18 at 13:52













    • (comment removed)

      – JHEngr
      Nov 14 '18 at 13:53













    • How about posting your modified code so we can see what you're actually using?

      – macropod
      Nov 14 '18 at 21:23











    • Updated in the original post.

      – JHEngr
      Nov 19 '18 at 14:15











    • Since you don't save the changes before quitting, you're unlikely to ever see what the find/replace did.

      – macropod
      Nov 19 '18 at 20:40
















    0














    The "Compile error: Variable not defined" problem is due to your use of late binding with a named Word constant. Either change



    .Wrap = wdFindContinue


    to:



    .Wrap =  1


    or insert:



    Const wdFindContinue as Long = 1


    at the top of your code.



    In any event, as it stands, your existing code won't replace anything. Instead of:



    .Execute


    you need either:



    .Execute 2


    or:



    .Execute wdReplaceAll


    (to replace all instances) or:



    .Execute 1


    or:



    .Execute wdReplaceOne


    (to replace only the first instance)



    As with wdFindContinue, you'd need to supplement wdReplaceAll or wdReplaceOne with:



    Const wdReplaceAll as Long = 2


    or:



    Const wdReplaceOne as Long = 1


    as appropriate.






    share|improve this answer
























    • I made the adjustment and it runs without any errors. However it does not seem to find and replace any text. I tried adjusted the code to test it by simply entering in what to find and what to replace it with "Text" and "Changed" and added these words to the word document. It can't seem to find them and change them. I changed .Wrap=1 and .Execute 2

      – JHEngr
      Nov 14 '18 at 13:52













    • (comment removed)

      – JHEngr
      Nov 14 '18 at 13:53













    • How about posting your modified code so we can see what you're actually using?

      – macropod
      Nov 14 '18 at 21:23











    • Updated in the original post.

      – JHEngr
      Nov 19 '18 at 14:15











    • Since you don't save the changes before quitting, you're unlikely to ever see what the find/replace did.

      – macropod
      Nov 19 '18 at 20:40














    0












    0








    0







    The "Compile error: Variable not defined" problem is due to your use of late binding with a named Word constant. Either change



    .Wrap = wdFindContinue


    to:



    .Wrap =  1


    or insert:



    Const wdFindContinue as Long = 1


    at the top of your code.



    In any event, as it stands, your existing code won't replace anything. Instead of:



    .Execute


    you need either:



    .Execute 2


    or:



    .Execute wdReplaceAll


    (to replace all instances) or:



    .Execute 1


    or:



    .Execute wdReplaceOne


    (to replace only the first instance)



    As with wdFindContinue, you'd need to supplement wdReplaceAll or wdReplaceOne with:



    Const wdReplaceAll as Long = 2


    or:



    Const wdReplaceOne as Long = 1


    as appropriate.






    share|improve this answer













    The "Compile error: Variable not defined" problem is due to your use of late binding with a named Word constant. Either change



    .Wrap = wdFindContinue


    to:



    .Wrap =  1


    or insert:



    Const wdFindContinue as Long = 1


    at the top of your code.



    In any event, as it stands, your existing code won't replace anything. Instead of:



    .Execute


    you need either:



    .Execute 2


    or:



    .Execute wdReplaceAll


    (to replace all instances) or:



    .Execute 1


    or:



    .Execute wdReplaceOne


    (to replace only the first instance)



    As with wdFindContinue, you'd need to supplement wdReplaceAll or wdReplaceOne with:



    Const wdReplaceAll as Long = 2


    or:



    Const wdReplaceOne as Long = 1


    as appropriate.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 13 '18 at 22:03









    macropodmacropod

    2,440239




    2,440239













    • I made the adjustment and it runs without any errors. However it does not seem to find and replace any text. I tried adjusted the code to test it by simply entering in what to find and what to replace it with "Text" and "Changed" and added these words to the word document. It can't seem to find them and change them. I changed .Wrap=1 and .Execute 2

      – JHEngr
      Nov 14 '18 at 13:52













    • (comment removed)

      – JHEngr
      Nov 14 '18 at 13:53













    • How about posting your modified code so we can see what you're actually using?

      – macropod
      Nov 14 '18 at 21:23











    • Updated in the original post.

      – JHEngr
      Nov 19 '18 at 14:15











    • Since you don't save the changes before quitting, you're unlikely to ever see what the find/replace did.

      – macropod
      Nov 19 '18 at 20:40



















    • I made the adjustment and it runs without any errors. However it does not seem to find and replace any text. I tried adjusted the code to test it by simply entering in what to find and what to replace it with "Text" and "Changed" and added these words to the word document. It can't seem to find them and change them. I changed .Wrap=1 and .Execute 2

      – JHEngr
      Nov 14 '18 at 13:52













    • (comment removed)

      – JHEngr
      Nov 14 '18 at 13:53













    • How about posting your modified code so we can see what you're actually using?

      – macropod
      Nov 14 '18 at 21:23











    • Updated in the original post.

      – JHEngr
      Nov 19 '18 at 14:15











    • Since you don't save the changes before quitting, you're unlikely to ever see what the find/replace did.

      – macropod
      Nov 19 '18 at 20:40

















    I made the adjustment and it runs without any errors. However it does not seem to find and replace any text. I tried adjusted the code to test it by simply entering in what to find and what to replace it with "Text" and "Changed" and added these words to the word document. It can't seem to find them and change them. I changed .Wrap=1 and .Execute 2

    – JHEngr
    Nov 14 '18 at 13:52







    I made the adjustment and it runs without any errors. However it does not seem to find and replace any text. I tried adjusted the code to test it by simply entering in what to find and what to replace it with "Text" and "Changed" and added these words to the word document. It can't seem to find them and change them. I changed .Wrap=1 and .Execute 2

    – JHEngr
    Nov 14 '18 at 13:52















    (comment removed)

    – JHEngr
    Nov 14 '18 at 13:53







    (comment removed)

    – JHEngr
    Nov 14 '18 at 13:53















    How about posting your modified code so we can see what you're actually using?

    – macropod
    Nov 14 '18 at 21:23





    How about posting your modified code so we can see what you're actually using?

    – macropod
    Nov 14 '18 at 21:23













    Updated in the original post.

    – JHEngr
    Nov 19 '18 at 14:15





    Updated in the original post.

    – JHEngr
    Nov 19 '18 at 14:15













    Since you don't save the changes before quitting, you're unlikely to ever see what the find/replace did.

    – macropod
    Nov 19 '18 at 20:40





    Since you don't save the changes before quitting, you're unlikely to ever see what the find/replace did.

    – macropod
    Nov 19 '18 at 20:40













    0














    I was able to get the code to function. The code searches a specified Microsoft Word Document from Excel using a list of find replace strings from the excel sheet (B4:B5004 (find), C4:C5005 (replace)) stored in an array in the code.



    To get the code to function I had to include the Microsoft Word 16 Object Library.



    From the excel VBA editor window.
    Tools > References then ensure Microsoft Word 16.0 Object Library is checked.



    Option Explicit

    Sub SearchReplace()

    Dim WordApp As Object, WordDoc As Object, N As Variant, i As Integer, j As Integer
    i = Range("C2").Value 'pulls length of list from an excel function located in cell C2 =COUNTIF(B4:B5005,"*")
    N = Range("B4:C" & CStr(i + 3)).Value
    Set WordApp = CreateObject(Class:="Word.Application")
    Set WordDoc = WordApp.Documents.Open("C:WordTest.docm")
    WordDoc.Visible = True
    For j = 1 To i
    With WordApp
    With WordDoc.Content.Find
    .Text = N(j, 1)
    .Replacement.Text = N(j, 2)
    .Wrap = wdFindContinue
    .MatchWholeWord = True
    .Execute Replace:=wdReplaceAll
    End With
    End With
    Next j
    WordApp.ActiveDocument.Save
    WordApp.ActiveDocument.Close
    WordApp.Quit

    Set WordApp = Nothing
    Set WordDoc = Nothing
    End Sub





    share|improve this answer




























      0














      I was able to get the code to function. The code searches a specified Microsoft Word Document from Excel using a list of find replace strings from the excel sheet (B4:B5004 (find), C4:C5005 (replace)) stored in an array in the code.



      To get the code to function I had to include the Microsoft Word 16 Object Library.



      From the excel VBA editor window.
      Tools > References then ensure Microsoft Word 16.0 Object Library is checked.



      Option Explicit

      Sub SearchReplace()

      Dim WordApp As Object, WordDoc As Object, N As Variant, i As Integer, j As Integer
      i = Range("C2").Value 'pulls length of list from an excel function located in cell C2 =COUNTIF(B4:B5005,"*")
      N = Range("B4:C" & CStr(i + 3)).Value
      Set WordApp = CreateObject(Class:="Word.Application")
      Set WordDoc = WordApp.Documents.Open("C:WordTest.docm")
      WordDoc.Visible = True
      For j = 1 To i
      With WordApp
      With WordDoc.Content.Find
      .Text = N(j, 1)
      .Replacement.Text = N(j, 2)
      .Wrap = wdFindContinue
      .MatchWholeWord = True
      .Execute Replace:=wdReplaceAll
      End With
      End With
      Next j
      WordApp.ActiveDocument.Save
      WordApp.ActiveDocument.Close
      WordApp.Quit

      Set WordApp = Nothing
      Set WordDoc = Nothing
      End Sub





      share|improve this answer


























        0












        0








        0







        I was able to get the code to function. The code searches a specified Microsoft Word Document from Excel using a list of find replace strings from the excel sheet (B4:B5004 (find), C4:C5005 (replace)) stored in an array in the code.



        To get the code to function I had to include the Microsoft Word 16 Object Library.



        From the excel VBA editor window.
        Tools > References then ensure Microsoft Word 16.0 Object Library is checked.



        Option Explicit

        Sub SearchReplace()

        Dim WordApp As Object, WordDoc As Object, N As Variant, i As Integer, j As Integer
        i = Range("C2").Value 'pulls length of list from an excel function located in cell C2 =COUNTIF(B4:B5005,"*")
        N = Range("B4:C" & CStr(i + 3)).Value
        Set WordApp = CreateObject(Class:="Word.Application")
        Set WordDoc = WordApp.Documents.Open("C:WordTest.docm")
        WordDoc.Visible = True
        For j = 1 To i
        With WordApp
        With WordDoc.Content.Find
        .Text = N(j, 1)
        .Replacement.Text = N(j, 2)
        .Wrap = wdFindContinue
        .MatchWholeWord = True
        .Execute Replace:=wdReplaceAll
        End With
        End With
        Next j
        WordApp.ActiveDocument.Save
        WordApp.ActiveDocument.Close
        WordApp.Quit

        Set WordApp = Nothing
        Set WordDoc = Nothing
        End Sub





        share|improve this answer













        I was able to get the code to function. The code searches a specified Microsoft Word Document from Excel using a list of find replace strings from the excel sheet (B4:B5004 (find), C4:C5005 (replace)) stored in an array in the code.



        To get the code to function I had to include the Microsoft Word 16 Object Library.



        From the excel VBA editor window.
        Tools > References then ensure Microsoft Word 16.0 Object Library is checked.



        Option Explicit

        Sub SearchReplace()

        Dim WordApp As Object, WordDoc As Object, N As Variant, i As Integer, j As Integer
        i = Range("C2").Value 'pulls length of list from an excel function located in cell C2 =COUNTIF(B4:B5005,"*")
        N = Range("B4:C" & CStr(i + 3)).Value
        Set WordApp = CreateObject(Class:="Word.Application")
        Set WordDoc = WordApp.Documents.Open("C:WordTest.docm")
        WordDoc.Visible = True
        For j = 1 To i
        With WordApp
        With WordDoc.Content.Find
        .Text = N(j, 1)
        .Replacement.Text = N(j, 2)
        .Wrap = wdFindContinue
        .MatchWholeWord = True
        .Execute Replace:=wdReplaceAll
        End With
        End With
        Next j
        WordApp.ActiveDocument.Save
        WordApp.ActiveDocument.Close
        WordApp.Quit

        Set WordApp = Nothing
        Set WordDoc = Nothing
        End Sub






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 26 '18 at 14:42









        JHEngrJHEngr

        11




        11






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287762%2fvba-search-replace-text-in-word-document-from-excel%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Error while running script in elastic search , gateway timeout

            Adding quotations to stringified JSON object values