精通
英语
和
开源
,
擅长
开发
与
培训
,
胸怀四海
第一信赖
锐英源精品开源心得,转载请注明:“锐英源www.wisestudy.cn,孙老师作品,电话13803810136。需要全文内容也请联系孙老师。
Once I was engaged in a project whose main features were the presence of a great amount of the typical forms of input and output generally associated with Office-type applications. The documents were to be filled from a data store, which the program would display via the use of views. It would be extremely desirable that the document template could understand these different data independently. Therefore, we made the decision to integrate Microsoft Office into our application in order to leverage the already built-in functionalty that we were seeking.
有一次我参与一个项目,其主要特征是存在大量与办公类型相关联的典型的输入和输出应用程序。填充文档的数据来自于一个数据存储,该程序会通过使用视图显示。非常理想的是,文档模板可以理解这些不同的数据。因此,我们决定将Microsoft Office集成到我们的应用程序中,以便于利用我们正在寻求的内置功能。
To that extent, I present to you this article detailing exactly what steps are necessary in order to integrate Microsoft Office into your Visual C++/MFC applications. 在一定程度上,,我给你的这篇文章讲述了怎样把Microsoft Office集成到你的Visual c++ / MFC应用程序中的详细步骤。
I should also note at this point that one constant problem that I have run into with regards to Office is the unstable nature of the product itself when using it in relation to an ActiveX Document. Therefore, as you look through this article (and the demo) you might see some less than efficient code. An example of a problem that I still have is that even after my application ends, Microsoft Office application remains in memory and can only be removed via the Task manager. If you do solve this problem, please let me know so that I can update this article as well as my own code.
我也注意在这一点,我曾遇到过当有关的ActiveX文档使用它的时候,关于产品本身的不稳定性的问题。因此,当你浏览这篇文章(演示)时,你可能会看到一些不是特别高效的代码。我还有一个问题,即使我的应用程序结束,Microsoft Office应用程序仍在内存中,只能通过任务管理器删除。如果你能解决这个问题,请告诉我,这样我可以更新这篇文章以及我自己的代码。
Let's begin. 让我们开始吧。
1.Via the Visual C++ AppWizard, generate a new MDI application called XOffice. On the third step, it will be necessary to select the Container radio button as well as the Active Document Container checkbox. 通过Visual c++ AppWizard,生成一个新的MDI应用程序命名为XOffice 。第三步中,要选择容器单选按钮以及活动文档容器复选框。
2.In addition, this application should be an Automation server. I have taken advantage of a way offered by Nick Hodapp in his article, Using ATL to Automate a MFC Application, which can be found here at CodeGuru. Please familiarize yourself with this example and execute all steps of program transformation to the automation server. 此外,这个应用程序应该是一个自动化服务器。我有利用Nick Hodapp在他的文章中提供的方式,使用ATL自动化MFC应用程序,这在CodeGuru可以找到。请熟悉这个例子并执行所有的步骤。
3.Now we shall engage in integrating MS Office. Include a file Office.h with the contents into the project: 现在我们应当参与集成MS Office。包括项目中Office.h文件的内容:
<FONT color=green>// Office.h</FONT> <FONT color=blue>#define</FONT> Uses_MSO2000 <FONT color=blue>#ifdef</FONT> Uses_MSO2000 <FONT color=green>// for MS Office 2000</FONT> <FONT color=blue>#import </FONT>
We shall create a new form, which we shall require further. Select the New Form option from the Insert menu. 我们将创建一个新的形式,我们需要更进一步。从插入菜单中新建窗体选项。
Enter CFormDemo into a field Name. Press the button New located near a Document field and then click the OK button, in the newly appeared form. And once again click the OK button. 输入CFormDemo到字段名里,按下位于文档字段的新建按钮,然后单击OK按钮,会出现新的表单,再一次单击OK按钮。
4.<!--#include virtual='/script/competitions/whereskent/kentface.asp'> -->We shall place three Edit Boxes and two Buttons on the new form. In ClassWizard we shall bind Edit Boxes to variables (accordingly CString m_str, double m_double, long m_long). 我们将三个编辑框和两个按钮放到一个新的表单中,在ClassWizard中,我们给编辑框绑定变量。
5.We shall create handler of the following kind for Buttons: 我们将创建下列用于按钮一种的处理程序
<FONT color=green>// FormDemo.cpp</FONT> <FONT color=blue>void </FONT>
We shall add the following code to the beginning of XOfficeDoc.cpp file: 我们将下面的代码添加到XOfficeDoc.cpp文件的开头:
<FONT color=green>// XOfficeDoc.cpp</FONT> <FONT color=blue>static </FONT>
Now we are able to create MDI documents by pressing the buttons on the form. 现在我们可以通过表单上的按钮创建MDI文档。
6.The MFC class ColeDocObjectItem provides the support for ActiveX documents. The class can do a lot already, but we also need to teach it to load the documents, which we set up. MFC中的类ColeDocObjectItem支持ActiveX文档。这个类可以做很多事情了,但是我们还需要教它加载文档,
Make the following change into a class CXOfficeCntrItem: 变成一个CXOfficeCntrItem类,需要进行以下更改:
<FONT color=green>// CntrItem.h</FONT>
<FONT color=blue>class</FONT>
<FONT color=green>// CntrItem.cpp</FONT>
CXOfficeCntrItem::CXOfficeCntrItem(CXOfficeDoc* pContainer,LPCTSTR templ:
COleDocObjectItem(pContainer), m_isCreate(<FONT color=blue>false</FONT>
7.And the last thing to do is to make changes in CXOfficeDoc and CXOfficeView classes for ActiveX document display. 而做的最后一件事是使CXOfficeDoc和ActiveX文档显示更改CXOfficeView类
<FONT color=green>// XOfficeDoc.h</FONT> ... <FONT color=blue>class</FONT> <FONT color=green>// XOfficeDoc.cpp</FONT> CXOfficeDoc::CXOfficeDoc() : m_ctrl(0) { EnableCompoundFile(); } BOOL CXOfficeDoc::OnNewDocument() { <FONT color=blue> if </FONT> <FONT color=green>// XOfficeView.cpp</FONT> <FONT color=blue>void </FONT>
So, now we are able to load ActiveX documents automatically. It is quite not bad already. 所以,现在我们能够自动加载ActiveX文档。已经相当不错了。
Pay attention to the fact that the procedures of preservation and loading of our documents work normally too, saving thus the contents of the initial template document. The truth is that we don't need it at all. The only thing that does not work is Print Preview. I was not able to understand it therefore, if someone manages to do it I shall be very obliged to find out about it first. 注意程序的保存和加载的文档工作量太大,从而节省了初始模板文档中的内容。事实是,我们并不需要它。唯一不工作是打印预览,我没能理解它,因此,如果有人能做到,我将感激不尽
8.Now we shall teach CXOfficeDoc class to store and to load only our data and not to ask questions about any data change of ActiveX document itself. For this purpose we shall add methods of OnOpenDocument and SaveModified and bring in the following changes with the help of ClassWizard: 现在,我们将教CXOffice文件级存储和只装载我们的数据,而不是询问有关ActiveX文档本身的任何数据变化的问题。为此我们将添加方法OnOpenDocument,在SaveModified和ClassWizard帮助下,做了以下更改:
<FONT color=green>// XOfficeDoc.cpp</FONT> <FONT color=blue>void </FONT> <FONT color=green>// CntrItem.cpp</FONT> <FONT color=blue>void </FONT>
9.The following step shall be the reception of the IDispatch interface of ActiveX document. Let's bring in the following changes to CXOfficeCntrItem class: 接下来的步骤应是ActiveX文档的IDispatch接口的接收。让我们携带CXOfficeCntrItem类做以下更改:
<FONT color=green>// CntrItem.h</FONT> ... <FONT color=blue>#include </FONT>
I did not want to present the text of the appropriate methods, as it would have taken too much space. They can be looked up in the source texts of the program. It shall be necessary also to bring in the alterations to CXOfficeDoc and CXOfficeView classes listed below. 我不想提出适当的方法文本,因为它会耗费太多空间。可以在程序的源文本进行查找。下面列出的在改建CXOfficeDoc和CXOfficeView类中也是必要的。
<FONT color=green>// CXOfficeView.cpp</FONT> <FONT color=blue>void </FONT> <FONT color=green>// CXOfficeDoc.cpp</FONT> <FONT color=blue>void </FONT>
10.Now it's high time to make our automation server intelligent. For this purpose we shall define ActiveDocument and IsActiveDocument properties for codeb>IApplication interface and also PStr, PDouble and PLong properties for IDocument interface. Is easy to do with the help of ATL Wizard. 现在是时候让我们的自动服务器智能化。为此我们将定义ActiveDocument和IsActiveDocument属性codeb > IApplication接口和PStr PDouble和IDocument PLong属性界面。很容易的帮助ATL向导。
Workspace->Class View->IApplication->Right button->Add Property 工作区- >类视图- > IApplication - >右按钮- >添加属性
Workspace->Class View->IDocument->Right button->Add Property 工作区- >类视图- > IDocument - >右按钮- >添加属性
The realization of methods can be looked up in the source texts. 实现的方法可以在源文本中找到。
11.The files XOffice.doc and XOffice.xls are the examples of Word and Excel documents. In Word the document initialization of field occurs in event Document_New, which is obviously called from the program. The value of fields is given to the named bookmarks. In Excel the document initialization of cells is made in event Workbook_Activate. It is not quite convenient, but I have tried a good deal of variants and have defined this one (Workbook_Activate) as the best and most stable. As I have already said, the direct call of macros from VBA leaves Excel in memory even when the program is finished.
文件Office.doc和XOffice.xls是Word和Excel文档的例子。在word文档初始化字段出现在的Document_New事件,显然是从从程序调用的。字段的值指定书签。在Excel文档初始化的是Workbook_Activate事件制造的。这是不是很方便,但是我尝试过大量的变异,定义了这个(Workbook_Activate)是最好的、最稳定。我已经说过了,即使程序完成,在内存中还在执行。