如何自動使用 c + + 的嵌入的 Office ActiveX 文檔

來源:互聯網
上載者:User

vc中嵌入excel的連結

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

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

解決問題的連結

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

本逐步式指南可用於自動執行嵌入的 Microsoft Office 文檔。 按照下面的過程使用 VC + + MFC 容器作為 ActiveX 文檔容器已啟用的 Excel 工作表。 該程式碼封裝含樣本擷取文檔的伺服器到一個 IDispatch 介面指標的方法,並示範如何自動嵌入的 Excel 工作表。

使用 Office 應用程式作為 ActiveX 文檔伺服器 ActiveX 文檔容器可以包含多個 Office 文檔中,但可以一次顯示一個文檔。 當啟用 Office 文檔時,Office 文檔的伺服器應用程式的菜單合并到容器的菜單中。 ActiveX 文檔容器自動啟用所顯示的文檔。 這不同於常規的 OLE 文檔嵌入。 傳統的嵌入或連結要求終端使用者之前合并菜單啟用的文檔。

"新"的工作表中嵌入在容器中時則將它視為一個 ActiveX 對象。 任何終端使用者操作不需要 Excel 菜單與容器的菜單合并在一起。

建立 MFC 容器應用程式自動執行 ActiveX 文檔 若要產生應用程式範例,可以自動嵌入的 Excel 工作表,請執行以下步驟: 啟動 Microsoft Visual Studio.NET。在檔案菜單上,指向建立,然後單擊項目。在項目類型下單擊Visual C++ 項目,,並選擇MFC 應用程式模板。AutomateEmbed為項目命名。對 C:\...root 檔案夾中儲存該項目。 在 MFC 應用程式嚮導,請按照下列步驟: 單擊應用程式類型,然後選擇單個文檔。 單擊複合文檔支援,然後選擇容器。 檢查使用中文件容器。 單擊完成接受剩餘的預設設定。 從 Excel 物件程式庫添加介面。若要執行此操作,請按照下列步驟操作: 在項目菜單上,單擊添加類。 從模板列表中,選擇從類型庫的 MFC 類,然後單擊開啟。此時將顯示從類型庫添加類嚮導。 在可用的類型庫的列表中,找到Microsoft Excelversion物件程式庫,其中version是 9.0 Excel 2000 或 Excel 2002 的 10.0。 添加下面的介面: _Application
_Workbook
_Worksheet
範圍
工作表 單擊完成。 在解決方案資源管理員的解決方案AutomateEmbed地區中,您將看到的樹視圖,其中包括以下:

原始碼檔案
標頭檔
資源檔 展開資源檔節點,然後雙擊AutomateEmbed.RC以將其開啟。 雙擊要查看的兩個菜單的菜單IDR_CNTR_INPLACEIDR_MAINFRAME。 雙擊 IDR_CNTR_INPLACE。將開啟圖形 菜單設計器 視窗,顯示在 檔案 菜單。底部的 檔案 菜單是一個空白 CommandBarButton 包含 此處輸入 的圖例。標題鍵入AutomateExcel。 用滑鼠右鍵單擊新標題為 CommandBarButton 中,然後單擊添加事件處理常式運行事件處理常式嚮導。

   Set this:                  To this:   ---------------------------------------------------------   Command Name               ID_FILE_AUTOMATEEXCEL   Message Type               Command   Function Handler Name      OnFileAutomateExcel   Class List                 CAutomateEmbedView
處理常式說明的內容為:"調用後,選擇功能表項目或命令按鈕"。 單擊添加並編輯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 中。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.