如何在VC中使用Word组件?

victorydsk 2004-06-09 10:36:59
想问一下如何在VC中使用Word组件,比如调用Word模板并向其中添加信息,最好能在一个自己生成的窗口中显示Word内容,谢谢!
...全文
305 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lixiaosan 2004-06-09
  • 打赏
  • 举报
回复
自己研究这个问题。。Q238611

http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/q238/6/11.asp&NoWebContent=1
xiaoqiqixiao 2004-06-09
  • 打赏
  • 举报
回复
首先在ClassWizard的Add Class-From a type library中选择mswordx.olb(x是版本号),然后初始化COM服务(CoInitialize(NULL)),然后做你的事了
三言两语说不清的,建议自己找找资料,翻翻书
zhucde 2004-06-09
  • 打赏
  • 举报
回复
void EmbedAutomateWord();
Open CntrItem.cpp and add a new CEmbedWordCntrItem::GetIDispatch member function:



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;
}
Open CntrItem.h and add the following declaration to the "implementation" region:



LPDISPATCH GetIDispatch();

In CntrItem.cpp, change the last line of code in CEmbedWordCntrItem::OnGetItemPosition from:



rPosition.SetRect(10, 10, 210, 210);
to:

rPosition.SetRect(20, 20, 630, 420);
Press the F7 key to build EmbedWord.exe. Then press the CTRL+F5 key combination to run the application. When the frame Untitled - EmbedWord appears, click Insert New Object on the Edit menu. The new embedded Word document appears, and the Word menu and Command button bar are merged with the menu of the EmbedWord application.

Your code embedded the new document, added text to it, and set the typeface and font size.


(c) Microsoft Corporation 1999, All Rights Reserved. Contributions by Chris Jensen, Microsoft Corporation.





REFERENCES
For additional information about automating an embedding Office document, please click the article number below to view the article in the Microsoft Knowledge Base:

Q184663 HOWTO: Embed and Automate a Microsoft Excel Worksheet with MFC

(c) Microsoft Corporation 1999, All Rights Reserved. Contributions by Chris Jensen, Microsoft Corporation.



Additional query words:

Keywords : kbole kbMFC kbVC500 kbVC600 kbWord kbGrpDSO kbDSupport kbWord97 kbword2000
Issue type : kbhowto
Technology : kbVCsearch kbWordSearch kbAudDeveloper kbWord2000Search kbWord2000 kbWord2002 kbWord2002Search kbWord97 kbWord97Search kbZNotKeyword2 kbVC32bitSearch kbVCPE500 kbVCPE600

如果嫌麻烦我这边有做好的
zhucde 2004-06-09
  • 打赏
  • 举报
回复
见MSDN:Q238611


HOWTO: Embed and Automate a Word Document with MFC




--------------------------------------------------------------------------------
The information in this article applies to:

Microsoft Word 2002
Microsoft Word 2000
Microsoft Word 97 for Windows
Microsoft Visual C++, 32-bit Professional Edition, versions 5.0, 6.0

--------------------------------------------------------------------------------


SUMMARY
Documents that are embedded in other application documents, using OLE embedding, can be modified by automation without double-clicking the embedded document to activate it in "edit" or "open"' mode.

This article demonstrates how to embed and automate a Microsoft Word 2000 document in an MFC Single Document Interface application. The same approach works with a Microsoft Word 97 document. The distinction is not which version of Word created the document, but rather, which version of Word you use in the automation process.

The difference is the use of the Word 2000 type library instead of that of Word 97. The different files containing the two different type libraries are MSWord9.olb for Microsoft Word 2000, and MSWord8.olb for Word 97. The default location for either file is ...\Program Files\Microsoft Office\Office.

(The default location for the type library for Word 2002 is C:\Program Files\Microsoft Office\Office10\msword.olb)



MORE INFORMATION

Create a Sample Project
Using Microsoft Visual Studio, start a new MFC AppWizard (exe) project named EmbedWord. In AppWizard, choose "Single Document" as the application type in step 1 and choose "Container" as the type of compound document support in step 3. You can accept all other defaults.


Press the CTRL+W key combination to invoke the Class Wizard. Select the Automation tab. Click the Add Class button and choose From a Type Library. Browse to locate the Microsoft Word 2000 type library (MSWord9.olb) or the Word 2002 type library (MSWord.olb).


In the Confirm Classes dialog box, select all of the members listed, and click OK.


Click OK again to close ClassWizard.


Modify EmbedWordView.cpp so that it includes the header file ClassWizard generated from the Word type library:



#include "msword9.h" // or #include "msword.h" for Word 2002
Replace the code in CEmbedWordView::OnInsertObject() with the following:



void CEmbedWordView::OnInsertObject()
{
EmbedAutomateWord();
}
Add a CEmbedWordView::EmbedAutomateWord() member function to 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();
}
Open EmbedWordView.h and add the declaration of this new method to the "implementation" region:



liuxianzhi 2004-06-09
  • 打赏
  • 举报
回复
你阅读MSDN (October 2001)文章:
Q184663 HOWTO: Embed and Automate a Microsoft Excel worksheet with MFC
Q179706 HOWTO: Use MFC to Automate Excel & Create/Format a New Workbook
Q186120 HOWTO: Use MFC to Automate Excel and Fill a Range with an Array
Q186427 HOWTO: Catch Microsoft Excel 97 Application Events Using VC++

会对你有帮助。
如果要在你的程序中嵌入word等,可以使用htmlview实现。

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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