Detecting whether a word is a part of speech

John 466 Reputation points
2025-02-02T01:09:30.4333333+00:00

I have this code snippet. It runs fine, but it does not detect anything as expected.

What am I missing?

            WordCtDoc = TSearchNumberOfTimes.Text.ToString();
            WordCt = 1;
            while (WordCt <= WordCtDoc.Max())
            {
                Globals.ThisAddIn.Application.Selection.MoveRight(WdUnits.wdWord, 1, WdMovementType.wdExtend);
                if (Globals.ThisAddIn.Application.Selection.Text.Equals(Globals.ThisAddIn.Application.SynonymInfo[Globals.ThisAddIn.Application.Selection.Text, WdPartOfSpeech.wdAdjective]))
                {
                    
                    Globals.ThisAddIn.Application.Selection.Font.Color = WdColor.wdColorRed;
                    LBSelectedAdjectives.Items.Add(Globals.ThisAddIn.Application.Selection.Text);
                }

                Globals.ThisAddIn.Application.Selection.MoveRight(WdUnits.wdWord, 1, WdMovementType.wdMove);
                WordCt++;
            }

Word
Word
A family of Microsoft word processing software products for creating web, email, and print documents.
942 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,268 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
4,214 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Marcin Policht 33,775 Reputation points MVP
    2025-02-02T02:41:20.4866667+00:00

    Looks like there are a few things that might be causing your code to not work as expected...

    1. Incorrect use of SynonymInfo The SynonymInfo object in Word's interop API provides synonyms for a given word but does not return a boolean or a direct comparison to check whether a word belongs to a part of speech.

    To fix this, check MeaningCount before accessing synonyms. Instead of trying to compare Selection.Text directly to SynonymInfo, you need to check whether SynonymInfo actually contains meanings.

    Try the following:

    WordCtDoc = TSearchNumberOfTimes.Text.ToString();
    WordCt = 1;
    
    while (WordCt <= WordCtDoc.Length) // Fix: Changed to 'Length' instead of Max()
    {
        Globals.ThisAddIn.Application.Selection.MoveRight(WdUnits.wdWord, 1, WdMovementType.wdExtend);
    
        string selectedText = Globals.ThisAddIn.Application.Selection.Text.Trim(); // Ensure no extra spaces
        var synonymInfo = Globals.ThisAddIn.Application.SynonymInfo[selectedText, WdPartOfSpeech.wdAdjective];
    
        if (synonymInfo.MeaningCount > 0) // Fix: Properly check if the word has meanings
        {
            Globals.ThisAddIn.Application.Selection.Font.Color = WdColor.wdColorRed;
            LBSelectedAdjectives.Items.Add(selectedText);
        }
    
        Globals.ThisAddIn.Application.Selection.MoveRight(WdUnits.wdWord, 1, WdMovementType.wdMove);
        WordCt++;
    }
    

    2. Fixing WordCtDoc.Max() Your original code uses WordCtDoc.Max(), which does not seem to make sense in this context. Since WordCtDoc is a string, calling .Max() will return the highest character value, not the length of the string.

    To fix this, use WordCtDoc.Length instead.

    3. Handling whitespace in selection Word may include extra spaces or newline characters in Selection.Text, causing mismatches. Use .Trim() when accessing the text.

    4. Preventing errors if no synonyms exist If SynonymInfo does not contain any synonyms, directly accessing it without checking MeaningCount first can cause an error. The fix is to check synonymInfo.MeaningCount > 0 before proceeding.


    If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    hth

    Marcin

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.