#Region "文件说明"
'***************************************************************************
' * * 文件名:WordLibrary.vb
' * * 创建人:孙振芳
' * * 日 期:2004-08-27
' * * 描 述:本类完成对Word的相关操作。
' * *
' * * 修改人:
' * * 日 期:
' * * 描 述:
' * *
' * * 版 本:1.0.0
' ***************************************************************************
#End Region
Public Class WordLibrary
'Word应用程序对象
Private mWordApp As Word.ApplicationClass
'Word文档对象
Private mDoc As Word.Document
'空值
Dim missing As Object = System.Reflection.Missing.Value
'构造函数
Public Sub New()
mWordApp = New Word.ApplicationClass
End Sub
'打开一个已经存在的Word文档并激活它
Public Sub Open(ByVal strFileName As String)
Dim fileName As Object = strFileName
Dim ro As Object = False
Dim isVisible As Object = True
mDoc = mWordApp.Documents.Open(fileName, missing, ro, missing, missing, missing, missing, missing, missing, missing, missing, isVisible)
mDoc.Activate()
End Sub
'打开一个新建立的Word文档并激活它
Public Sub Open()
mDoc = mWordApp.Documents.Add(missing, missing, missing, missing)
mDoc.Activate()
End Sub
'退出Word应用程序
Public Sub Quit()
mWordApp.Application.Quit(missing, missing, missing)
End Sub
'保存Word文档
Public Sub Save()
mDoc.Save()
End Sub
'另存为Word文档
Public Sub SaveAs(ByVal strFileName As String)
Dim fileName As Object = strFileName
mDoc.SaveAs(fileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing)
End Sub
Public Sub CloseDoc()
mDoc.Close(missing, missing, missing)
End Sub
'将Word文档另存为Html格式
Public Sub SaveAsHtml(ByVal strFileName As String)
Dim fileName As Object = strFileName
Dim Format As Object = CType(Word.WdSaveFormat.wdFormatHTML, Integer)
mDoc.SaveAs(fileName, Format, missing, missing, missing, missing, missing, missing, missing, missing, missing)
End Sub
'插入指定文本+一行
Public Sub InsertText(ByVal strText As String)
mWordApp.Selection.TypeText(strText)
mWordApp.Selection.TypeParagraph()
End Sub
'插入指定文本
Public Sub InsertSingleText(ByVal strText As String)
mWordApp.Selection.TypeText(strText)
End Sub
'插入一行
Public Sub InsertLineBreak()
mWordApp.Selection.TypeParagraph()
End Sub
'插入若干行
Public Sub InsertLineBreak(ByVal nline As Integer)
Dim i As Integer
For i = 0 To nline
mWordApp.Selection.TypeParagraph()
Next i
End Sub
'设定字体样式
Public Sub SetStyle(ByVal iStyle As Formats.StyleList)
Dim obj As Object
Select Case iStyle
Case Formats.StyleList.Normal
obj = "正文"
Case Formats.StyleList.Title1
obj = "标题 1"
Case Formats.StyleList.Title2
obj = "标题 2"
Case Formats.StyleList.Title3
obj = "标题 3"
Case Formats.StyleList.Title4
obj = "标题 4"
Case Formats.StyleList.Title5
obj = "标题 5"
Case Formats.StyleList.Title
obj = "标题"
End Select
mWordApp.Selection.Style = mWordApp.ActiveDocument.Styles.Item(obj)
End Sub
'设定段落格式
Public Sub SetAlignment(ByVal iType As Formats.AlignType)
Select Case iType
Case Formats.AlignType.Center
mWordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
Case Formats.AlignType.Left
mWordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft
Case Formats.AlignType.Right
mWordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
Case Formats.AlignType.Justify
mWordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphJustify
End Select
End Sub
'设置字体格式
Public Sub SetFont(ByVal iType As Formats.FontType, ByVal isSet As Boolean)
Select Case iType
Case Formats.FontType.Bold
mWordApp.Selection.Font.Bold = IIf(isSet = True, 1, 0)
Case Formats.FontType.Italic
mWordApp.Selection.Font.Italic = IIf(isSet = True, 1, 0)
Case Formats.FontType.Underlined
mWordApp.Selection.Font.Subscript = IIf(isSet = True, 1, 0)
End Select
End Sub
'设定字体名称
Public Sub SetFontName(ByVal strName As String)
mWordApp.Selection.Font.Name = strName
End Sub
'设定字体大小
Public Sub SetFontSize(ByVal iSize As Integer)
mWordApp.Selection.Font.Size = iSize
End Sub
'插入分页符
Public Sub InsertPageBreak()
Dim pBreak As Object = CInt(Word.WdBreakType.wdPageBreak)
mWordApp.Selection.InsertBreak(pBreak)
End Sub
'定位到一个预先定义好的书签,并指定该书签的值
Public Sub GotoBookMark(ByVal strBookMarkName As String)
Dim Bookmark As Object = CInt(Word.WdGoToItem.wdGoToBookmark)
Dim NameBookMark As Object = strBookMarkName
mWordApp.Selection.GoTo(Bookmark, missing, missing, NameBookMark)
End Sub
'定位到文档结尾
Public Sub GoToTheEnd()
Dim unit As Object
unit = Word.WdUnits.wdStory
mWordApp.Selection.EndKey(unit, missing)
End Sub
'定位到文档开头
Public Sub GoToTheBeginning()
Dim unit As Object
unit = Word.WdUnits.wdStory
mWordApp.Selection.HomeKey(unit, missing)
End Sub
'指定当前活动区域
Public Sub SetActivePane(ByVal iPane As Formats.ActivePane)
Select Case iPane
Case Formats.ActivePane.MainDocument
mDoc.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument
Case Formats.ActivePane.CurrentPageHeader
mDoc.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageHeader
Case Formats.ActivePane.CurrentPageFooter
mDoc.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageFooter
End Select
End Sub
'创建表格
Public Sub CreateTable(ByVal rowCount As Integer, ByVal columnCount As Integer)
mDoc.Tables.Add(mWordApp.Selection.Range, rowCount, columnCount)
End Sub
'插入下面的行
Public Sub InsertBelowRow()
mWordApp.Selection.InsertRowsBelow()
End Sub
'移动到右单元格
Public Sub GoToRightCell()
Dim direction As Object
direction = Word.WdUnits.wdCell
mWordApp.Selection.MoveRight(direction, missing, missing)
End Sub
'移动到左单元格
Public Sub GoToLeftCell()
Dim direction As Object
direction = Word.WdUnits.wdCell
mWordApp.Selection.MoveLeft(direction, missing, missing)
End Sub
'移动到下单元格
Public Sub GoToDownCell()
Dim direction As Object
direction = Word.WdUnits.wdLine
mWordApp.Selection.MoveDown(direction, missing, missing)
End Sub
'移动到上单元格
Public Sub GoToUpCell()
Dim direction As Object
direction = Word.WdUnits.wdLine
mWordApp.Selection.MoveUp(direction, missing, missing)
End Sub
End Class