處理常式說明的內容為:"調用後,選擇功能表項目或命令按鈕"。 單擊添加並編輯CAutomateEmbedView.cpp 檔案的代碼中插入主幹處理常式。 在方案總管中,雙擊AutomateEmbedView.cpp在代碼視窗中開啟該檔案。 鍵入或粘貼以下代碼檔案的頂部: // AutomateEmbedView.cpp : implementation of the CAutomateEmbedView class// #include "stdafx.h"#include "AutomateEmbed.h"#include "AutomateEmbedDoc.h"#include "CntrItem.h"#include "AutomateEmbedView.h"#include "CWorkbook.h"#include "CWorksheet.h"#include "CWorksheets.h"#include "CRange.h"#ifdef _DEBUG#define new DEBUG_NEW#endif// CAutomateEmbedView
在 AutomateEmbedView.h 檔案中的 CAutomateEmbedView 中加入一個新的公用成員函數: HRESULT GetDocIDispatch( LPDISPATCH* ppDisp );
在 AutomateEmbedView.cpp 檔案的底部,替換骨架的訊息處理常式的 CAutomateEmbedView::OnFileAutomateExcel 用下面的代碼: // CAutomateEmbedView message handlersvoid CAutomateEmbedView::OnFileAutomateExcel(){ // Query for the IDispatch pointer for the embedded object. // In this case it is Excel worksheet. LPDISPATCH lpDisp; HRESULT hr = GetDocIDispatch(&lpDisp); // Your own new function. // If you got an IDispatch, then use it to Automate Excel if(SUCCEEDED(hr)) { CWorkbook oBook; CWorksheets oSheets; CWorksheet oSheet; CRange oRange; // Set_Workbook oBook to use lpDisp, the IDispatch* of the // embedded/Embedded workbook. oBook.AttachDispatch(lpDisp); // Then, get the first worksheet in the workbook. oSheets = oBook.get_Worksheets(); oSheet = oSheets.get_Item(COleVariant((long)1)); // Get the Range object corresponding to Cell A1. oRange = oSheet.get_Range(COleVariant(TEXT("A1")), COleVariant(TEXT("A1"))); // Fill the range with the string "Hello World". oRange.put_Value(COleVariant((long)DISP_E_PARAMNOTFOUND, VT_ERROR), COleVariant(TEXT("Hello World"))); //NOTE: If you are automating Excel 2000 the Range.SetValue requires only one // argument. The first parameter in the Excel 2002 syntax in the line above is for the data type, // and is optional. It is not permitted by Excel 2000 or earlier versions of Excel. } // End if} // End of method/****************************************************************************** ** GetDocIDispatch - This method determines if the document is embedded ** or linked, and acquires an IDispatch pointer to the embedded/linked ** document's server application for use in Automation. ** The document must be activated for this method to succeed. ** ** Parameters: ppDisp = The address of an LPDISPATCH to be filled with ** the IDispatch pointer of the embedded/linked document's server. ** ** Returns: S_OK if successful, otherwise an HRESULT reporting the error. ** ******************************************************************************/ HRESULT CAutomateEmbedView::GetDocIDispatch(LPDISPATCH* ppDisp){ //HRESULT hr = S_OK; HRESULT hr = E_UNEXPECTED; // If no document then return no ppDisp. IOleLink* lpLink = NULL; IMoniker* lpMoniker = NULL; IRunningObjectTable* lpROT = NULL; IUnknown* lpUnk = NULL; if(!m_pSelection) { return hr; } // First, try to get an IOleLink interface from the document. // If successful, this indicates that the document is linked as // opposed to embedded. hr = m_pSelection->m_lpObject->QueryInterface(IID_IOleLink, (void**)&lpLink); if(SUCCEEDED(hr)) { // Get the moniker of the source document for this link. // You need this to find the ActiveX Document Server. hr = lpLink->GetSourceMoniker(&lpMoniker); if(SUCCEEDED(hr)) { // For linked documents, search the Running Object Table // for the relevant server. Do this through the // IRunningObjectTable interfce, which you can get through // an API call. hr = GetRunningObjectTable(0,&lpROT); if(SUCCEEDED(hr)) { // Search the Running Object Table for the ActiveX // Document Server of this document. You'll get back an // IUnknown pointer to the server. hr = lpROT->GetObject( lpMoniker, &lpUnk ); if(SUCCEEDED(hr)) { // Finally, get the IDispatch pointer from the // IUnknown pointer. hr = lpUnk->QueryInterface(IID_IDispatch, (void**)ppDisp); } } } } else { // If that fails, try for a direct IDispatch pointer. This // indicates that the document is embedded, not linked. hr = m_pSelection->m_lpObject->QueryInterface(IID_IDispatch, (void**)ppDisp); } // Clean up interface pointers you may have acquired along the way. if(lpLink) lpLink->Release(); if(lpMoniker) lpMoniker->Release(); if(lpROT) lpROT->Release(); if(lpUnk) lpUnk->Release(); return hr;} 編譯並運行該應用程式。如果您收到編譯器錯誤,請參閱"疑難解答"部分。 容器表單上單擊編輯,然後單擊插入對象。 在插入新對象列表框中,選擇一個新的 Excel 工作表。空容器中顯示的 Excel 工作表,Excel 菜單將與容器的菜單合并。 從容器中的檔案菜單上,單擊AutomateExcel。在儲存格 A1 中顯示字串"Hello World"。 在檔案菜單上,單擊建立以清除工作表。不儲存該工作表。 在新文檔中,將插入現有 Excel 活頁簿 (由檔案建立)。 在檔案菜單上,單擊AutomateExcel。"Hello World"將顯示在工作表中的儲存格 A1 中。