locked
Why does this code not work? RRS feed

  • Question

  • In Ribbon.vb, the sub Button4_Click is to run Globals.ThisAddIn.OWCheck, which is a sub in the class file ThisAddIn. This code is for finding a word and then commenting it, but the code does not work on the click of the button. I worked it in both Debug and Release Mode and the program(WINWORD.EXE) exits with code 0. Can you tell me what's wrong in the following code?

     

    Public Class ThisAddIn
        Sub OWCheck()
            Dim findText As String = " awesome "
            Application.Selection.Find.ClearFormatting()
            If Application.Selection.Find.Execute(findText) = True Then
                Dim findedText As Word.Range
                findedText = Me.Application.ActiveDocumentRange.Select()
                Me.Application.ActiveDocument.Comments.Add(findedText, "This is an awesome word.")
            End If
        End Sub
    End Class
    

     


    Tanu Agg-Director of Awesomeness and All things Nerdy
    • Edited by Tanu Agg Friday, January 6, 2012 11:50 PM
    Friday, January 6, 2012 11:49 PM

Answers

  • Well you have errors in this one line.

    findedText = Me.Application.ActiveDocumentRange.Select()

    It should be: findedtext = Me.Application.Selection.Range

    Ater correcting the syntax the subroutine itself works and I verified it by converting it to the below VBA.  However, a question might still remain because you didn't say what exactly didn't work when you tested the code.

    Sub OWCheck()
        Dim findText As String: findText = " awesome "
        Application.Selection.Find.ClearFormatting
        If Application.Selection.Find.Execute(findText) = True Then
            Dim findedtext As Word.Range
            Set findedtext = Application.Selection.Range
            Application.ActiveDocument.Comments.Add findedtext, "This is an awesome word."
        End If
    End Sub
    

     

     


    Kind Regards, Rich ... http://greatcirclelearning.com
    Saturday, January 7, 2012 3:51 AM
  • Yes it's VBA and that will convert to VB.Net easily.

    Yes a two word combination will work also. Just put it in the string "cool,a lot, good, etc"


    Kind Regards, Rich ... http://greatcirclelearning.com
    • Marked as answer by Tanu Agg Sunday, January 8, 2012 4:41 PM
    Sunday, January 8, 2012 4:30 PM

All replies

  • Well you have errors in this one line.

    findedText = Me.Application.ActiveDocumentRange.Select()

    It should be: findedtext = Me.Application.Selection.Range

    Ater correcting the syntax the subroutine itself works and I verified it by converting it to the below VBA.  However, a question might still remain because you didn't say what exactly didn't work when you tested the code.

    Sub OWCheck()
        Dim findText As String: findText = " awesome "
        Application.Selection.Find.ClearFormatting
        If Application.Selection.Find.Execute(findText) = True Then
            Dim findedtext As Word.Range
            Set findedtext = Application.Selection.Range
            Application.ActiveDocument.Comments.Add findedtext, "This is an awesome word."
        End If
    End Sub
    

     

     


    Kind Regards, Rich ... http://greatcirclelearning.com
    Saturday, January 7, 2012 3:51 AM
  • Thank you for that response.

    Another question I have is: How do I make the code find all word forms of awesome? It could be awesomely, awesomeness, etc. etc. I know the code would highlight "awesome" in these word forms, but how do I make it highlight the whole word form? I don't want to manually search awesomeness, awesomely.

    Would it also be possible to match only the whole word form? For example, I was going to use this code for a lot of words, such as "cool", but the finder could find words such as "coolant" and still highlight just the cool, although the word "coolant" has very little relation to the word "cool".


    Tanu Agg-Director of Awesomeness and All things Nerdy
    Saturday, January 7, 2012 2:14 PM
  • Take a look at this code and I think you'll see how it works. I did it in VBA for speed and you should be able to port it over to VB.Net quickly and use your own reference names.

    Sub OWCheck()
        Dim doc As Word.Document
        Dim rng As Word.Range
        Dim done As Boolean: done = False
        Dim findText As String: findText = "awesome"
        
        Set doc = Word.ActiveDocument
        Set rng = doc.Content
        rng.Collapse Word.WdCollapseDirection.wdCollapseStart
        rng.Find.ClearFormatting
        rng.Find.Text = findText
        Do Until done
            With rng.Find
                .Forward = True
                .Wrap = wdFindStop
                .Execute
            End With
            If rng.Find.Found Then
                rng.EndOf unit:=Word.WdUnits.wdWord, Extend:=Word.WdMovementType.wdExtend
                doc.Comments.Add rng, "This is an awesome word."
                rng.Collapse Word.WdCollapseDirection.wdCollapseEnd
            Else
                done = True
            End If
        Loop
    End Sub
    
    

    Regarding your question about "cool" I think the best approach on that word is to create an inclusion list because there will be only certain variations that you want. Coolest might work but cooler might not.  What you do is build a table of acceptable variations and when in my example code above when the value of "rng" equals one of the acceptable variations of the word you add the comment.

    Hope this helps

     


    Kind Regards, Rich ... http://greatcirclelearning.com
    Saturday, January 7, 2012 3:19 PM
  •  

    Public Class ThisAddIn
        Dim findText, FindText2, FindText3, FindText4, FindText5, FindText6, FindText7, FindText8, FindText9, FindText10, FindText11 As String
        Sub OWCheck()
                findText = " good "
                Application.Selection.Find.ClearFormatting()
                If Application.Selection.Find.Execute(findText) = True Then
                    Dim findedText As Word.Range
                    findedText = Me.Application.Selection.Range
                    Me.Application.ActiveDocument.Comments.Add(findedText, "This is an OUTLAWED word.")
                End If
                FindText2 = "Good "
                Application.Selection.Find.ClearFormatting()
                If Application.Selection.Find.Execute(FindText2) = True Then
                    Dim findedText As Word.Range
                    findedText = Me.Application.Selection.Range
                    Me.Application.ActiveDocument.Comments.Add(findedText, "This is an OUTLAWED word.")
                End If
                FindText3 = "Good."
                Application.Selection.Find.ClearFormatting()
                If Application.Selection.Find.Execute(FindText3) = True Then
                    Dim findedText As Word.Range
                    findedText = Me.Application.Selection.Range
                    Me.Application.ActiveDocument.Comments.Add(findedText, "This is an OUTLAWED word.")
                End If
                FindText4 = "Good!"
                Application.Selection.Find.ClearFormatting()
                If Application.Selection.Find.Execute(FindText4) = True Then
                    Dim findedText As Word.Range
                    findedText = Me.Application.Selection.Range
                    Me.Application.ActiveDocument.Comments.Add(findedText, "This is an OUTLAWED word.")
                End If
                FindText5 = "Good?"
                Application.Selection.Find.ClearFormatting()
                If Application.Selection.Find.Execute(FindText5) = True Then
                    Dim findedText As Word.Range
                    findedText = Me.Application.Selection.Range
                    Me.Application.ActiveDocument.Comments.Add(findedText, "This is an OUTLAWED word.")
                End If
                FindText6 = " good."
                Application.Selection.Find.ClearFormatting()
                If Application.Selection.Find.Execute(FindText6) = True Then
                    Dim findedText As Word.Range
                    findedText = Me.Application.Selection.Range
                    Me.Application.ActiveDocument.Comments.Add(findedText, "This is an OUTLAWED word.")
                End If
                FindText7 = " good!"
                Application.Selection.Find.ClearFormatting()
                If Application.Selection.Find.Execute(FindText7) = True Then
                    Dim findedText As Word.Range
                    findedText = Me.Application.Selection.Range
                    Me.Application.ActiveDocument.Comments.Add(findedText, "This is an OUTLAWED word.")
                End If
                FindText8 = " good?"
                Application.Selection.Find.ClearFormatting()
                If Application.Selection.Find.Execute(FindText8) = True Then
                    Dim findedText As Word.Range
                    findedText = Me.Application.Selection.Range
                    Me.Application.ActiveDocument.Comments.Add(findedText, "This is an OUTLAWED word.")
                End If
                FindText9 = " good,"
                Application.Selection.Find.ClearFormatting()
                If Application.Selection.Find.Execute(FindText9) = True Then
                    Dim findedText As Word.Range
                    findedText = Me.Application.Selection.Range
                    Me.Application.ActiveDocument.Comments.Add(findedText, "This is an OUTLAWED word.")
                End If
                FindText10 = " good;"
                Application.Selection.Find.ClearFormatting()
                If Application.Selection.Find.Execute(FindText10) = True Then
                    Dim findedText As Word.Range
                    findedText = Me.Application.Selection.Range
                    Me.Application.ActiveDocument.Comments.Add(findedText, "This is an OUTLAWED word.")
                End If
                FindText11 = " good:"
                Application.Selection.Find.ClearFormatting()
                If Application.Selection.Find.Execute(FindText11) = True Then
                    Dim findedText As Word.Range
                    findedText = Me.Application.Selection.Range
                    Me.Application.ActiveDocument.Comments.Add(findedText, "This is an OUTLAWED word.")
                End If
        End Sub
    End Class
    

     

    Well, I was also using this code for a typing checker for my English class teacher's rules, but this code only checks for one outlawed occurrence at a time. You have to repeatedly highlight other unchecked areas, and click the button again,  to get every occurrence.

    Here are the sentences I made for testing:

    Good. Very good: and good; good, but good? Good! Good? This is good!


    Tanu Agg-Director of Awesomeness and All things Nerdy


    • Edited by Tanu Agg Sunday, January 8, 2012 2:39 PM
    Sunday, January 8, 2012 2:36 PM
  • With the code I posted you don't have to worry about punctuation because the unit:=wdWord recognizes that punctuation is not part of the actual word and thus no matter what punctuation follows, the word is still found.  Regarding capitalization, since in the Find statement criteria matching capitalization was not specified then it will find, regardless of capitalization, all occurrences of the word.

    Here is an adjustment to the routine I previously posted. This new routine shows how you can use a string variable to search for various words in the entire document.

    Hope this helps and good luck with your project

    Sub OWCheck()
        Dim Doc As Word.Document
        Dim rng As Word.Range
        Dim done As Boolean
        Dim findText As String
        Dim i As Integer
        
        findText = "good,cool,kewl" 'kewl... a pirate sailor's slang for cool :-)
        Set Doc = Word.ActiveDocument
        For i = 0 To UBound(Split(findText, ","))
            done = False
            Set rng = Doc.Content
            rng.Collapse Word.WdCollapseDirection.wdCollapseStart
            With rng.Find
                .ClearFormatting
                .Forward = True
                .Wrap = wdFindStop
            End With
            Do Until done
                If rng.Find.Execute(Split(findText, ",")(i)) = True Then
                    rng.EndOf unit:=Word.WdUnits.wdWord, Extend:=Word.WdMovementType.wdExtend
                    Doc.Comments.Add rng, "This is an OUTLAWED word."
                    rng.Collapse Word.WdCollapseDirection.wdCollapseEnd
                Else
                    done = True
                End If
            Loop
        Next
    End Sub
    

     


    Kind Regards, Rich ... http://greatcirclelearning.com
    Sunday, January 8, 2012 3:48 PM
  • The above code is in VBA, right?

     Also, would this work for 2 word combinations, such as "a lot"?


    Tanu Agg-Director of Awesomeness and All things Nerdy
    • Edited by Tanu Agg Sunday, January 8, 2012 4:03 PM
    Sunday, January 8, 2012 3:55 PM
  • Yes it's VBA and that will convert to VB.Net easily.

    Yes a two word combination will work also. Just put it in the string "cool,a lot, good, etc"


    Kind Regards, Rich ... http://greatcirclelearning.com
    • Marked as answer by Tanu Agg Sunday, January 8, 2012 4:41 PM
    Sunday, January 8, 2012 4:30 PM
  • Thank you very much Rich! I think we may close this thread now.
    Tanu Agg-Director of Awesomeness and All things Nerdy
    • Marked as answer by Tanu Agg Sunday, January 8, 2012 4:41 PM
    • Unmarked as answer by Tanu Agg Sunday, January 8, 2012 4:41 PM
    Sunday, January 8, 2012 4:41 PM