Click here to Skip to main content
15,921,884 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using WordApplication = Microsoft.Office.Interop.Word.Application;
using Microsoft.Office.Interop.Outlook;
namespace synonyms_try1
{
    public partial class Form1 : Form
    {
        Class1 c=new Class1();
        public Form1()
        {
            InitializeComponent();
        }

        private void bSubmit_Click(object sender, EventArgs e)
        {
              Microsoft.Office.Interop.Word.Application appWord;      // word application var
      object objNull = null;      // word object method calls require
                        // references to objects... create
                        // object for null and
      object objFalse = false;      // false entries and language

      object objLanguage = Microsoft.Office.Interop.Word.WdLanguageID.wdEnglishUS; // or appropritate lang!

      try
      {
            // Try opening Word app
            appWord = new Microsoft.Office.Interop.Word.Application();
      }
      catch(System.Exception exc)
      {
            // could not open word... show error message and return
            MessageBox.Show(exc.Message);
            return;
      }

      // clear synonym listbox lbSynonym
      lbSynonym.Items.Clear();

      // now call get_SynonymInfo to get SynonymInfo structure for
      // word entered in TextBox tbWord
      Microsoft.Office.Interop.Word.SynonymInfo si =
               appWord.get_SynonymInfo(tbWord.Text, ref (objLanguage));
        
      
      // first find out how many meanings were found for word
      int iMeanings = (int)si.MeaningCount;
      
      if(iMeanings > 0)
      {
            // one or more meanings were found... loop over each
            // (notice SynonymInfo.MeaningList is type System.ArrayList!)
          
          foreach (string strMeaning in (Array)si.MeaningList)
            {
                  // get Synonym List for each meaning... note that
                  // get_SynonymList takes an object ref, thus we
                  // must create objMeaning object
                  object objMeaning = strMeaning;
                  System.Array aSynonyms =
                        (System.Array)si.get_SynonymList(ref objMeaning);
                  foreach(string strSynonym in aSynonyms)
                  {
                        // loop over each synonym in ArrayList
                        // and add to lbSynonym ListBox
                        lbSynonym.Items.Add(strSynonym);
                  }
            }
      }
      else
      {
            // no meanings/synonyms found... set ListBox value to "NONE"
            lbSynonym.Items.Add("NONE");
      }
      // Clean up COM object
      c.Destroy(si);

      // quit WINWORD app
      appWord.Quit(ref objFalse, ref objNull, ref objNull);
      // clean up COM object
      c.Destroy(appWord);
      
}

        }
    }

i m using this code for finding synonyms but it give error
Unable to cast object of type 'System.String[*]' to type 'System.String[]'.

The error on that line......
foreach (string strMeaning in (Array)si.MeaningList)


Tell me the suggestion if u have.. i take this code from this link jczerk solution use in this link..
C#: WordNet / MS Word Object Library Spellchecker/SynonymInfo[^]

What I have tried:

i am trying to find the synonyms using microsoft word ....but it give the error
Posted
Updated 23-Sep-16 3:54am

1 solution

Try changing your code to use As Array and use var for your declarations. E.g. (I've also added in some null checking)
C#
if (si.MeaningCount > 0)
{
    // one or more meanings were found... loop over each
    // (notice SynonymInfo.MeaningList is type System.ArrayList!)
    var strMeanings = si.MeaningList as Array;
    if (strMeanings != null)
        foreach (var strMeaning in strMeanings)
        {
            // get Synonym List for each meaning... note that
            // get_SynonymList takes an object ref, thus we
            // must create objMeaning object
            var objMeaning = strMeaning;

            var aSynonyms = si.SynonymList[objMeaning];

            var strSynonyms = si.SynonymList[objMeaning] as Array;
            if (strSynonyms != null)
                foreach (string strSynonym in strSynonyms)
                {
                    // loop over each synonym in ArrayList
                    // and add to lbSynonym ListBox
                    lbSynonym.Items.Add(strSynonym);
                }
        }
}
else
{
    // no meanings/synonyms found... set ListBox value to "NONE"
    lbSynonym.Items.Add("NONE");
}

This article for reference: How to: Safely Cast by Using as and is Operators (C# Programming Guide)[^]
 
Share this answer
 
Comments
Maciej Los 23-Sep-16 10:40am    
Looks perfect!
Member 12107284 26-Sep-16 4:16am    
thanks.....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900