microsoft.com: How to embed and automate an Excel worksheet by using MFC and Visual C++ 2005

microsoft.com: How to embed and automate an Excel worksheet by using MFC and Visual C++ 2005

This step-by-step article describes how to embed an Excel worksheet in a View object in a single-document interface (SDI) Microsoft Foundation Classes (MFC) application by using Visual C++ 2005 or Visual C++ .NET.
Create an MFC Application that Embeds an Excel Worksheet

The following steps describe how to embed a worksheet and automate the worksheet to add data to a cell.
Start Microsoft Visual Studio .NET. On the File menu, click New, and then click Project. Under Project types click Visual C++ Projects, and then click MFC Application under Templates. Name the project Embed_Excel.

Note In Visual C++ 2005, click Visual C++ instead of Visual C++ Projects.
When the MFC Application Wizard appears, follow these steps:
Click Application Type and then select Single Document.
Click Compound Document Support and then select Container.
Click Finish to accept all other default settings.
Add interfaces from the Excel object library. To do this, follow these steps:
On the Project menu, click Add Class.
Select MFC Class From TypeLib from the list of templates and click Open. The Add Class From TypeLib Wizard appears.

Note In Visual C++ 2005, click Add instead of Open.
In the list of available type libraries, locate Microsoft Excel version Object Library. For Excel 2000, the version is 9.0, for Excel 2002, the version is 10.0, and for Microsoft Office Excel 2003, the version is 11.0.
Add the following interfaces:
_Application
_Workbook
_Worksheet
Range
Workbooks
Worksheets
Click Finish.
Add the following line to Cntritem.h as a public member function of the CEmbed_ExcelCntrItem class:
LPDISPATCH GetIDispatch();
Add the GetIDispatch method to Cntritem.cpp, as follows:

/*******************************************************************
*   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 you verify that it is valid, return the IDispatch
	//interface to the caller.
	ASSERT(lpDispatch != NULL);
	return lpDispatch;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s