本文演示如何嵌入和自動化的 MFC 單文檔界面應用程序中的 Microsoft Office Word 文檔。相同的方法適用於任何 Word 版本。不同之處在於不是什麼版本的 Word 創建的文檔中,但相反,哪個版本的 Word 中使用的自動化過程。
Word 類型庫,如下所示:
- 對於 Microsoft Office Word 2007 中,類型庫是 Msword.olb,且位於"C:\Program 數值 Office\Office12"文件夾中。
- 對於 Microsoft Office Word 2003,類型庫是 Msword.olb,且位於"C:\Program 數值 Office\Office11"文件夾中。
- 對於 Microsoft Word 2002,類型庫是 Msword.olb,且位於"C:\Program 數值 Office\Office10"文件夾中。
- 對於 Microsoft Word 2000 中,類型庫是 Msword9.olb,且位於"C:\Program 數值網絡"文件夾中。
- 對於 Microsoft Word 97 中,類型庫是 Msword8.olb,且位於"C:\Program 數值網絡"文件夾中
創建示例項目
- 使用 Microsoft Visual Studio,啓動一個名爲EmbedWord的新 MFC 應用程序嚮導 (exe) 項目。在應用程序嚮導,在步驟 1 中選擇"單文檔"作爲應用程序類型,並選擇"容器"的步驟 3 中的複合文檔支持類型。您可以接受其他所有默認設置。
- 請按 CTRL + W 組合鍵以調用類嚮導。選擇自動化選項單擊添加類按鈕,然後選擇從類型庫。瀏覽以查找 Word 類型庫。
- 在確認類對話框中,選擇所有列出的成員,然後單擊確定。
- 再次單擊確定以關閉類嚮導。
- 修改 EmbedWordView.cpp,使它包括類嚮導從 Word 類型庫生成的頭文件。
Word 2002 或更高版本的版本的 Word。#include "msword.h"
#include "msword9.h"
- CEmbedWordView::OnInsertObject()中的代碼替換爲以下:
void CEmbedWordView::OnInsertObject() { EmbedAutomateWord(); }
- 將CEmbedWordView::EmbedAutomateWord()成員函數添加到 EmbedWordView.cpp 中:
void CEmbedWordView::EmbedAutomateWord() { /******************************************************************* This method encapsulates the process of embedding a Word document in a View object and automating that document to add text. *******************************************************************/ //Change the cursor so the user knows something exciting is going //on. BeginWaitCursor(); CEmbedWordCntrItem* pItem = NULL; TRY { //Get the document associated with this view, and be sure it's //valid. CEmbedWordDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); //Create a new item associated with this document, and be sure //it's valid. pItem = new CEmbedWordCntrItem(pDoc); ASSERT_VALID(pItem); // Get Class ID for Word document. // This is used in creation. CLSID clsid; if(FAILED(::CLSIDFromProgID(L"Word.Document",&clsid))) //Any exception will do. You just need to break out of the //TRY statement. AfxThrowMemoryException(); // Create the Word embedded item. if(!pItem->CreateNewItem(clsid)) //Any exception will do. You just need to break out of the //TRY statement. AfxThrowMemoryException(); //Make sure the new CContainerItem is valid. ASSERT_VALID(pItem); // Launch the server to edit the item. pItem->DoVerb(OLEIVERB_SHOW, this); // As an arbitrary user interface design, this sets the // selection to the last item inserted. m_pSelection = pItem; // set selection to last inserted item pDoc->UpdateAllViews(NULL); //Query for the dispatch pointer for the embedded object. In //this case, this is the Word document. LPDISPATCH lpDisp; lpDisp = pItem->GetIDispatch(); //Add text to the first line of the document _Document doc; Selection selection; _Application app; PageSetup pagesetup; _Font font; //set _Document doc to use lpDisp, the IDispatch* of the //actual document. doc.AttachDispatch(lpDisp); //Then get the document's application object reference. app = doc.GetApplication(); // From there, get a Selection object for the insertion point. selection = app.GetSelection(); selection.SetText( "This is a good place to say \"Hello World\""); // Automate setting the values for various properties. font = selection.GetFont(); font.SetName("Tahoma"); font.SetSize(16); selection.SetFont(font); } //Here, you need to do clean up if something went wrong. CATCH(CException, e) { if (pItem != NULL) { ASSERT_VALID(pItem); pItem->Delete(); } AfxMessageBox(IDP_FAILED_TO_CREATE); } END_CATCH //Set the cursor back to normal so the user knows exciting stuff //is no longer happening. EndWaitCursor(); }
- 打開 EmbedWordView.h 並添加到"實現"區域的這一新方法的聲明:
void EmbedAutomateWord();
- 打開 CntrItem.cpp 並添加一個新的CEmbedWordCntrItem::GetIDispatch成員函數:
LPDISPATCH CEmbedWordCntrItem::GetIDispatch() { /**************************************************************** This method returns the IDispatch* for the application linked to this container. *****************************************************************/ //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 assuring yourself that it is valid, return the IDispatch //interface to the caller. ASSERT(lpDispatch != NULL); return lpDispatch; }
- 打開 CntrItem.h,然後將下面的聲明添加到"實現"區域:
LPDISPATCH GetIDispatch();
- 在 CntrItem.cpp,將更改從CEmbedWordCntrItem::OnGetItemPosition中的代碼的最後一行:
rPosition.SetRect(10, 10, 210, 210);
rPosition.SetRect(20, 20, 630, 420);
- 按 F7 鍵以生成 EmbedWord.exe。然後按 CTRL + F5 運行應用程序的鍵組合。無標題-EmbedWord 框架在出現時單擊在編輯 菜單上的 插入新對象。將出現新的嵌入的
Word 文檔和 Word 菜單和 命令 按鈕欄與該 EmbedWord 的菜單合併應用程序。
您的代碼嵌入新文檔,添加至其中的文本設置字體和字體大小。