当前位置: 代码迷 >> 企业信息化 >> Creating Office 2000的.NET解决方案解决思路
  详细解决方案

Creating Office 2000的.NET解决方案解决思路

热度:4546   发布时间:2013-02-26 00:00:00.0
Creating Office 2000的.NET解决方案
框架安装程序
VB.NET解决方案的原代码
.NET解决方案的原代码

介绍:
通常,开发者用VBA来建立基于Office的应用程序。当.NET出现之后,大家开始用.NET来解决围绕建立文档处理的解决方案。为了满足这种需要,微软为Microsoft Office发行了Visual Studio Tools(VSTO),添加到Visual Studio .NET 2003中。以便建立管理Word 2003 或 Excel 2003的应用程序。
Office 2003 的Visual Studio Tools(VSTO)是一个伟大的工具用来用.NET建立 Office的解决方案。但遗憾的是Office 2000的用户不能使用该工具。这里就是尝试填补这个缺陷。这里将介绍怎样用Visual Studio .NET 2003建立Office 2000解决方法。我常识将代码尽量贴近VSTO 2003以便轻松的移植到Office 2003。
开始:
首先介绍如果在 Visual Basic .NET中建立一个Word 程序。我们将建立一个程序包含可以显示Word文档的窗口框架。
打开Visual Studio .NET,在File | New |Project中,选择项目类库摸板如下:

Use Project| Add References menu. Chose the COM tab and add a reference to Microsoft Word 9.0 object library.
使用菜单中Project| Add。选择COM并添加一个Microsoft Word 9.0对象库
 CollapseImports Word
   
Public Class OfficeCodeBehind
  Friend WithEvents ThisDocument As Word.Document
  Friend WithEvents ThisApplication As Word.Application

#Region "Initialization code"
  ' Default constructor.
  Public Sub New()
  End Sub
  Public Sub Startup(ByVal application As Object, ByVal document As Object)
  ThisApplication = CType(application, Word.Application)
  ThisDocument = CType(document, Word.Document)
  End Sub
   
  Public Sub Shutdown()
  ThisApplication = Nothing
  ThisDocument = Nothing
  End Sub
   
  Private Sub ThisApplication_DocumentOpen(ByVal Doc As Word.Document) 
  Handles ThisApplication.DocumentOpen
  MsgBox("Application event Document open")
  End Sub

  Private Sub ThisDocument_New() Handles ThisDocument.New
  MsgBox("inside Document new managed code")
  End Sub
#End Region

End Class


建立一个OfficeCodeBehind类。建立变量和主文件对象。
建立引入代码有开始和结束指令。开始指令在打开一个WORD文档是在引入ThisApplication 和 ThisDocument时调用,结束指令在关闭WORD文档并做清理时调用。另外,建立事件句柄在基本事件和向文档中写入字符是使用。
建立窗口框架调用OfficeCodeBehind类的WriteStringToWordDocument来想WORD中写入字符。
打开WORD,在菜单 Tools | Macro选择 Visual Basic Editor。通过选择Tools|References点浏览按钮来添加一个参量到WordTest1.tlb文件,如下:


Dim app As New WordTest1.OfficeCodeBehind

Private Sub Document_Close()
  app.ShutDown
End Sub

Private Sub Document_Open()
  app.Startup Me.Application, Me
End Sub

添加以上代码到Visual Basic编辑器中。
以上代码是将WORD文档和.NET的类库关联的工具。当打开了一个WORD文档时,它激活了Document_Open事件,通过调用OfficeCodeBehind的StartUp方法。启动了一个参量到程序(Word.Application)和一个到文档(Word.Document)的参量。Startup方法拷贝信息到本地变量中和钩住事件句柄。
Shutdown方法被调用当文档被清除后。
用Word 和 Excel项目摸板建立Office项目:
注意:请阅读ProjectTemplateSetup.rtf 中的指令来建立项目摸板
打开Visual Studio .NET用File | New | Project中,选择Word Document project摸板如下:


‘当文档被打开时调用
Private Sub ThisDocument_Open() Handles ThisDocument.Open
  MsgBox("Document Open from Managed code")
End Sub

Add the above code in the Code Editor. Build the project. Follow the same steps outlined for the Class Library project above to hook up the Word Document to the Word Project we created in .NET.

添加以上代码,编译项目,以下步奏体现了如果使.NET的类库项目关联WORD文档

------解决方案--------------------------------------------------------
接分是王道!
  相关解决方案