How to fix WdPartOfSpeech that cannot be applied to operands of string[] data type.

John 486 Reputation points
2025-04-13T20:17:12.0633333+00:00

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException

HResult=0x80131500

Message=Operator '==' cannot be applied to operands of type 'string[]' and 'Microsoft.Office.Interop.Word.WdPartOfSpeech'

Source=<Cannot evaluate the exception source>

StackTrace:

<Cannot evaluate the exception stack trace>

Here is the rest of the code snippet, which has compilation errors that included the message above.

      private void BSearchForVerbs_Click(object sender, EventArgs e)
      {
          LBSelectedVerbs.Items.Clear();
          Globals.ThisAddIn.Application.Selection.MoveRight(WdUnits.wdWord, 1, WdMovementType.wdExtend);
          string wordText = Globals.ThisAddIn.Application.Selection.Text;
          var synonymInfo = Globals.ThisAddIn.Application.SynonymInfo[wordText];
          
          if (synonymInfo.Found.Equals(true) && synonymInfo.RelatedWordList == WdPartOfSpeech.wdVerb)
          {
              LBSelectedVerbs.Items.Add(wordText);
          }
          // Check if this word can be a verb
          
          Globals.ThisAddIn.Application.Selection.MoveRight(WdUnits.wdWord, 1, WdMovementType.wdMove);
        
      }
  
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,487 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 121.6K Reputation points
    2025-04-13T22:13:31.0633333+00:00

    Try something like this:

    
    if( synonymInfo.Found )
    {
        Array pa = (Array)(object)synonymInfo.PartOfSpeechList;
    
        for( int i = 1; i <= synonymInfo.MeaningCount; ++i )
        {
            if( (WdPartOfSpeech)pa.GetValue( i ) == WdPartOfSpeech.wdVerb )
            {
                Array sa = (Array)(object)synonymInfo.SynonymList[i];
    
                foreach( string s in sa )
                {
                    LBSelectedVerbs.Items.Add( s );
                }
            }
        }
    }
    

    It is also possible to find the distinct words.

    0 comments No comments

0 additional answers

Sort by: Most helpful

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.