How to Find text strings (in Word) using a two dimensional array





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have a two dimensional array comprised of "trouble" words and phrases in the first dimension and the comments I frequently make in the second dimension. I seem to be lost at how to select the text that matches the first dimension and add the comment using the text from the second dimension. Any ideas?



Sub findtrouble()
Dim i As Integer
Dim j As Integer
Dim oRng As Word.Range



Dim MyArray(1, 4) As String
MyArray(0, 0) = "Trouble0"
MyArray(0, 1) = "Trouble1"
MyArray(0, 2) = "Trouble2"
MyArray(0, 3) = "Trouble3"

MyArray(1, 0) = "Comment0"
MyArray(1, 1) = "Comment1"
MyArray(1, 2) = "Comment2"
MyArray(1, 3) = "Comment3"



For j = 0 To 4
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearAllFuzzyOptions
.ClearFormatting
.Text = MyArray(0, j)
While .Execute
oRng.Select
ActiveDocument.Comments.Add oRng, MyArray(1, j)
Wend
End With
Debug.Print "Find: " & MyArray(0, j) & " add cmt box w/ "; MyArray(1, j)
Next j

End Sub









share|improve this question




















  • 2





    You need to read up on Scripting.Dictionary. A scripting.dictionary will replace MyArray. Dictionaries are a data structure that manages key:value pairs. To get your comment you would use something like my_comment=my_lookup.item(found_text). (or in your case (ActiveDocument.Comments.Add my_lookup.item(.Range.Text) ). The oRng.Select you have after the execute is not needed. The found range can be accessed using just .Range.

    – Freeflow
    Nov 23 '18 at 15:32













  • Freeflow's comment aside, what problem is the code you show us having? At a glance, it looks like it should work.

    – Cindy Meister
    Nov 23 '18 at 15:58











  • Cindy - I got it - I had the review settings to hide comments for some reason..it was working all along! Thanks! I tested the scripting method as well, it works too. Will look more into using scripting dictionaries...they are not something I've ever used before.

    – kroy2008
    Nov 23 '18 at 16:49


















0















I have a two dimensional array comprised of "trouble" words and phrases in the first dimension and the comments I frequently make in the second dimension. I seem to be lost at how to select the text that matches the first dimension and add the comment using the text from the second dimension. Any ideas?



Sub findtrouble()
Dim i As Integer
Dim j As Integer
Dim oRng As Word.Range



Dim MyArray(1, 4) As String
MyArray(0, 0) = "Trouble0"
MyArray(0, 1) = "Trouble1"
MyArray(0, 2) = "Trouble2"
MyArray(0, 3) = "Trouble3"

MyArray(1, 0) = "Comment0"
MyArray(1, 1) = "Comment1"
MyArray(1, 2) = "Comment2"
MyArray(1, 3) = "Comment3"



For j = 0 To 4
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearAllFuzzyOptions
.ClearFormatting
.Text = MyArray(0, j)
While .Execute
oRng.Select
ActiveDocument.Comments.Add oRng, MyArray(1, j)
Wend
End With
Debug.Print "Find: " & MyArray(0, j) & " add cmt box w/ "; MyArray(1, j)
Next j

End Sub









share|improve this question




















  • 2





    You need to read up on Scripting.Dictionary. A scripting.dictionary will replace MyArray. Dictionaries are a data structure that manages key:value pairs. To get your comment you would use something like my_comment=my_lookup.item(found_text). (or in your case (ActiveDocument.Comments.Add my_lookup.item(.Range.Text) ). The oRng.Select you have after the execute is not needed. The found range can be accessed using just .Range.

    – Freeflow
    Nov 23 '18 at 15:32













  • Freeflow's comment aside, what problem is the code you show us having? At a glance, it looks like it should work.

    – Cindy Meister
    Nov 23 '18 at 15:58











  • Cindy - I got it - I had the review settings to hide comments for some reason..it was working all along! Thanks! I tested the scripting method as well, it works too. Will look more into using scripting dictionaries...they are not something I've ever used before.

    – kroy2008
    Nov 23 '18 at 16:49














0












0








0








I have a two dimensional array comprised of "trouble" words and phrases in the first dimension and the comments I frequently make in the second dimension. I seem to be lost at how to select the text that matches the first dimension and add the comment using the text from the second dimension. Any ideas?



Sub findtrouble()
Dim i As Integer
Dim j As Integer
Dim oRng As Word.Range



Dim MyArray(1, 4) As String
MyArray(0, 0) = "Trouble0"
MyArray(0, 1) = "Trouble1"
MyArray(0, 2) = "Trouble2"
MyArray(0, 3) = "Trouble3"

MyArray(1, 0) = "Comment0"
MyArray(1, 1) = "Comment1"
MyArray(1, 2) = "Comment2"
MyArray(1, 3) = "Comment3"



For j = 0 To 4
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearAllFuzzyOptions
.ClearFormatting
.Text = MyArray(0, j)
While .Execute
oRng.Select
ActiveDocument.Comments.Add oRng, MyArray(1, j)
Wend
End With
Debug.Print "Find: " & MyArray(0, j) & " add cmt box w/ "; MyArray(1, j)
Next j

End Sub









share|improve this question
















I have a two dimensional array comprised of "trouble" words and phrases in the first dimension and the comments I frequently make in the second dimension. I seem to be lost at how to select the text that matches the first dimension and add the comment using the text from the second dimension. Any ideas?



Sub findtrouble()
Dim i As Integer
Dim j As Integer
Dim oRng As Word.Range



Dim MyArray(1, 4) As String
MyArray(0, 0) = "Trouble0"
MyArray(0, 1) = "Trouble1"
MyArray(0, 2) = "Trouble2"
MyArray(0, 3) = "Trouble3"

MyArray(1, 0) = "Comment0"
MyArray(1, 1) = "Comment1"
MyArray(1, 2) = "Comment2"
MyArray(1, 3) = "Comment3"



For j = 0 To 4
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearAllFuzzyOptions
.ClearFormatting
.Text = MyArray(0, j)
While .Execute
oRng.Select
ActiveDocument.Comments.Add oRng, MyArray(1, j)
Wend
End With
Debug.Print "Find: " & MyArray(0, j) & " add cmt box w/ "; MyArray(1, j)
Next j

End Sub






arrays vba ms-word






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 15:56









Cindy Meister

16k102537




16k102537










asked Nov 23 '18 at 15:28









kroy2008kroy2008

788




788








  • 2





    You need to read up on Scripting.Dictionary. A scripting.dictionary will replace MyArray. Dictionaries are a data structure that manages key:value pairs. To get your comment you would use something like my_comment=my_lookup.item(found_text). (or in your case (ActiveDocument.Comments.Add my_lookup.item(.Range.Text) ). The oRng.Select you have after the execute is not needed. The found range can be accessed using just .Range.

    – Freeflow
    Nov 23 '18 at 15:32













  • Freeflow's comment aside, what problem is the code you show us having? At a glance, it looks like it should work.

    – Cindy Meister
    Nov 23 '18 at 15:58











  • Cindy - I got it - I had the review settings to hide comments for some reason..it was working all along! Thanks! I tested the scripting method as well, it works too. Will look more into using scripting dictionaries...they are not something I've ever used before.

    – kroy2008
    Nov 23 '18 at 16:49














  • 2





    You need to read up on Scripting.Dictionary. A scripting.dictionary will replace MyArray. Dictionaries are a data structure that manages key:value pairs. To get your comment you would use something like my_comment=my_lookup.item(found_text). (or in your case (ActiveDocument.Comments.Add my_lookup.item(.Range.Text) ). The oRng.Select you have after the execute is not needed. The found range can be accessed using just .Range.

    – Freeflow
    Nov 23 '18 at 15:32













  • Freeflow's comment aside, what problem is the code you show us having? At a glance, it looks like it should work.

    – Cindy Meister
    Nov 23 '18 at 15:58











  • Cindy - I got it - I had the review settings to hide comments for some reason..it was working all along! Thanks! I tested the scripting method as well, it works too. Will look more into using scripting dictionaries...they are not something I've ever used before.

    – kroy2008
    Nov 23 '18 at 16:49








2




2





You need to read up on Scripting.Dictionary. A scripting.dictionary will replace MyArray. Dictionaries are a data structure that manages key:value pairs. To get your comment you would use something like my_comment=my_lookup.item(found_text). (or in your case (ActiveDocument.Comments.Add my_lookup.item(.Range.Text) ). The oRng.Select you have after the execute is not needed. The found range can be accessed using just .Range.

– Freeflow
Nov 23 '18 at 15:32







You need to read up on Scripting.Dictionary. A scripting.dictionary will replace MyArray. Dictionaries are a data structure that manages key:value pairs. To get your comment you would use something like my_comment=my_lookup.item(found_text). (or in your case (ActiveDocument.Comments.Add my_lookup.item(.Range.Text) ). The oRng.Select you have after the execute is not needed. The found range can be accessed using just .Range.

– Freeflow
Nov 23 '18 at 15:32















Freeflow's comment aside, what problem is the code you show us having? At a glance, it looks like it should work.

– Cindy Meister
Nov 23 '18 at 15:58





Freeflow's comment aside, what problem is the code you show us having? At a glance, it looks like it should work.

– Cindy Meister
Nov 23 '18 at 15:58













Cindy - I got it - I had the review settings to hide comments for some reason..it was working all along! Thanks! I tested the scripting method as well, it works too. Will look more into using scripting dictionaries...they are not something I've ever used before.

– kroy2008
Nov 23 '18 at 16:49





Cindy - I got it - I had the review settings to hide comments for some reason..it was working all along! Thanks! I tested the scripting method as well, it works too. Will look more into using scripting dictionaries...they are not something I've ever used before.

– kroy2008
Nov 23 '18 at 16:49












2 Answers
2






active

oldest

votes


















1














The code in the question does insert one comment for me, but that's all. It's because oRng isn't being reset. Compare the code in the question to that below.



In this code, after Find.Execute is successful and the comment added the range is collapsed to it endpoint (after the found term) then the end extended to the end of the document. In this way, the next time the term is searched it looks only in what follows the first term.



It's also important when looping in Find to set the Find.Wrap to wdFindStop to avoid going into an "infinite loop" (so that Find doesn't start again at the top of the document).



Sub findtrouble()
Dim i As Integer
Dim j As Integer
Dim oRng As Word.Range

Dim MyArray(1, 4) As String
MyArray(0, 0) = "Trouble0"
MyArray(0, 1) = "Trouble1"
MyArray(0, 2) = "Trouble2"
MyArray(0, 3) = "Trouble3"

MyArray(1, 0) = "Comment0"
MyArray(1, 1) = "Comment1"
MyArray(1, 2) = "Comment2"
MyArray(1, 3) = "Comment3"

For j = 0 To 4
Set oRng = ActiveDocument.Content
With oRng.Find
.ClearAllFuzzyOptions
.ClearFormatting
.text = MyArray(0, j)
.wrap = wdFindStop
While .Execute
oRng.Select
ActiveDocument.Comments.Add oRng, MyArray(1, j)
oRng.Collapse wdCollapseEnd
oRng.End = ActiveDocument.content.End
Wend
End With
Debug.Print "Find: " & MyArray(0, j) & " add cmt box w/ "; MyArray(1, j)
Next j

End Sub





share|improve this answer
























  • Cindy - for some reason, all the comments (Comment0, Comment1, Comment2, etc) get added to the same (the first) comment box. Empty comment boxes get added to the other trouble words, but the associated comment ends up placed in the first comment in the document. Any ideas?

    – kroy2008
    Nov 26 '18 at 14:38





















-1














As per @Cindy Meisters' comment the posted code does work (even with the indexing error in the for loop). The code below is the same rewritten to use a scripting.dictionary



Sub testfindtrouble()
findtrouble ActiveDocument.Range
End Sub
Sub findtrouble(this_range As Word.Range)
Dim my_lookup As scripting.Dictionary
Dim my_troubles As Variant
Dim my_trouble As Variant
Dim my_range As Word.Range

' see https://stackoverflow.com/questions/53317548/how-to-delete-a-section-using-excel-vba-to-create-a-word-document/53322166?noredirect=1#comment93559248_53322166
Set my_lookup = New scripting.Dictionary

With my_lookup

.Add key:="Trouble0", item:="Comment0"
.Add key:="Trouble1", item:="Comment1"
.Add key:="Trouble2", item:="Comment2"
.Add key:="Trouble3", item:="Comment3"

End With

my_troubles = my_lookup.Keys

' Avoid the off by 1 error (j=0 to 4 is 5 items not the 4 you declared in the array
For Each my_trouble In my_troubles

Set my_range = this_range.Duplicate

With my_range

With .Find

.ClearAllFuzzyOptions
.ClearFormatting
.text = my_trouble
.Execute

End With

Do While .Find.Found

Debug.Print "Find: " & my_trouble & " add cmt box w/ "; my_lookup.item(my_trouble)
.Comments.Add .Duplicate, my_lookup.item(my_trouble)
.Collapse Direction:=wdCollapseEnd
.Move unit:=wdCharacter, Count:=1
.Find.Execute

Loop

End With

Next

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%2f53449326%2fhow-to-find-text-strings-in-word-using-a-two-dimensional-array%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









    1














    The code in the question does insert one comment for me, but that's all. It's because oRng isn't being reset. Compare the code in the question to that below.



    In this code, after Find.Execute is successful and the comment added the range is collapsed to it endpoint (after the found term) then the end extended to the end of the document. In this way, the next time the term is searched it looks only in what follows the first term.



    It's also important when looping in Find to set the Find.Wrap to wdFindStop to avoid going into an "infinite loop" (so that Find doesn't start again at the top of the document).



    Sub findtrouble()
    Dim i As Integer
    Dim j As Integer
    Dim oRng As Word.Range

    Dim MyArray(1, 4) As String
    MyArray(0, 0) = "Trouble0"
    MyArray(0, 1) = "Trouble1"
    MyArray(0, 2) = "Trouble2"
    MyArray(0, 3) = "Trouble3"

    MyArray(1, 0) = "Comment0"
    MyArray(1, 1) = "Comment1"
    MyArray(1, 2) = "Comment2"
    MyArray(1, 3) = "Comment3"

    For j = 0 To 4
    Set oRng = ActiveDocument.Content
    With oRng.Find
    .ClearAllFuzzyOptions
    .ClearFormatting
    .text = MyArray(0, j)
    .wrap = wdFindStop
    While .Execute
    oRng.Select
    ActiveDocument.Comments.Add oRng, MyArray(1, j)
    oRng.Collapse wdCollapseEnd
    oRng.End = ActiveDocument.content.End
    Wend
    End With
    Debug.Print "Find: " & MyArray(0, j) & " add cmt box w/ "; MyArray(1, j)
    Next j

    End Sub





    share|improve this answer
























    • Cindy - for some reason, all the comments (Comment0, Comment1, Comment2, etc) get added to the same (the first) comment box. Empty comment boxes get added to the other trouble words, but the associated comment ends up placed in the first comment in the document. Any ideas?

      – kroy2008
      Nov 26 '18 at 14:38


















    1














    The code in the question does insert one comment for me, but that's all. It's because oRng isn't being reset. Compare the code in the question to that below.



    In this code, after Find.Execute is successful and the comment added the range is collapsed to it endpoint (after the found term) then the end extended to the end of the document. In this way, the next time the term is searched it looks only in what follows the first term.



    It's also important when looping in Find to set the Find.Wrap to wdFindStop to avoid going into an "infinite loop" (so that Find doesn't start again at the top of the document).



    Sub findtrouble()
    Dim i As Integer
    Dim j As Integer
    Dim oRng As Word.Range

    Dim MyArray(1, 4) As String
    MyArray(0, 0) = "Trouble0"
    MyArray(0, 1) = "Trouble1"
    MyArray(0, 2) = "Trouble2"
    MyArray(0, 3) = "Trouble3"

    MyArray(1, 0) = "Comment0"
    MyArray(1, 1) = "Comment1"
    MyArray(1, 2) = "Comment2"
    MyArray(1, 3) = "Comment3"

    For j = 0 To 4
    Set oRng = ActiveDocument.Content
    With oRng.Find
    .ClearAllFuzzyOptions
    .ClearFormatting
    .text = MyArray(0, j)
    .wrap = wdFindStop
    While .Execute
    oRng.Select
    ActiveDocument.Comments.Add oRng, MyArray(1, j)
    oRng.Collapse wdCollapseEnd
    oRng.End = ActiveDocument.content.End
    Wend
    End With
    Debug.Print "Find: " & MyArray(0, j) & " add cmt box w/ "; MyArray(1, j)
    Next j

    End Sub





    share|improve this answer
























    • Cindy - for some reason, all the comments (Comment0, Comment1, Comment2, etc) get added to the same (the first) comment box. Empty comment boxes get added to the other trouble words, but the associated comment ends up placed in the first comment in the document. Any ideas?

      – kroy2008
      Nov 26 '18 at 14:38
















    1












    1








    1







    The code in the question does insert one comment for me, but that's all. It's because oRng isn't being reset. Compare the code in the question to that below.



    In this code, after Find.Execute is successful and the comment added the range is collapsed to it endpoint (after the found term) then the end extended to the end of the document. In this way, the next time the term is searched it looks only in what follows the first term.



    It's also important when looping in Find to set the Find.Wrap to wdFindStop to avoid going into an "infinite loop" (so that Find doesn't start again at the top of the document).



    Sub findtrouble()
    Dim i As Integer
    Dim j As Integer
    Dim oRng As Word.Range

    Dim MyArray(1, 4) As String
    MyArray(0, 0) = "Trouble0"
    MyArray(0, 1) = "Trouble1"
    MyArray(0, 2) = "Trouble2"
    MyArray(0, 3) = "Trouble3"

    MyArray(1, 0) = "Comment0"
    MyArray(1, 1) = "Comment1"
    MyArray(1, 2) = "Comment2"
    MyArray(1, 3) = "Comment3"

    For j = 0 To 4
    Set oRng = ActiveDocument.Content
    With oRng.Find
    .ClearAllFuzzyOptions
    .ClearFormatting
    .text = MyArray(0, j)
    .wrap = wdFindStop
    While .Execute
    oRng.Select
    ActiveDocument.Comments.Add oRng, MyArray(1, j)
    oRng.Collapse wdCollapseEnd
    oRng.End = ActiveDocument.content.End
    Wend
    End With
    Debug.Print "Find: " & MyArray(0, j) & " add cmt box w/ "; MyArray(1, j)
    Next j

    End Sub





    share|improve this answer













    The code in the question does insert one comment for me, but that's all. It's because oRng isn't being reset. Compare the code in the question to that below.



    In this code, after Find.Execute is successful and the comment added the range is collapsed to it endpoint (after the found term) then the end extended to the end of the document. In this way, the next time the term is searched it looks only in what follows the first term.



    It's also important when looping in Find to set the Find.Wrap to wdFindStop to avoid going into an "infinite loop" (so that Find doesn't start again at the top of the document).



    Sub findtrouble()
    Dim i As Integer
    Dim j As Integer
    Dim oRng As Word.Range

    Dim MyArray(1, 4) As String
    MyArray(0, 0) = "Trouble0"
    MyArray(0, 1) = "Trouble1"
    MyArray(0, 2) = "Trouble2"
    MyArray(0, 3) = "Trouble3"

    MyArray(1, 0) = "Comment0"
    MyArray(1, 1) = "Comment1"
    MyArray(1, 2) = "Comment2"
    MyArray(1, 3) = "Comment3"

    For j = 0 To 4
    Set oRng = ActiveDocument.Content
    With oRng.Find
    .ClearAllFuzzyOptions
    .ClearFormatting
    .text = MyArray(0, j)
    .wrap = wdFindStop
    While .Execute
    oRng.Select
    ActiveDocument.Comments.Add oRng, MyArray(1, j)
    oRng.Collapse wdCollapseEnd
    oRng.End = ActiveDocument.content.End
    Wend
    End With
    Debug.Print "Find: " & MyArray(0, j) & " add cmt box w/ "; MyArray(1, j)
    Next j

    End Sub






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 23 '18 at 16:50









    Cindy MeisterCindy Meister

    16k102537




    16k102537













    • Cindy - for some reason, all the comments (Comment0, Comment1, Comment2, etc) get added to the same (the first) comment box. Empty comment boxes get added to the other trouble words, but the associated comment ends up placed in the first comment in the document. Any ideas?

      – kroy2008
      Nov 26 '18 at 14:38





















    • Cindy - for some reason, all the comments (Comment0, Comment1, Comment2, etc) get added to the same (the first) comment box. Empty comment boxes get added to the other trouble words, but the associated comment ends up placed in the first comment in the document. Any ideas?

      – kroy2008
      Nov 26 '18 at 14:38



















    Cindy - for some reason, all the comments (Comment0, Comment1, Comment2, etc) get added to the same (the first) comment box. Empty comment boxes get added to the other trouble words, but the associated comment ends up placed in the first comment in the document. Any ideas?

    – kroy2008
    Nov 26 '18 at 14:38







    Cindy - for some reason, all the comments (Comment0, Comment1, Comment2, etc) get added to the same (the first) comment box. Empty comment boxes get added to the other trouble words, but the associated comment ends up placed in the first comment in the document. Any ideas?

    – kroy2008
    Nov 26 '18 at 14:38















    -1














    As per @Cindy Meisters' comment the posted code does work (even with the indexing error in the for loop). The code below is the same rewritten to use a scripting.dictionary



    Sub testfindtrouble()
    findtrouble ActiveDocument.Range
    End Sub
    Sub findtrouble(this_range As Word.Range)
    Dim my_lookup As scripting.Dictionary
    Dim my_troubles As Variant
    Dim my_trouble As Variant
    Dim my_range As Word.Range

    ' see https://stackoverflow.com/questions/53317548/how-to-delete-a-section-using-excel-vba-to-create-a-word-document/53322166?noredirect=1#comment93559248_53322166
    Set my_lookup = New scripting.Dictionary

    With my_lookup

    .Add key:="Trouble0", item:="Comment0"
    .Add key:="Trouble1", item:="Comment1"
    .Add key:="Trouble2", item:="Comment2"
    .Add key:="Trouble3", item:="Comment3"

    End With

    my_troubles = my_lookup.Keys

    ' Avoid the off by 1 error (j=0 to 4 is 5 items not the 4 you declared in the array
    For Each my_trouble In my_troubles

    Set my_range = this_range.Duplicate

    With my_range

    With .Find

    .ClearAllFuzzyOptions
    .ClearFormatting
    .text = my_trouble
    .Execute

    End With

    Do While .Find.Found

    Debug.Print "Find: " & my_trouble & " add cmt box w/ "; my_lookup.item(my_trouble)
    .Comments.Add .Duplicate, my_lookup.item(my_trouble)
    .Collapse Direction:=wdCollapseEnd
    .Move unit:=wdCharacter, Count:=1
    .Find.Execute

    Loop

    End With

    Next

    End Sub





    share|improve this answer




























      -1














      As per @Cindy Meisters' comment the posted code does work (even with the indexing error in the for loop). The code below is the same rewritten to use a scripting.dictionary



      Sub testfindtrouble()
      findtrouble ActiveDocument.Range
      End Sub
      Sub findtrouble(this_range As Word.Range)
      Dim my_lookup As scripting.Dictionary
      Dim my_troubles As Variant
      Dim my_trouble As Variant
      Dim my_range As Word.Range

      ' see https://stackoverflow.com/questions/53317548/how-to-delete-a-section-using-excel-vba-to-create-a-word-document/53322166?noredirect=1#comment93559248_53322166
      Set my_lookup = New scripting.Dictionary

      With my_lookup

      .Add key:="Trouble0", item:="Comment0"
      .Add key:="Trouble1", item:="Comment1"
      .Add key:="Trouble2", item:="Comment2"
      .Add key:="Trouble3", item:="Comment3"

      End With

      my_troubles = my_lookup.Keys

      ' Avoid the off by 1 error (j=0 to 4 is 5 items not the 4 you declared in the array
      For Each my_trouble In my_troubles

      Set my_range = this_range.Duplicate

      With my_range

      With .Find

      .ClearAllFuzzyOptions
      .ClearFormatting
      .text = my_trouble
      .Execute

      End With

      Do While .Find.Found

      Debug.Print "Find: " & my_trouble & " add cmt box w/ "; my_lookup.item(my_trouble)
      .Comments.Add .Duplicate, my_lookup.item(my_trouble)
      .Collapse Direction:=wdCollapseEnd
      .Move unit:=wdCharacter, Count:=1
      .Find.Execute

      Loop

      End With

      Next

      End Sub





      share|improve this answer


























        -1












        -1








        -1







        As per @Cindy Meisters' comment the posted code does work (even with the indexing error in the for loop). The code below is the same rewritten to use a scripting.dictionary



        Sub testfindtrouble()
        findtrouble ActiveDocument.Range
        End Sub
        Sub findtrouble(this_range As Word.Range)
        Dim my_lookup As scripting.Dictionary
        Dim my_troubles As Variant
        Dim my_trouble As Variant
        Dim my_range As Word.Range

        ' see https://stackoverflow.com/questions/53317548/how-to-delete-a-section-using-excel-vba-to-create-a-word-document/53322166?noredirect=1#comment93559248_53322166
        Set my_lookup = New scripting.Dictionary

        With my_lookup

        .Add key:="Trouble0", item:="Comment0"
        .Add key:="Trouble1", item:="Comment1"
        .Add key:="Trouble2", item:="Comment2"
        .Add key:="Trouble3", item:="Comment3"

        End With

        my_troubles = my_lookup.Keys

        ' Avoid the off by 1 error (j=0 to 4 is 5 items not the 4 you declared in the array
        For Each my_trouble In my_troubles

        Set my_range = this_range.Duplicate

        With my_range

        With .Find

        .ClearAllFuzzyOptions
        .ClearFormatting
        .text = my_trouble
        .Execute

        End With

        Do While .Find.Found

        Debug.Print "Find: " & my_trouble & " add cmt box w/ "; my_lookup.item(my_trouble)
        .Comments.Add .Duplicate, my_lookup.item(my_trouble)
        .Collapse Direction:=wdCollapseEnd
        .Move unit:=wdCharacter, Count:=1
        .Find.Execute

        Loop

        End With

        Next

        End Sub





        share|improve this answer













        As per @Cindy Meisters' comment the posted code does work (even with the indexing error in the for loop). The code below is the same rewritten to use a scripting.dictionary



        Sub testfindtrouble()
        findtrouble ActiveDocument.Range
        End Sub
        Sub findtrouble(this_range As Word.Range)
        Dim my_lookup As scripting.Dictionary
        Dim my_troubles As Variant
        Dim my_trouble As Variant
        Dim my_range As Word.Range

        ' see https://stackoverflow.com/questions/53317548/how-to-delete-a-section-using-excel-vba-to-create-a-word-document/53322166?noredirect=1#comment93559248_53322166
        Set my_lookup = New scripting.Dictionary

        With my_lookup

        .Add key:="Trouble0", item:="Comment0"
        .Add key:="Trouble1", item:="Comment1"
        .Add key:="Trouble2", item:="Comment2"
        .Add key:="Trouble3", item:="Comment3"

        End With

        my_troubles = my_lookup.Keys

        ' Avoid the off by 1 error (j=0 to 4 is 5 items not the 4 you declared in the array
        For Each my_trouble In my_troubles

        Set my_range = this_range.Duplicate

        With my_range

        With .Find

        .ClearAllFuzzyOptions
        .ClearFormatting
        .text = my_trouble
        .Execute

        End With

        Do While .Find.Found

        Debug.Print "Find: " & my_trouble & " add cmt box w/ "; my_lookup.item(my_trouble)
        .Comments.Add .Duplicate, my_lookup.item(my_trouble)
        .Collapse Direction:=wdCollapseEnd
        .Move unit:=wdCharacter, Count:=1
        .Find.Execute

        Loop

        End With

        Next

        End Sub






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 16:26









        FreeflowFreeflow

        606129




        606129






























            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%2f53449326%2fhow-to-find-text-strings-in-word-using-a-two-dimensional-array%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