章鱼哥出品—VB.NET Office *** 作之Word(一)

章鱼哥出品—VB.NET Office *** 作之Word(一),第1张

概述在这里给出了一个Word *** 作的类,该类具备了对word 文档 *** 作的基本功能,包括word 文档的新建,打开,保存,另存,插入图片,插入表格,插入文字,读取文字,定位光标位置,移动光标,移动到指定页等等 *** 作。在下一篇文章中我将给出这个类实现的实例,读者可以借鉴下 程序引用的是Microsoft Word 14.0 Object Library 使用word 2007 +VS2010 '*******

在这里给出了一个Word *** 作的类,该类具备了对word 文档 *** 作的基本功能,包括word 文档的新建,打开,保存,另存,插入图片,插入表格,插入文字,读取文字,定位光标位置,移动光标,移动到指定页等等 *** 作。在下一篇文章中我将给出这个类实现的实例,读者可以借鉴下
程序引用的是Microsoft Word 14.0 Object library 使用word 2007 +VS2010

'*********************************************************************'作者:章鱼哥,QQ:3107073263 群:309816713    '如有疑问或好的建议请联系我,大家一起进步  '*********************************************************************imports Microsoft.Office.InteropPublic 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,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
总结

以上是内存溢出为你收集整理的章鱼出品—VB.NET Office *** 作之Word(一)全部内容,希望文章能够帮你解决章鱼哥出品—VB.NET Office *** 作之Word(一)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1271973.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存