按照微软帮助和支持里面的方法,用MFC嵌入的word打开是乱码怎么办?

liuzhanchen1987 2011-11-25 04:52:41
我按照以下步骤执行,编译通过,但是产生的word打开是乱码。
创建示例项目
1.使用 Microsoft 可视制作室启动一个新的 MFC 应用程序向导 (exe) 项目命名 EmbedWord。在应用程序向导,在第 1 步中选择"单文档"作为应用程序类型和选择"容器"的步骤 3 中的复合文档支持类型为。您可以接受其他所有默认设置。
2.按下 CTRL + W 组合键来调用类向导。选择 自动化 选项卡,单击 添加类 按钮,然后选择 一个类型库。浏览并找到 Word 类型库。
3.确认类 对话框中选择的所有成员列出,,然后单击 确定。
4.单击 确定,再次以关闭类向导。
5.修改 EmbedWordView.cpp,以使其包括类向导从 Word 类型库生成的头文件。

Word 2002 或更高版本的 Word.
#include "msword.h"
为 Word 97 或 Word 2000
#include "msword9.h"
6.用下列替换代码中 CEmbedWordView::OnInsertObject():
void CEmbedWordView::OnInsertObject()
{
EmbedAutomateWord();
}
7.将 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();
}
8.打开 EmbedWordView.h 并将此新方法的声明添加到"实现"区域:
void EmbedAutomateWord();
9.打开 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;
}
10.打开 CntrItem.h 并将下面的声明添加到"实现"区域:
LPDISPATCH GetIDispatch();
11.在 CntrItem.cpp,更改从 CEmbedWordCntrItem::OnGetItemPosition 中代码的最后一行:
rPosition.SetRect(10, 10, 210, 210);
要:
rPosition.SetRect(20, 20, 630, 420);
12.按 F7 键以生成 EmbedWord.exe。然后按 CTRL + F5 运行应用程序的键组合。无标题-EmbedWord 框架在出现时单击在 编辑 菜单上的 插入新对象。将出现新的嵌入的 Word 文档和 Word 菜单和 命令 按钮栏与该 EmbedWord 的菜单合并应用程序。

...全文
127 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
windmissing 2012-03-01
  • 打赏
  • 举报
回复
我也遇到的这样的问题啊,同问~
liuzhanchen1987 2012-03-01
  • 打赏
  • 举报
回复
这个问题提的好!

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧