Handler Descriptionis: "After the call, select the menu item or command button." ClickAdd and editInsert the skeleton handler in the code for the CAutomateEmbedView.cpp file. InSolution Explorer, double-clickAutomateEmbedView.cppOpen the file in the Code window. Type or paste the top of the following code file: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
Add a new public member function to the Cautomateembedview in the AutomateEmbedView.h file:HRESULT Getdocidispatch (lpdispatch* ppdisp);
At the bottom of the AutomateEmbedView.cpp file, replace the skeleton's message handler Cautomateembedview::onfileautomateexcel with the following code:Cautomateembedview message handlers void Cautomateembedview::onfileautomateexcel () {//Query for the IDispatch poi
Nter for the embedded object.
In the It is Excel worksheet.
Lpdispatch Lpdisp; HRESULT hr = Getdocidispatch (&LPDISP);
Your own new function.
If you got a 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 is automating Excel the Range.setvalue requires only one//argument. The first parameter in the Excel 2002 syntax with the line above are for the data type,//and is optional.
It is not a permitted by Excel or earlier versions of Excel. }//End If}//End of Method/***************************************************************************** * * * Getdocidispatch-this method determines if the docu ment is embedded * * or linked, and acquires a IDispatch pointer to the embedded/linked * * document ' s Serv ER application for use in Automation. * * The document must is activated for this method to succeed. * * * * * Parameters:ppdisp = the address of an Lpdispatch to IS 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 a Iolelink interface from the document.
If successful, this indicates then 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 S Erver.
Do this through the//IRunningObjectTable INTERFCE, which you can get through//a 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 A//IUnknown pointer to the server.
hr = Lprot->getobject (Lpmoniker, &lpunk); if (SUCCEEDED (HR)) {//Finally, get the IDispatch pointer from the//IUnknown P
Ointer. hr = Lpunk->queryinterface (IID_IDispatch, (void**) ppdiSP); }}}} else {//If-fails, try for a direct IDispatch pointer.
This//indicates the document is embedded, not linked.
hr = M_pselection->m_lpobject->queryinterface (IID_IDispatch, (void**) ppdisp);
}//Clean up interface pointers your may have acquired along the.
if (Lplink) lplink->release ();
if (lpmoniker) lpmoniker->release ();
if (Lprot) lprot->release ();
if (lpunk) lpunk->release ();
return HR;
}
Compile and run the application. If you receive a compiler error, see the "Troubleshooting" section. On the container form, clickEdit, and then clickinserting Objects。 InInsert New ObjectIn the list box, select a new Excel worksheet. An Excel worksheet is displayed in an empty container, and the Excel menu is merged with the container's menu. From the container in thefilemenu, clickAutomateexcel。 The string "Hello World" is displayed in cell A1. Infilemenu, clickNewTo clear the worksheet. The worksheet is not saved. In a new document, you insert an existing Excel workbook (created by file)。 Infilemenu, clickAutomateexcel。 "Hello World" appears in cell A1 on the worksheet.