How to automatically use the embedded Office ActiveX document for C + +

Source: Internet
Author: User

Links embedded in the VC for Excel

http://support.microsoft.com/kb/307473

Http://support.microsoft.com/kb/311546/en-us

Problem-solving Links

http://support.microsoft.com/kb/316587

This stepwise guide can be used to automate the implementation of embedded Microsoft Office documents. Follow the procedure below to use the VC + + MFC container as an active Excel worksheet for an ActiveX document container. The code contains a sample method that gets the document's server to a IDispatch interface pointer, and shows how to automatically embed an Excel worksheet.

using an Office application as an ActiveX document serverThe ActiveX document container can contain multiple Office documents, but you can display one document at a time. When you activate an Office document, the menu for the Office document's server application is merged into the container's menu. The ActiveX document container automatically activates the displayed document. This is different from regular OLE document embedding. Traditional embedding or linking requires end users to merge menu-activated documents before they are merged.

When embedded in a container in a "new" worksheet, it is treated as an ActiveX object. Any end-user action does not require the Excel menu to be merged with the container's menu.

Create an MFC container application to automatically execute ActiveX documentsTo build a sample application that can automatically embed an EXCEL worksheet, follow these steps: Start Microsoft Visual Studio.NET. Infilemenu, point toNew, and then clickProject。 InProject TypeUnder ClickVisual C + + projects, and selectMFC ApplicationsTemplate.automateembedName the project. Save the item to the C:\...root folder. In the MFC Application Wizard, follow these steps: clickApplication Type, and then selectSingle Document。 ClickCompound document Support, and then selectContainer。 CheckActive Document Container。 ClickCompleteAccept the remaining default settings. Add an interface from the Excel object library. To do this, follow these steps:Projectmenu, clickAdd Class。 From the list of templates, selectMFC classes from the type library, and then clickOpen。 The Add Class from Type Library wizard appears. In the list of available type libraries, locateMicrosoft excelversion Object Library, where version is 9.0 Excel 2000 or Excel 2002 of 10.0. Add the following interface: _application
_workbook
_worksheet
Range
Sheet Click Finish. InSolution SolutionsSolution for Resource ManagerautomateembedArea, you will see the tree view, which includes the following:

Source code files
Header file
Resource file expansionresource Filenode, and then double-clickautomateembed.rcTo open it. Double-click the two menu you want to view.MenuIdr_cntr_inplaceAndIdr_mainframe。 Double clickIdr_cntr_inplace。 The graphic will openMenu Designerwindow, displayed infileMenu. Bottom offileThe menu is a blank CommandBarButton containsEnter hereThe legend. Title Type Automateexcel. Right-click the new heading in CommandBarButton, and then clickTo add an event handlerRun the event handler wizard.

   Set this:
   ---------------------------------------------------------

   Command Name               id_ File_automateexcel
   Message Type               Command
   Function Handler Name      onfileautomateexcel
   Class List                 Cautomateembedview

						
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.