nicktherod
Programmer
I want to write a simple bit of code that finds a word and replaces it with another that appears in a Word doc. I have tried Find.Execute and get Type Mismatch error when I run it. Can anyone either help me out or refer me to a book or help files on the subject. THe code that I wrote is as follows:
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;
}
//Create a new document
Documents oDocs;
oDocs = oWordApp.GetDocuments();
oDocs.Open(COleVariant("c:\\doc.doc", vOpt,vFalse, vOpt, vOpt,
vOpt, vOpt,vOpt,vOpt,vOpt,vOpt, vOpt);
Find oFind;
oFind= oWordApp.GetActiveDocument();
oFind.Execute(COleVariant("one",vTrue,vTrue,vFalse,vFalse,
vTrue,COleVariant((short)0),vFalse,vFalse,
COleVariant("Replaced",COleVariant((short)0),vFalse,
vFalse,vFalse,vFalse);
//Save the document
_Document oActiveDoc;
oActiveDoc = oWordApp.GetActiveDocument();
oActiveDoc.SaveAs(COleVariant("c:\\doc1.doc",
COleVariant((short)0),
vFalse, COleVariant("", vTrue, COleVariant("",
vFalse, vFalse, vFalse, vFalse, vFalse);
//Quit the application
oWordApp.Quit(vOpt, vOpt, vOpt);
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;
}
//Create a new document
Documents oDocs;
oDocs = oWordApp.GetDocuments();
oDocs.Open(COleVariant("c:\\doc.doc", vOpt,vFalse, vOpt, vOpt,
vOpt, vOpt,vOpt,vOpt,vOpt,vOpt, vOpt);
Find oFind;
oFind= oWordApp.GetActiveDocument();
oFind.Execute(COleVariant("one",vTrue,vTrue,vFalse,vFalse,
vTrue,COleVariant((short)0),vFalse,vFalse,
COleVariant("Replaced",COleVariant((short)0),vFalse,
vFalse,vFalse,vFalse);
//Save the document
_Document oActiveDoc;
oActiveDoc = oWordApp.GetActiveDocument();
oActiveDoc.SaveAs(COleVariant("c:\\doc1.doc",
COleVariant((short)0),
vFalse, COleVariant("", vTrue, COleVariant("",
vFalse, vFalse, vFalse, vFalse, vFalse);
//Quit the application
oWordApp.Quit(vOpt, vOpt, vOpt);