working with the Word spellchecker

M

Mark Nuetzmann

Hi,

I am trying to use the exposed Word.Application automation server to provide
spell checking functionality for my application. The following code creates
the server, sets up the text to be checked, runs the Word spell checker and
returns the corrected text. My question is when the Cancel/Close button is
clicked fromt the Spell Check dialog, I get this very annoying opening of a
Word application window that will then close when I call the Close method on
the document interface. How do I keep the application window from ever
displaying at all?


Word::_ApplicationPtr pApp;
Word::_DocumentPtr pDoc;

////////////////////////////////////////////////////////////////////////////
/
// Function name : SpellChecker::ExecSpellingAndGrammarDialog
// Description :
// Return type : STDMETHODIMP
// Argument : BSTR bstrOrigText
// Argument : BOOL bRTF
// Argument : BSTR *bstrOutText

STDMETHODIMP SpellChecker::ExecSpellingAndGrammarDialog(BSTR bstrOrigText,
BOOL bRTF, BSTR *bstrOutText)
{
TCHAR szJustCR[] = {13, 0};
TCHAR szCRLF[] = {13, 10, 0};
bool bCrExists = false;
HRESULT hResult = S_OK;

CString origText = bstrOrigText;
CString outText;

if ( origText.Find(szJustCR) != -1 )
bCrExists = true;
try
{
*bstrOutText = NULL;

if (pApp == NULL)
{
pApp.CreateInstance(__uuidof(Word::Application));
pApp->Visible = VARIANT_FALSE;
}

if ( pDoc == NULL )
{
// We add an empty document; otherwise the GetSpellingSuggestions method
will fail.
pDoc = pApp->Documents->Add();
}
pApp->Selection->TypeText(bstrOrigText);

Word::DialogPtr spDialog =
pApp->Dialogs->Item(Word::wdDialogToolsSpellingAndGrammar);
if ( spDialog->Display() == VARIANT_TRUE )
{
pApp->Visible = VARIANT_FALSE;
pApp->Selection->WholeStory();
outText = (char*)pApp->Selection->Text;

if ( bCrExists )
outText.Replace(szJustCR, szCRLF);
else
outText.Replace(szJustCR, "");
*bstrOutText = outText.AllocSysString();
}
else
{
pApp->Visible = VARIANT_FALSE;
variant_t vtFalse = VARIANT_FALSE;
pDoc->Close(&vtFalse);
pDoc = NULL;
}
}
catch (_com_error& e)
{
ATLTRACE("DisplaySpellingAndGrammarDialog COM Error: 0x%08lX.
%s\n",e.Error(), e.ErrorMessage());
hResult = e.Error();

// Attempt to recover for the next call
pDoc = NULL;

try
{
if ( pApp )
pApp->Quit();
pApp = NULL;
}
catch (...)
{
pApp = NULL;
}
}
catch (...)
{
DWORD err = GetLastError();
ATLTRACE("DisplaySpellingAndGrammarDialog General exception caught.
GLE:0x%08x\n", err);
hResult = E_FAIL;
}

return hResult;
}


Thanks,
Mark.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top