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()
{
UpdateData();
if(m_Word=="")
{
m_Word="Type the word to search...";
UpdateData(0);
return;
}
m_SynComb.ResetContent();
COleVariant vTrue((short)TRUE), vFalse((short)FALSE),
vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
_Application oWordApp;
if (!oWordApp.CreateDispatch("Word.Application", NULL))
{
AfxMessageBox("CreateDispatch failed.", MB_OK | MB_SETFOREGROUND);
return;
}
Documents oDocs;
_Document oDoc;
oDocs = oWordApp.GetDocuments();
oDoc = oDocs.Add(vOpt, vOpt,vOpt, vOpt);
Selection oSel;
oSel = oWordApp.GetSelection();
oSel.SetText(m_Word);
Range oRng=oSel.GetRange();
SynonymInfo oSin=oRng.GetSynonymInfo();
COleVariant vMeaning(short(1));
COleVariant vSyno=oSin.GetSynonymList(vMeaning);
long index=0;
if(vSyno.vt==(VT_ARRAY|VT_BSTR))
{
CString str;
int syncount;
syncount=(vSyno.parray)->cbElements;
long* ptr;
HRESULT lResult;
if(syncount==0) return;
lResult=SafeArrayLock(vSyno.parray);
if(lResult)return;
for(int i=0 ; i < syncount ; i++)
{
ptr=(long*) (vSyno.parray)->pvData;
str=(BSTR)ptr[i];
m_SynComb.AddString(str);
}
lResult=SafeArrayUnlock(vSyno.parray);
if (lResult) return;
}
m_SynList="It means...";
UpdateData(FALSE);
}
Version History
- 12/18/2002 - Initial release