//Create a new document Documents oDocs; _Document oDoc; oDocs = oWordApp.GetDocuments(); oDoc = oDocs.Add(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(""), vTrue, COleVariant(""), vFalse, vFalse, vFalse, vFalse, vFalse); //Quit the application oWordApp.Quit(vOpt, vOpt, vOpt);
示例代码 /******************************************************************* * This method returns the IDispatch* for the application linked to * this container. *****************************************************************/ LPDISPATCH CEmbed_ExcelCntrItem::GetIDispatch() { //The this and m_lpObject pointers must be valid for this function //to work correctly. The m_lpObject is the IUnknown pointer to // this object. ASSERT_VALID(this); ASSERT(m_lpObject != NULL); LPUNKNOWN lpUnk = m_lpObject; //The embedded application must be running in order for the rest //of the function to work. Run(); //QI for the IOleLink interface of m_lpObject. LPOLELINK lpOleLink = NULL; if (m_lpObject->QueryInterface(IID_IOleLink, (LPVOID FAR*)&lpOleLink) == NOERROR) { ASSERT(lpOleLink != NULL); lpUnk = NULL; //Retrieve the IUnknown interface to the linked application. if (lpOleLink->GetBoundSource(&lpUnk) != NOERROR) { TRACE0("Warning: Link is not connected!\n"); lpOleLink->Release(); return NULL; } ASSERT(lpUnk != NULL); } //QI for the IDispatch interface of the linked application. LPDISPATCH lpDispatch = NULL; if (lpUnk->QueryInterface(IID_IDispatch, (LPVOID FAR*)&lpDispatch) !=NOERROR) { TRACE0("Warning: does not support IDispatch!\n"); return NULL; } //After assuring ourselves it is valid, return the IDispatch //interface to the caller. ASSERT(lpDispatch != NULL); return lpDispatch; }5.在Embed_ExcelView.h中添加如下行: void EmbedAutomateExcel(); 6.然后在Embed_ExcelView.cpp中添加EmbedAutomateExcel方法:示例代码 /******************************************************************** * This method encapsulates the process of embedding an Excel * Worksheet in a View object and automating that worksheet to add * some text to cell A1. ********************************************************************/ void CEmbed_ExcelView::EmbedAutomateExcel() { //Change the cursor so the user knows something exciting is going //on. BeginWaitCursor(); CEmbed_ExcelCntrItem* pItem = NULL; TRY { //Get the document associated with this view, and be sure it's //valid. CEmbed_ExcelDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); //Create a new item associated with this document, and be sure //it's valid. pItem = new CEmbed_ExcelCntrItem(pDoc); ASSERT_VALID(pItem); // Get Class ID for Excel sheet. // This is used in creation. CLSID clsid; if(FAILED(::CLSIDFromProgID(L"Excel.sheet",&clsid))) //Any exception will do. We just need to break out of the //TRY statement. AfxThrowMemoryException(); // Create the Excel embedded item. if(!pItem->CreateNewItem(clsid)) //Any exception will do. We just need to break out of the //TRY statement. AfxThrowMemoryException(); //Make sure the new CContainerItem is valid. ASSERT_VALID(pItem); // Launch the server to edit the item. pItem->DoVerb(OLEIVERB_SHOW, this); // As an arbitrary user interface design, this sets the // selection to the last item inserted. m_pSelection = pItem; // set selection to last inserted item pDoc->UpdateAllViews(NULL); //Query for the dispatch pointer for the embedded object. In //this case, this is the Excel worksheet. LPDISPATCH lpDisp; lpDisp = pItem->GetIDispatch(); //Add text in cell A1 of the embedded Excel sheet _Workbook wb; Worksheets wsSet; _Worksheet ws; Range range; _Application app; //set _Workbook wb to use lpDisp, the IDispatch* of the //actual workbook. wb.AttachDispatch(lpDisp); //Then get the worksheet's application. app = wb.GetApplication(); //Then get the first worksheet in the workbook wsSet = wb.GetWorksheets(); ws = wsSet.GetItem(COleVariant((short)1)); //From there, get a Range object corresponding to cell A1. range = ws.GetRange(COleVariant("A1"), COleVariant("A1")); //Fill A1 with the string "Hello, World!" range.SetValue(COleVariant("Hello, World!")); } //Here, we need to do clean up if something went wrong. CATCH(CException, e) { if (pItem != NULL) { ASSERT_VALID(pItem); pItem->Delete(); } AfxMessageBox(IDP_FAILED_TO_CREATE); } END_CATCH //Set the cursor back to normal so the user knows exciting stuff //is no longer happening. EndWaitCursor(); } 7.将下面一行添加到 Embed_ExcelView.h: #include "excel9.h"8.在Embed_ExcelView类的OnInitialUpdate()函数中加入语句:EmbedAutomateExcel();这样,当程序启动是就可以自动嵌入Excel文档了。
_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);
//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(""), vTrue, COleVariant(""),
vFalse, vFalse, vFalse, vFalse, vFalse); //Quit the application
oWordApp.Quit(vOpt, vOpt, vOpt);
http://it.sohu.com/20040809/n221441035.shtml
帮你Up 学习
1.选择多文档视图(MDI)结构,在第3步中需要选中Container,以提供容器支持。(选中ActiveX Document的选项框)2.启动ClassWizard,选Automation选项卡,选Add Class,选择From a TypeLibrary,
选中Microsoft Excel 2000 类型库,Excel9.olb,将类型库中的所有类添加到你
的工程中。3.在CntrItem.h中添加如下行:LPDISPATCH GetIDispatch(); 4.然后在CntrItem.cpp中添加GetIDispatch方法
示例代码
/*******************************************************************
* This method returns the IDispatch* for the application linked to
* this container.
*****************************************************************/
LPDISPATCH CEmbed_ExcelCntrItem::GetIDispatch()
{
//The this and m_lpObject pointers must be valid for this function
//to work correctly. The m_lpObject is the IUnknown pointer to
// this object.
ASSERT_VALID(this);
ASSERT(m_lpObject != NULL); LPUNKNOWN lpUnk = m_lpObject; //The embedded application must be running in order for the rest
//of the function to work.
Run(); //QI for the IOleLink interface of m_lpObject.
LPOLELINK lpOleLink = NULL;
if (m_lpObject->QueryInterface(IID_IOleLink,
(LPVOID FAR*)&lpOleLink) == NOERROR)
{
ASSERT(lpOleLink != NULL);
lpUnk = NULL; //Retrieve the IUnknown interface to the linked application.
if (lpOleLink->GetBoundSource(&lpUnk) != NOERROR)
{
TRACE0("Warning: Link is not connected!\n");
lpOleLink->Release();
return NULL;
}
ASSERT(lpUnk != NULL);
} //QI for the IDispatch interface of the linked application.
LPDISPATCH lpDispatch = NULL;
if (lpUnk->QueryInterface(IID_IDispatch, (LPVOID FAR*)&lpDispatch)
!=NOERROR)
{
TRACE0("Warning: does not support IDispatch!\n");
return NULL;
} //After assuring ourselves it is valid, return the IDispatch
//interface to the caller.
ASSERT(lpDispatch != NULL);
return lpDispatch;
}5.在Embed_ExcelView.h中添加如下行: void EmbedAutomateExcel(); 6.然后在Embed_ExcelView.cpp中添加EmbedAutomateExcel方法:示例代码 /********************************************************************
* This method encapsulates the process of embedding an Excel
* Worksheet in a View object and automating that worksheet to add
* some text to cell A1.
********************************************************************/
void CEmbed_ExcelView::EmbedAutomateExcel()
{
//Change the cursor so the user knows something exciting is going
//on.
BeginWaitCursor(); CEmbed_ExcelCntrItem* pItem = NULL;
TRY
{
//Get the document associated with this view, and be sure it's
//valid.
CEmbed_ExcelDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc); //Create a new item associated with this document, and be sure
//it's valid.
pItem = new CEmbed_ExcelCntrItem(pDoc);
ASSERT_VALID(pItem); // Get Class ID for Excel sheet.
// This is used in creation.
CLSID clsid;
if(FAILED(::CLSIDFromProgID(L"Excel.sheet",&clsid)))
//Any exception will do. We just need to break out of the
//TRY statement.
AfxThrowMemoryException(); // Create the Excel embedded item.
if(!pItem->CreateNewItem(clsid))
//Any exception will do. We just need to break out of the
//TRY statement.
AfxThrowMemoryException(); //Make sure the new CContainerItem is valid.
ASSERT_VALID(pItem); // Launch the server to edit the item.
pItem->DoVerb(OLEIVERB_SHOW, this); // As an arbitrary user interface design, this sets the
// selection to the last item inserted.
m_pSelection = pItem; // set selection to last inserted item
pDoc->UpdateAllViews(NULL); //Query for the dispatch pointer for the embedded object. In
//this case, this is the Excel worksheet.
LPDISPATCH lpDisp;
lpDisp = pItem->GetIDispatch(); //Add text in cell A1 of the embedded Excel sheet
_Workbook wb;
Worksheets wsSet;
_Worksheet ws;
Range range;
_Application app; //set _Workbook wb to use lpDisp, the IDispatch* of the
//actual workbook.
wb.AttachDispatch(lpDisp); //Then get the worksheet's application.
app = wb.GetApplication(); //Then get the first worksheet in the workbook
wsSet = wb.GetWorksheets();
ws = wsSet.GetItem(COleVariant((short)1)); //From there, get a Range object corresponding to cell A1.
range = ws.GetRange(COleVariant("A1"), COleVariant("A1")); //Fill A1 with the string "Hello, World!"
range.SetValue(COleVariant("Hello, World!"));
} //Here, we need to do clean up if something went wrong.
CATCH(CException, e)
{
if (pItem != NULL)
{
ASSERT_VALID(pItem);
pItem->Delete();
}
AfxMessageBox(IDP_FAILED_TO_CREATE);
}
END_CATCH //Set the cursor back to normal so the user knows exciting stuff
//is no longer happening.
EndWaitCursor();
} 7.将下面一行添加到 Embed_ExcelView.h: #include "excel9.h"8.在Embed_ExcelView类的OnInitialUpdate()函数中加入语句:EmbedAutomateExcel();这样,当程序启动是就可以自动嵌入Excel文档了。