Imports Microsoft.Office.InteropPublic Class Class_Word1Public ZWordApplic As Word.ApplicationPrivate ZDocument As Word.DocumentPublic Sub New() '生成类实例ZWordApplic = New Word.ApplicationZWordApplic.Visible = TrueEnd Sub'新建一个Word文档Public Sub NewDocument()ZDocument = ZWordApplic.Documents.Add() '新建一个文档End Sub'使用模板新建一个文档Public Sub ModulNewDocument(ByVal FileAddress As String)ZDocument = ZWordApplic.Documents.Add(FileAddress)End Sub'打开一个文档Public Sub OpenWordDocument(ByVal FileAddress As String, ByVal IsReadOnly As Boolean)TryZDocument = ZWordApplic.Documents.Open(FileAddress, Nothing, IsReadOnly)Catch ex As ExceptionMsgBox("您输入的地址不正确")End TryEnd Sub'关闭一个文档Public Sub CloseWordDocument()ZWordApplic.Quit()System.Runtime.InteropServices.Marshal.ReleaseComObject(ZWordApplic)ZWordApplic = NothingEnd Sub'关闭所有打开的文档Public Sub CloseAllDocuments()' ZWordApplic.Documents.Close(Word.WdSaveOptions.wdDoNotSaveChanges)ZWordApplic.Documents.Close(Word.WdSaveOptions.wdDoNotSaveChanges)End Sub'保存文档Public Sub Save()TryZDocument.Save()MsgBox("保存成功")Catch ex As ExceptionMsgBox(ex.Message)End TryEnd Sub'另存为Public Sub SaveAs(ByVal FileAdress As String)TryZDocument.SaveAs2(FileAdress)MsgBox("另存为成功!")Catch ex As ExceptionMsgBox(ex.Message)End TryEnd Sub'插入文字Public Sub InsertText(ByVal text As String)ZWordApplic.Selection.TypeText(text)End Sub'插入表格Public Sub InsertTabel(ByVal Tabel As DataTable)Dim ZTabel As Word.TableZTabel = ZDocument.Tables.Add(ZWordApplic.Selection.Range, Tabel.Rows.Count + 1, Tabel.Columns.Count)'添加表头For i = 1 To Tabel.Columns.CountZTabel.Rows(1).Cells(i).Range.InsertAfter(Tabel.Columns(i - 1).ColumnName)Next'添加表格数据For i = 2 To Tabel.Rows.Count + 1For j = 1 To Tabel.Columns.CountZTabel.Rows(i).Cells(j).Range.InsertAfter(Tabel.Rows(i - 2).Item(j - 1).ToString)NextNextZTabel.AllowAutoFit = TrueZTabel.ApplyStyleFirstColumn = TrueZTabel.ApplyStyleHeadingRows = TrueEnd Sub'插入图片Public Sub InsertPic(ByVal PicAddress As String)TryZWordApplic.Selection.InlineShapes.AddPicture(PicAddress, False, True)Catch ex As ExceptionMsgBox("图片地址不正确 ")End TryEnd Sub'读取文字Public Sub ReadText()ZWordApplic.Selection.WholeStory()ZWordApplic.Selection.Copy()End Sub'获取当前的光标位置信息,存放在数组中Public Function GetCursor() As ArrayListTryDim cursor As New ArrayList'当前光标所在的页数Dim Page As Object = ZDocument.Application.Selection.Information(Word.WdInformation.wdActiveEndAdjustedPageNumber)'当前光标所在行数Dim row As Object = ZDocument.Application.Selection.Information(Word.WdInformation.wdFirstCharacterLineNumber)'当前光标所在列数Dim cul As Object = ZDocument.Application.Selection.Information(Word.WdInformation.wdFirstCharacterColumnNumber)cursor.AddRange({Page, row, cul})Return cursorCatch ex As ExceptionMsgBox(ex.Message)Return NothingEnd TryEnd Function'鼠标定位到指定页Public Sub GoToPage(ByVal Page As Integer)Try'跳转到指定页码ZDocument.Application.Selection.GoTo(Word.WdGoToItem.wdGoToPage, Word.WdGoToDirection.wdGoToFirst, Page)Catch ex As ExceptionMsgBox(ex.Message)End TryEnd Sub'光标调到指定行。这个是绝对跳转Public Sub GoToAbsolutLine(ByVal Row As Integer)Try'跳转到指定行,说明:这个行是相对于整个文档来算的,将如第一页就2行,你跳到第三行的时候,就是第2页的第1行'读者可自行测试,目前还实现不了给定页,行,列调到精确位置的功能。至少我还没实现。这里就不进行实现了ZDocument.Application.Selection.GoTo(Word.WdGoToItem.wdGoToLine, Word.WdGoToDirection.wdGoToFirst, Row)Catch ex As ExceptionMsgBox(ex.Message)End TryEnd Sub'光标调到指定行。这个是相对跳转。大家应该理解什么意思的Public Sub GoToOppsiteLine(ByVal Row As Int16)Try'读者可自行测试,目前还实现不了给定页,行,列调到精确位置的功能。至少我还没实现If Row >= 0 Then '如果大于0,像后跳转ZDocument.Application.Selection.GoTo(Word.WdGoToItem.wdGoToLine, Word.WdGoToDirection.wdGoToNext, Math.Abs(Row))Else '小于0,像前跳转ZDocument.Application.Selection.GoTo(Word.WdGoToItem.wdGoToLine, Word.WdGoToDirection.wdGoToPrevious, Math.Abs(Row))End IfCatch ex As ExceptionMsgBox(ex.Message)End TryEnd Sub'左移光标Public Sub MoveLeft()ZDocument.Application.Selection.MoveLeft() '每次移动1位End Sub'右移Public Sub MoveRight()ZDocument.Application.Selection.MoveRight() '每次移动1位End Sub'上移Public Sub MoveUp()ZDocument.Application.Selection.MoveUp() '每次移动1位End Sub'下移Public Sub MoveDown()ZDocument.Application.Selection.MoveDown() '每次移动1位End Sub
本文是在上文给出的Class_Word1类的实例,实现了类中的各个功能,读者可借鉴参考。
实现窗体:
代码实现:代码直接复制到上文的窗体类中
'*********************************************************************
Imports Microsoft.Office.InteropPublic Class Form1Dim Array_Word As New ArrayListPrivate Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LoadRichTextBox1.Text = "章鱼哥出品VB.NET"End Sub'新建一个Word文档Private Sub But_NewWord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles But_NewWord.ClickDim My_word As New Class_Word1My_word.NewDocument()Array_Word.Add(My_word)End Sub'以模板新建Private Sub But_ModuleNewWord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles But_ModuleNewWord.ClickDim My_word As New Class_Word1My_word.ModulNewDocument(TextBox1.Text)Array_Word.Add(My_word)End Sub'打开一个文档Private Sub But_OpenWord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles But_OpenWord.ClickDim My_word As New Class_Word1My_word.OpenWordDocument(TextBox1.Text, False)Array_Word.Add(My_word)End Sub'关闭当前打开的所有文档Private Sub But_CloseAllDocument_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles But_CloseAllDocument.ClickFor Each Word_Class As Class_Word1 In Array_WordWord_Class.CloseWordDocument()NextArray_Word.Clear()End Sub'保存文档Private Sub But_Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles But_Save.ClickFor Each Word_Class As Class_Word1 In Array_WordWord_Class.Save()NextEnd Sub'另存为Private Sub But_SaveAs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles But_SaveAs.ClickFor Each Word_Class As Class_Word1 In Array_WordWord_Class.SaveAs(TextBox1.Text)NextEnd Sub'插入文本Private Sub But_Insert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles But_Insert.ClickFor Each Word_Class As Class_Word1 In Array_WordWord_Class.InsertText(RichTextBox1.Text)NextEnd Sub'插入表格Private Sub But_InsertTabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles But_InsertTabel.ClickDim tabel As DataTable = GetTabel(ListView1)For Each Word_Class As Class_Word1 In Array_WordWord_Class.InsertTabel(GetTabel(ListView1))NextEnd Sub'从listview 中读取数据生成DataTablePrivate Function GetTabel(ByVal lis As ListView) As DataTableDim Tabel As New DataTable()'加表头For i = 0 To lis.Columns.Count - 1Tabel.Columns.Add(lis.Columns(i).Text.ToString)NextFor i = 0 To lis.Items.Count - 1Dim row As DataRow = Tabel.NewRowFor j = 0 To lis.Columns.Count - 1row.Item(j) = lis.Items(i).SubItems(j).TextNextTabel.Rows.Add(row)NextReturn TabelEnd Function'插入图片Private Sub But_InsertPic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles But_InsertPic.ClickFor Each Word_Class As Class_Word1 In Array_WordWord_Class.InsertPic(TextBox2.Text)NextEnd Sub'读取文档的内容Private Sub But_ReadText_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles But_ReadText.ClickFor Each Word_Class As Class_Word1 In Array_WordWord_Class.ReadText()RichTextBox1.Paste()NextEnd Sub'获取文档路径Private Sub But_GetAdrress_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles But_GetAdrress.ClickDim opendialog As New OpenFileDialogIf opendialog.ShowDialog = DialogResult.OK ThenTextBox1.Text = opendialog.FileNameEnd IfEnd Sub'获取当前鼠标的位置Private Sub But_GetCursor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles But_GetCursor.ClickFor Each Word_Class As Class_Word1 In Array_WordDim Cursor As ArrayList = Word_Class.GetCursor()If Cursor IsNot Nothing ThenFor i = 0 To Cursor.Count - 1RichTextBox1.Text &= " " & Cursor(i)NextEnd IfNextEnd Sub'将光标移动到指定页Private Sub But_GoTo_Page_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles But_GoTo_Page.ClickFor Each Word_Class As Class_Word1 In Array_WordWord_Class.GoToPage(Tex_Page.Text)NextEnd Sub'光标移动到指定行(绝对)Private Sub But_GotoAbsoultRow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles But_GotoAbsoultRow.ClickFor Each Word_Class As Class_Word1 In Array_WordWord_Class.GoToAbsolutLine(Tex_Row_Absoult.Text)NextEnd Sub'光标移动到指定行(相对)Private Sub But_GotoOppsitRow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles But_GotoOppsitRow.ClickFor Each Word_Class As Class_Word1 In Array_WordWord_Class.GoToOppsiteLine(Tex_Row_Oppsit.Text)NextEnd Sub'上下左右按钮,点击按钮一次移动一位Private Sub PictureBox1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp'MsgBox("X:" & e.X & "Y:" & e.Y)Dim x As Integer = e.XDim y As Integer = e.Y'RichTextBox1.Text &= "|" & e.X & ":" & e.YFor Each Word_Class As Class_Word1 In Array_WordIf x > 70 And x < 130 ThenIf y > 20 And y < 45 ThenWord_Class.MoveUp()ElseIf y > 110 And y < 135 ThenWord_Class.MoveDown()End IfEnd IfIf y > 45 And y < 105 ThenIf x > 40 And x < 65 ThenWord_Class.MoveLeft()ElseIf x > 135 And y < 160 ThenWord_Class.MoveRight()End IfEnd IfNextEnd Sub
End Class
如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛