章魚哥出品—VB.NET Office操作之Word(一)

在這裏給出了一個Word操作的類,該類具備了對word 文檔操作的基本功能,包括word 文檔的新建,打開,保存,另存,插入圖片,插入表格,插入文字,讀取文字,定位光標位置,移動光標,移動到指定頁等等操作。在下一篇文章中我將給出這個類實現的實例,讀者可以借鑑下
程序引用的是Microsoft Word 14.0 Object Library 使用word 2007 +VS2010

'*********************************************************************
'作者:章魚哥,QQ:3107073263 羣:309816713    
'如有疑問或好的建議請聯繫我,大家一起進步  
'*********************************************************************
Imports Microsoft.Office.Interop
Public Class Class_Word1

    Public ZWordApplic As Word.Application

    Private ZDocument As Word.Document

    Public Sub New() '生成類實例
        ZWordApplic = New Word.Application
        ZWordApplic.Visible = True

    End 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)
        Try
            ZDocument = ZWordApplic.Documents.Open(FileAddress, Nothing, IsReadOnly)
        Catch ex As Exception
            MsgBox("您輸入的地址不正確")
        End Try
    End Sub

    '關閉一個文檔
    Public Sub CloseWordDocument()
        ZWordApplic.Quit()
        System.Runtime.InteropServices.Marshal.ReleaseComObject(ZWordApplic)
        ZWordApplic = Nothing
    End Sub
    '關閉所有打開的文檔
    Public Sub CloseAllDocuments()

        ' ZWordApplic.Documents.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
        ZWordApplic.Documents.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
    End Sub
    '保存文檔
    Public Sub Save()
        Try
            ZDocument.Save()
            MsgBox("保存成功")
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    '另存爲
    Public Sub SaveAs(ByVal FileAdress As String)
        Try
            ZDocument.SaveAs2(FileAdress)
            MsgBox("另存爲成功!")
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End 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.Table
        ZTabel = ZDocument.Tables.Add(ZWordApplic.Selection.Range, Tabel.Rows.Count + 1, Tabel.Columns.Count)

      
        '添加表頭
        For i = 1 To Tabel.Columns.Count
            ZTabel.Rows(1).Cells(i).Range.InsertAfter(Tabel.Columns(i - 1).ColumnName)
        Next
        '添加表格數據
        For i = 2 To Tabel.Rows.Count + 1
            For j = 1 To Tabel.Columns.Count
                ZTabel.Rows(i).Cells(j).Range.InsertAfter(Tabel.Rows(i - 2).Item(j - 1).ToString)
            Next
        Next
       

        ZTabel.AllowAutoFit = True

        ZTabel.ApplyStyleFirstColumn = True

        ZTabel.ApplyStyleHeadingRows = True
    End Sub
    '插入圖片 
    Public Sub InsertPic(ByVal PicAddress As String)

        Try
            ZWordApplic.Selection.InlineShapes.AddPicture(PicAddress, False, True)

        Catch ex As Exception
            MsgBox("圖片地址不正確 ")
        End Try


    End Sub
    '讀取文字
    Public Sub ReadText()
        ZWordApplic.Selection.WholeStory()
        ZWordApplic.Selection.Copy()

    End Sub
 '獲取當前的光標位置信息,存放在數組中
    Public Function GetCursor() As ArrayList
        Try
            Dim 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 cursor
        Catch ex As Exception
            MsgBox(ex.Message)
            Return Nothing
        End Try
    End Function


    '鼠標定位到指定頁
    Public Sub GoToPage(ByVal Page As Integer)
        Try
            '跳轉到指定頁碼
            ZDocument.Application.Selection.GoTo(Word.WdGoToItem.wdGoToPage, Word.WdGoToDirection.wdGoToFirst, Page)


        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End 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 Exception
            MsgBox(ex.Message)
        End Try
    End 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 If




        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End 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

End Class


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章