Click here to Skip to main content
Click here to Skip to main content

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

By , 17 Dec 2002
Rate this:
Please Sign up or sign in to vote.

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

About the Author

pratheesh
Web Developer
India India
I'm a nature lover

Comments and Discussions

 
QuestionDoesnot Work on XP Pinmemberrobshere21-Jun-07 21:41 
AnswerRe: Doesnot Work on XP Pinmemberpratheesh21-Jun-07 22:32 
GeneralWin XP Pinmemberr_ravi22-Apr-07 4:39 
GeneralRe: Win XP Pinmemberpratheesh21-Jun-07 22:36 
QuestionLittle help for a beginner (FMC) PinmemberCkito12-Sep-06 5:42 
GeneralIs it possible to write the same application in c# PinmemberVijay Kumar Raja Grandhi24-Mar-06 2:08 
GeneralMeanings Pinmembercweaver2-Feb-05 9:41 
GeneralRe: Meanings Pinmembercweaver3-Feb-05 9:56 
GeneralAbout recognition Pinmembersinmorn24-May-04 15:09 
QuestionHow to Get the Synonyms/CheckSpelling in other languge Pinmembersrinivas_s27-Aug-03 18:24 
QuestionHow to Get the Synonyms/CheckSpelling in other languge Pinsusssrinivas_s27-Aug-03 18:24 
GeneralWord automate PinmemberRomanticHero8-Aug-03 2:33 
Generalfemale voice PinmemberJorgeDiez3-Jul-03 11:48 
GeneralRe: female voice Pinmemberpratheesh4-Jul-03 17:16 
General&quot;nature&quot; lover or nature &quot;lover&quot; PinsussAnonymous11-Jun-03 7:35 
GeneralRe: &quot;nature&quot; lover or nature &quot;lover&quot; PinsussAnonymous13-Jun-03 21:26 
GeneralClosing WINWORD.EXE PinmemberJoeSox11-Apr-03 12:01 
GeneralRe: Closing WINWORD.EXE PinmemberSpiros1-May-03 8:15 
GeneralCTextToSpeech Question... PinmemberJoeSox20-Dec-02 10:11 
GeneralRe: CTextToSpeech Question... Pinmemberpratheesh20-Dec-02 23:07 
GeneralRe: CTextToSpeech Question... PinmemberJoeSox21-Dec-02 6:08 
GeneralRe: CTextToSpeech Question... Pinmemberpratheesh21-Dec-02 15:32 
GeneralRe: CTextToSpeech Question... PinmemberJoeSox21-Dec-02 21:17 
Generalsynonym list for all meanings Pinsussanonymous20-Dec-02 4:30 
GeneralRe: synonym list for all meanings Pinmemberpratheesh20-Dec-02 7:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 18 Dec 2002
Article Copyright 2002 by pratheesh
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid