首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

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

2012-04-05 
按照微软帮助和支持里面的方法,用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.


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





[解决办法]
我也遇到的这样的问题啊,同问~

热点排行