您的位置:首页 > 其它

如何嵌入并自动使用 MFC 的 Word 文档

2015-01-20 10:39 926 查看
而双击嵌入的文档以将其激活"编辑"或"打开"可以通过自动化来修改文档的嵌入在其他应用程序文档,使用嵌入的 OLE,模式。

本文演示如何嵌入和自动化的 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"


Word 97 或 Word 2000。


#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 的菜单合并应用程序。

您的代码嵌入新文档,添加至其中的文本设置字体和字体大小。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: