下面的文章將一步一步演示如何在VB.NET應(yīng)用中添加MS word。
在VB.NET應(yīng)用中添加Word組件
打開(kāi)Visual Studio并新建一個(gè)VB.NET應(yīng)用,右鍵點(diǎn)擊HostOffice Solution,然后點(diǎn)擊Add Reference...菜單項(xiàng)。

在彈出對(duì)話框的Browse選項(xiàng)卡中選擇officeviewer.ocx文件。

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

切換到Form1窗體設(shè)計(jì)窗口。

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

為新建、打開(kāi)、另存,關(guān)閉和打印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
打開(kāi)配置管理器。更改Active解決方案平臺(tái)為x86選項(xiàng)。

然后構(gòu)建VB.NET項(xiàng)目并運(yùn)行。

The office viewer component組件支持MS Word 97、Word 2000,Word 2003、Word 2007、Word 2010。 它可以在Windows 2000 / Xp / Vista / 2008/7的32位或64位操作系統(tǒng)上運(yùn)行。 將MS Excel or PowerPoint, Visio, Project 嵌入到VB.NET應(yīng)用程序中,只需要改變Open方法的第二個(gè)參數(shù)。如下所示:
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應(yīng)用程序中使用COM實(shí)現(xiàn)Word自動(dòng)化就會(huì)變得十分簡(jiǎn)單。從解決方案資源管理器引用中為Word Object Library 11.0添加引用。在這里我所用Word 2003,所以對(duì)象庫(kù)的版本是11.0。
下面的示例代碼顯示了如何構(gòu)建一個(gè)最小的文檔,插入一個(gè)書簽,并且在隨后用文本更換空書簽。
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
標(biāo)簽:
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:網(wǎng)絡(luò)翻譯整理