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

前端之家收集整理的这篇文章主要介绍了章鱼哥出品—VB.NET Office操作之Word(一)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

  1. '*********************************************************************
  2. '作者:章鱼哥,QQ3107073263 群:309816713
  3. '如有疑问或好的建议请联系我,大家一起进步
  4. '*********************************************************************
  5. Imports Microsoft.Office.Interop
  6. Public Class Class_Word1
  7.  
  8. Public ZWordApplic As Word.Application
  9.  
  10. Private ZDocument As Word.Document
  11.  
  12. Public Sub New() '生成类实例
  13. ZWordApplic = New Word.Application
  14. ZWordApplic.Visible = True
  15.  
  16. End Sub
  17.  
  18. '新建一个Word文档
  19. Public Sub NewDocument()
  20. ZDocument = ZWordApplic.Documents.Add() '新建一个文档
  21.  
  22. End Sub
  23. '使用模板新建一个文档
  24. Public Sub ModulNewDocument(ByVal FileAddress As String)
  25. ZDocument = ZWordApplic.Documents.Add(FileAddress)
  26.  
  27. End Sub
  28. '打开一个文档
  29. Public Sub OpenWordDocument(ByVal FileAddress As String,ByVal IsReadOnly As Boolean)
  30. Try
  31. ZDocument = ZWordApplic.Documents.Open(FileAddress,Nothing,IsReadOnly)
  32. Catch ex As Exception
  33. MsgBox("您输入的地址不正确")
  34. End Try
  35. End Sub
  36.  
  37. '关闭一个文档
  38. Public Sub CloseWordDocument()
  39. ZWordApplic.Quit()
  40. System.Runtime.InteropServices.Marshal.ReleaseComObject(ZWordApplic)
  41. ZWordApplic = Nothing
  42. End Sub
  43. '关闭所有打开的文档
  44. Public Sub CloseAllDocuments()
  45.  
  46. ' ZWordApplic.Documents.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
  47. ZWordApplic.Documents.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
  48. End Sub
  49. '保存文档
  50. Public Sub Save()
  51. Try
  52. ZDocument.Save()
  53. MsgBox("保存成功")
  54. Catch ex As Exception
  55. MsgBox(ex.Message)
  56. End Try
  57. End Sub
  58. '另存为
  59. Public Sub SaveAs(ByVal FileAdress As String)
  60. Try
  61. ZDocument.SaveAs2(FileAdress)
  62. MsgBox("另存为成功!")
  63. Catch ex As Exception
  64. MsgBox(ex.Message)
  65. End Try
  66. End Sub
  67. '插入文字
  68. Public Sub InsertText(ByVal text As String)
  69.  
  70. ZWordApplic.Selection.TypeText(text)
  71.  
  72. End Sub
  73.  
  74. '插入表格
  75. Public Sub InsertTabel(ByVal Tabel As DataTable)
  76. Dim ZTabel As Word.Table
  77. ZTabel = ZDocument.Tables.Add(ZWordApplic.Selection.Range,Tabel.Rows.Count + 1,Tabel.Columns.Count)
  78.  
  79. '添加表头
  80. For i = 1 To Tabel.Columns.Count
  81. ZTabel.Rows(1).Cells(i).Range.InsertAfter(Tabel.Columns(i - 1).ColumnName)
  82. Next
  83. '添加表格数据
  84. For i = 2 To Tabel.Rows.Count + 1
  85. For j = 1 To Tabel.Columns.Count
  86. ZTabel.Rows(i).Cells(j).Range.InsertAfter(Tabel.Rows(i - 2).Item(j - 1).ToString)
  87. Next
  88. Next
  89.  
  90. ZTabel.AllowAutoFit = True
  91.  
  92. ZTabel.ApplyStyleFirstColumn = True
  93.  
  94. ZTabel.ApplyStyleHeadingRows = True
  95. End Sub
  96. '插入图片
  97. Public Sub InsertPic(ByVal PicAddress As String)
  98.  
  99. Try
  100. ZWordApplic.Selection.InlineShapes.AddPicture(PicAddress,False,True)
  101.  
  102. Catch ex As Exception
  103. MsgBox("图片地址不正确 ")
  104. End Try
  105.  
  106.  
  107. End Sub
  108. '读取文字
  109. Public Sub ReadText()
  110. ZWordApplic.Selection.WholeStory()
  111. ZWordApplic.Selection.Copy()
  112.  
  113. End Sub
  114. '获取当前的光标位置信息,存放在数组中
  115. Public Function GetCursor() As ArrayList
  116. Try
  117. Dim cursor As New ArrayList
  118. '当前光标所在的页数
  119. Dim Page As Object = ZDocument.Application.Selection.Information(Word.WdInformation.wdActiveEndAdjustedPageNumber)
  120. '当前光标所在行数
  121. Dim row As Object = ZDocument.Application.Selection.Information(Word.WdInformation.wdFirstCharacterLineNumber)
  122. '当前光标所在列数
  123. Dim cul As Object = ZDocument.Application.Selection.Information(Word.WdInformation.wdFirstCharacterColumnNumber)
  124. cursor.AddRange({Page,row,cul})
  125. Return cursor
  126. Catch ex As Exception
  127. MsgBox(ex.Message)
  128. Return Nothing
  129. End Try
  130. End Function
  131.  
  132.  
  133. '鼠标定位到指定页
  134. Public Sub GoToPage(ByVal Page As Integer)
  135. Try
  136. '跳转到指定页码
  137. ZDocument.Application.Selection.GoTo(Word.WdGoToItem.wdGoToPage,Word.WdGoToDirection.wdGoToFirst,Page)
  138.  
  139.  
  140. Catch ex As Exception
  141. MsgBox(ex.Message)
  142. End Try
  143. End Sub
  144. '光标调到指定行。这个是绝对跳转
  145. Public Sub GoToAbsolutLine(ByVal Row As Integer)
  146. Try
  147. '跳转到指定行,说明:这个行是相对于整个文档来算的,将如第一页就2行,你跳到第三行的时候,就是第2页的第1
  148. '读者可自行测试,目前还实现不了给定页,行,列调到精确位置的功能。至少我还没实现。这里就不进行实现了
  149. ZDocument.Application.Selection.GoTo(Word.WdGoToItem.wdGoToLine,Row)
  150.  
  151.  
  152. Catch ex As Exception
  153. MsgBox(ex.Message)
  154. End Try
  155. End Sub
  156. '光标调到指定行。这个是相对跳转。大家应该理解什么意思的
  157. Public Sub GoToOppsiteLine(ByVal Row As Int16)
  158. Try
  159.  
  160.  
  161. '读者可自行测试,目前还实现不了给定页,行,列调到精确位置的功能。至少我还没实现
  162. If Row >= 0 Then '如果大于0,像后跳转
  163. ZDocument.Application.Selection.GoTo(Word.WdGoToItem.wdGoToLine,Word.WdGoToDirection.wdGoToNext,Math.Abs(Row))
  164. Else '小于0,像前跳转
  165. ZDocument.Application.Selection.GoTo(Word.WdGoToItem.wdGoToLine,Word.WdGoToDirection.wdGoToPrevIoUs,Math.Abs(Row))
  166. End If
  167.  
  168.  
  169.  
  170.  
  171. Catch ex As Exception
  172. MsgBox(ex.Message)
  173. End Try
  174. End Sub
  175. '左移光标
  176. Public Sub MoveLeft()
  177. ZDocument.Application.Selection.MoveLeft() '每次移动1位
  178. End Sub
  179. '右移
  180. Public Sub MoveRight()
  181. ZDocument.Application.Selection.MoveRight() '每次移动1位
  182. End Sub
  183. '上移
  184. Public Sub MoveUp()
  185. ZDocument.Application.Selection.MoveUp() '每次移动1位
  186. End Sub
  187. '下移
  188. Public Sub MoveDown()
  189. ZDocument.Application.Selection.MoveDown() '每次移动1位
  190. End Sub

  1. End Class

猜你在找的VB相关文章