Click here to Skip to main content
13,045,790 members (46,245 online)
Click here to Skip to main content
Add your own
alternative version


44 bookmarked
Posted 17 Dec 2002

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

, 17 Dec 2002
Rate this:
Please Sign up or sign in to vote.
An MFC based implementation of a MS Word Speller.

Sample Image - dictionary_automation.gif


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?


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.


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
    m_Word="Type the word to search...";
   //(Edit box)
   // clear the curent synonym-list (Combo box)

   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);

   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...
   // 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
     CString str;
     int syncount;
     // get the size of the list
     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
    for(int i=0 ; i < syncount ; i++)
           // get the pointer to the array data
       ptr=(long*) (vSyno.parray)->pvData;
       // get each meaning
       // add to the combo
        // done... so unlock
        if (lResult) return;
   m_SynList="It means...";//(Combo)
   UpdateData(FALSE);// display list

Version History

  • 12/18/2002 - Initial release


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

Web Developer
India India
I'm a nature lover

You may also be interested in...


Comments and Discussions

Questiondownload links Pin
Member 431831314-Aug-15 12:37
memberMember 431831314-Aug-15 12:37 
QuestionDoesnot Work on XP Pin
robshere21-Jun-07 21:41
memberrobshere21-Jun-07 21:41 
AnswerRe: Doesnot Work on XP Pin
pratheesh21-Jun-07 22:32
memberpratheesh21-Jun-07 22:32 
GeneralWin XP Pin
r_ravi22-Apr-07 4:39
memberr_ravi22-Apr-07 4:39 
GeneralRe: Win XP Pin
pratheesh21-Jun-07 22:36
memberpratheesh21-Jun-07 22:36 
QuestionLittle help for a beginner (FMC) Pin
Ckito12-Sep-06 5:42
memberCkito12-Sep-06 5:42 
GeneralIs it possible to write the same application in c# Pin
Vijay Kumar Raja Grandhi24-Mar-06 2:08
memberVijay Kumar Raja Grandhi24-Mar-06 2:08 
GeneralMeanings Pin
cweaver2-Feb-05 9:41
membercweaver2-Feb-05 9:41 
GeneralRe: Meanings Pin
cweaver3-Feb-05 9:56
membercweaver3-Feb-05 9:56 
GeneralAbout recognition Pin
sinmorn24-May-04 15:09
membersinmorn24-May-04 15:09 
QuestionHow to Get the Synonyms/CheckSpelling in other languge Pin
srinivas_s27-Aug-03 18:24
membersrinivas_s27-Aug-03 18:24 
QuestionHow to Get the Synonyms/CheckSpelling in other languge Pin
srinivas_s27-Aug-03 18:24
susssrinivas_s27-Aug-03 18:24 
GeneralWord automate Pin
RomanticHero8-Aug-03 2:33
memberRomanticHero8-Aug-03 2:33 
Generalfemale voice Pin
JorgeDiez3-Jul-03 11:48
memberJorgeDiez3-Jul-03 11:48 
GeneralRe: female voice Pin
pratheesh4-Jul-03 17:16
memberpratheesh4-Jul-03 17:16 
General&quot;nature&quot; lover or nature &quot;lover&quot; Pin
Anonymous11-Jun-03 7:35
sussAnonymous11-Jun-03 7:35 
GeneralRe: &quot;nature&quot; lover or nature &quot;lover&quot; Pin
Anonymous13-Jun-03 21:26
sussAnonymous13-Jun-03 21:26 
GeneralClosing WINWORD.EXE Pin
JoeSox11-Apr-03 12:01
memberJoeSox11-Apr-03 12:01 
GeneralRe: Closing WINWORD.EXE Pin
Spiros1-May-03 8:15
memberSpiros1-May-03 8:15 
GeneralCTextToSpeech Question... Pin
JoeSox20-Dec-02 10:11
memberJoeSox20-Dec-02 10:11 
GeneralRe: CTextToSpeech Question... Pin
pratheesh20-Dec-02 23:07
memberpratheesh20-Dec-02 23:07 
GeneralRe: CTextToSpeech Question... Pin
JoeSox21-Dec-02 6:08
memberJoeSox21-Dec-02 6:08 
GeneralRe: CTextToSpeech Question... Pin
pratheesh21-Dec-02 15:32
memberpratheesh21-Dec-02 15:32 
GeneralRe: CTextToSpeech Question... Pin
JoeSox21-Dec-02 21:17
memberJoeSox21-Dec-02 21:17 
Generalsynonym list for all meanings Pin
anonymous20-Dec-02 4:30
sussanonymous20-Dec-02 4:30 
GeneralRe: synonym list for all meanings Pin
pratheesh20-Dec-02 7:39
memberpratheesh20-Dec-02 7:39 
Generaltext to speech help Pin
preetin19-Dec-02 10:07
susspreetin19-Dec-02 10:07 
Generalexception catching hack Pin
Scracha18-Dec-02 23:53
memberScracha18-Dec-02 23:53 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 18 Dec 2002
Article Copyright 2002 by pratheesh
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid