//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; _Document oDoc; oDocs = oWordApp.GetDocuments(); oDoc = oDocs.Add(vOpt, vOpt,vOpt,vOpt); //Note for Word 2000: The Add method has 4 arguments in Word 2000. If //you wrapped the classes from the Word type library (msword9.olb), //modify the Add method to provide 4 optional arguments: // oDoc = oDocs.Add(vOpt, vOpt, vOpt, vOpt);
//Add text to the document Selection oSel; oSel = oWordApp.GetSelection(); oSel.TypeText("one"); oSel.TypeParagraph(); oSel.TypeText("two"); oSel.TypeParagraph(); oSel.TypeText("three");
include "msword9.h" _Application WordApp; //代表WORD程序
Documents docs; //代表一个WORD程序里的所有文档
_Document doc; //代表一个文档 if(!WordApp.CreateDispatch("Word.Application",NULL))//创建一个新的word程序
{
AfxMessageBox("创建ms_word服务失败");
exit(1);
} //让用户能够查看自动化的过程
WordApp.SetVisible(true);
//让docs和word程序连接,docs代表word中所有文档
docs=WordApp.GetDocuments();
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR);
//用docs来打开一个文档,并将句柄返回给doc
doc=docs.Add(COleVariant(".\\ox.doc"),covOptional,covOptional,covOptional);
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;
_Document oDoc;
oDocs = oWordApp.GetDocuments();
oDoc = oDocs.Add(vOpt, vOpt,vOpt,vOpt);
//Note for Word 2000: The Add method has 4 arguments in Word 2000. If
//you wrapped the classes from the Word type library (msword9.olb),
//modify the Add method to provide 4 optional arguments:
// oDoc = oDocs.Add(vOpt, vOpt, vOpt, vOpt);
//Add text to the document
Selection oSel;
oSel = oWordApp.GetSelection();
oSel.TypeText("one");
oSel.TypeParagraph();
oSel.TypeText("two");
oSel.TypeParagraph();
oSel.TypeText("three");
//Save the document
_Document oActiveDoc;
oActiveDoc = oWordApp.GetActiveDocument();
oActiveDoc.SaveAs(COleVariant("c:\\doc1.doc"),
COleVariant((short)0),
vFalse, COleVariant(""), vFalse, COleVariant(""),
vFalse, vFalse, vFalse, vFalse, vFalse);
//Quit the application
oWordApp.Quit(vOpt, vOpt, vOpt);