下面的文章將一步一步演示如何在VB.NET應用中添加MS word。
在VB.NET應用中添加Word組件
打開Visual Studio并新建一個VB.NET應用,右鍵點擊HostOffice Solution,然后點擊Add Reference...菜單項。

在彈出對話框的Browse選項卡中選擇officeviewer.ocx文件。

或從COM選項卡中選擇Edraw Office Viewer Component組件。
單擊OK。將Edraw Office Viewer Component組件引用添加到新的vb.net項目中。

切換到Form1窗體設計窗口。

將Edraw Office Viewer Component組件從工具箱面板拖放到form1中。

為新建、打開、另存,關閉和打印word文檔添加如下所示的VB.NET代碼:
Public Class Form1
Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System
.EventArgs) Handles btnNew.Click
If Dialog1.ShowDialog() Then
If Dialog1.GetChooseType() = 1 Then
AxEDOffice1.CreateNew("Word.Application")
ElseIf Dialog1.GetChooseType() = 2 Then
AxEDOffice1.CreateNew("Excel.Application")
ElseIf Dialog1.GetChooseType() = 3 Then
AxEDOffice1.CreateNew("PowerPoint.Application")
ElseIf Dialog1.GetChooseType() = 4 Then
AxEDOffice1.CreateNew("Visio.Application")
ElseIf Dialog1.GetChooseType() = 5 Then
AxEDOffice1.CreateNew("MSProject.Application")
End If
End If
End Sub
Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System
.EventArgs) Handles btnOpen.Click
AxEDOffice1.OpenFileDialog()
End Sub
Private Sub btnSaveAs_Click(ByVal sender As System.Object, ByVal e As System
.EventArgs) Handles btnSaveAs.Click
AxEDOffice1.SaveFileDialog()
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System
.EventArgs) Handles btnClose.Click
AxEDOffice1.CloseDoc()
End Sub
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System
.EventArgs) Handles btnPrint.Click
AxEDOffice1.PrintDialog()
End Sub
Private Sub btnPreview_Click(ByVal sender As System.Object, ByVal e As System
.EventArgs) Handles btnPreview.Click
AxEDOffice1.PrintPreview()
End Sub
Private Sub btnToolbars_Click(ByVal sender As System.Object, ByVal e As System
.EventArgs) Handles btnToolbars.Click
If AxEDOffice1.Toolbars = True Then
AxEDOffice1.Toolbars = False
Else
AxEDOffice1.Toolbars = True
End If
End Sub
Private Sub btnAbout_Click(ByVal sender As System.Object, ByVal e As System
.EventArgs) Handles btnAbout.Click
AxEDOffice1.AboutBox()
End Sub
End Class
打開配置管理器。更改Active解決方案平臺為x86選項。

然后構建VB.NET項目并運行。

The office viewer component組件支持MS Word 97、Word 2000,Word 2003、Word 2007、Word 2010。 它可以在Windows 2000 / Xp / Vista / 2008/7的32位或64位操作系統上運行。 將MS Excel or PowerPoint, Visio, Project 嵌入到VB.NET應用程序中,只需要改變Open方法的第二個參數。如下所示:
public void Open()
{
axEDOffice1.Open(sPath, "Excel.Application");
axEDOffice1.Open(sPath, "PowerPoint.Application");
axEDOffice1.Open(sPath, "Visio.Application");
axEDOffice1.Open(sPath, "MSProject.Application");
}
在Office Viewer Component組件中的Automating Word
有了word組件,在Visual Basic應用程序中使用COM實現Word自動化就會變得十分簡單。從解決方案資源管理器引用中為Word Object Library 11.0添加引用。在這里我所用Word 2003,所以對象庫的版本是11.0。
下面的示例代碼顯示了如何構建一個最小的文檔,插入一個書簽,并且在隨后用文本更換空書簽。
Imports Microsoft.Office.Interop.Word
Private Sub Automating_Click(ByVal sender As System.Object, ByVal e As System
.EventArgs) Handles Automating.Click
Dim word = AxEDOffice1.GetApplication()
word.Visible = True
Dim doc As Document = AxEDOffice1.ActiveDocument()
Dim range As Range = doc.Range
range.InsertAfter("Range1" + vbCrLf)
range.Collapse(WdCollapseDirection.wdCollapseEnd)
doc.Bookmarks.Add("MijnBookmark", range)
range.InsertAfter("Range2" + vbCrLf)
Dim bookmark As Bookmark = doc.Bookmarks(1)
range = bookmark.Range
range.Text = "Bookmark" + vbCrLf
range.Font.Color = WdColor.wdColorBlue
End Sub
標簽:
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:網絡翻譯整理