Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Creating a Dictionary using Word Automation and Text-to-Speech Control

0.00/5 (No votes)
17 Dec 2002 1  
An MFC based implementation of a MS Word Speller.

Sample Image - dictionary_automation.gif

Introduction

Whenever I wanted to know the meaning of a word, I used to open-up a Word document and search for synonyms. So, I thought it would be nice to invoke only the Thesaurus, without opening a Word-document. And it would be even nicer to make my computer pronounce the word. Automation was the answer to the first problem and the Microsoft Text to Speech Control solves the second one. Office Automation from VC is one of the least documented topics, I think. But we get plenty of docs for VBA. Let's see how I did it. I will show you a step-by-step approach.

Step 1:

Open up an MFC Dialog-based application.

Step 2:

Create the controls as shown.

Step 3:

Open Class Wizard. In the 'Automation' tab, click 'Add Class' --> 'From Type-Library'. The open file dialog asks for '*.olb' files. You can find it in the 'Microsoft Office directory'. For Office 2000, it is msword9.olb. For older versions, it is msword8.olb. Search it if you can't find it! But you should find it, else we can't proceed!!! Once you find the Type-Lib, open it and a new window pops up. It asks us to choose the automation objects that Word exposes. For our purpose, we may not need all of them. So if you are not sure which classes to choose then select all and press OK.

You can see that 2 files are being added to the project - Msword9.cpp and Msword9.h (in older versions, it is Msword8.cpp and Msword8.h). Examine the new classes that were added.

It is the _Application object that creates the Word application instance. The Documents class enumerates the documents and the Document class is used to work on a particular document.

Step 4:

I hope you know what all objects are needed for Word automation. (You can skip this step if you want.) If you are not sure, you can get a fair idea by using the Word-Macros. Have a look at it... Open a new Word document. Select Tools--->Macro--->Record New Macro. Store the Macro in the new document itself. A new button set will be added to the toolbar and the mouse pointer has a cassette-tape attached to it. Now type in some word and select it. (Use shift+arrow keys, if mouse is not acting friendly.) Select Tools--->Language--->Thesaurus. Now Thesaurus shows the synonyms. Choose Replace or Cancel. In the toolbar, you can see the Stop button for the Macro. Stop it. Now if you open Tools--->Macro--->Macros, you can see the list of Macros. Select Macro1 (or whatever you named it) and click Edit. It will be opened in a VB environment. You can see the Selection object being used frequently. Also the Thesaurus window is being called from the Range object of Selection. In your application, if you call CheckSynonyms() from the Range object, you can display the very same Thesaurus window. But we would like to have our own window, rather than displaying it straightaway, isn't it?

Step5:

Note on COleVariant: The COleVariant is just a wrapper MFC class for the VARIANT type. Also please note that the GetSynonymList() method returns a VARIANT, that contains a safearray of type BSTR.

Step6:

Adding a text-to-speech control: It is very simple. This is an ActiveX control that might be already installed in your system. If not, try this link. Add, text-to-speech class from the registered ActiveX controls list. Connect it with a button handler as usual. Invoke the Speak method, passing the desired text as argument.

Now we'll call each of the Word objects to repeat the processes described in Step4 in the background.

Using the code

//

void CWordtestDlg::OnButton1() 
{
  // TODO: Add your control notification handler code here

  UpdateData();
  if(m_Word=="") 
  {
    m_Word="Type the word to search...";
    UpdateData(0);
    return;
   } 
   //(Edit box)

   // clear the curent synonym-list (Combo box)

   m_SynComb.ResetContent();

   COleVariant vTrue((short)TRUE), vFalse((short)FALSE),  
                      vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
   
   //Start a new instance of Microsoft Word

   _Application oWordApp;
   if (!oWordApp.CreateDispatch("Word.Application", NULL))
   {
     AfxMessageBox("CreateDispatch failed.", MB_OK | MB_SETFOREGROUND);
     return;
   }

   Documents oDocs;
   _Document oDoc;
   oDocs = oWordApp.GetDocuments();
   //Add a doc

   oDoc = oDocs.Add(vOpt, vOpt,vOpt, vOpt);
   // create a Selection object

   Selection oSel;
   // get Selection

   oSel = oWordApp.GetSelection();
   // set text to the word-unknown...

   oSel.SetText(m_Word);
   // get the Range object

   Range oRng=oSel.GetRange();
   // get the SynonymInfo object

   SynonymInfo oSin=oRng.GetSynonymInfo(); 
   // let's find the synonym list for the first meaning only

   COleVariant vMeaning(short(1));
   //get it...

   COleVariant vSyno=oSin.GetSynonymList(vMeaning); 
   long index=0;
   //does nothing... just a confirmation

   if(vSyno.vt==(VT_ARRAY|VT_BSTR))
   {
     CString str;
     int syncount;
     // get the size of the list

     syncount=(vSyno.parray)->cbElements;
     long* ptr;
     HRESULT lResult;
     // no synonyms... but the server is the first to know this!!! 

     //and it will react

     if(syncount==0) return;
          // lock the safe-array before extraction

     lResult=SafeArrayLock(vSyno.parray);
     if(lResult)return;
    for(int i=0 ; i < syncount ; i++)
    {
           // get the pointer to the array data

       ptr=(long*) (vSyno.parray)->pvData;
       // get each meaning

       str=(BSTR)ptr[i];
       // add to the combo

       m_SynComb.AddString(str);
    }
        // done... so unlock

    lResult=SafeArrayUnlock(vSyno.parray);
        if (lResult) return;
   }
   m_SynList="It means...";//(Combo)

   UpdateData(FALSE);// display list

}

Version History

  • 12/18/2002 - Initial release

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here