按照微软帮助和支持里面的方法,用MFC嵌入的word打开是乱码怎么处理
按照微软帮助和支持里面的方法,用MFC嵌入的word打开是乱码怎么办?
我按照以下步骤执行,编译通过,但是产生的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.
我按照以下步骤执行,编译通过,但是产生的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.