Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word Automation - Replace??

Status
Not open for further replies.

nicktherod

Programmer
Nov 10, 2000
19
CA
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);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top