Public Class vbwordapp
Private oWordApplic As word.ApplicationClass
Private oDoc As word.Document
Public Sub vbwordapp()
'激活com word接口
oWordApplic = New Word.ApplicationClass
End Sub
' Open a file (the file must exists) and activate it
Public Sub open(ByVal strFilename As String)
Dim filename As String
Dim onlyread As Boolean
Dim isvisible As Boolean
Dim missing
filename = strFilename
onlyread = False
isvisible = True
missing = System.Reflection.Missing.Value
oDoc = oWordApplic.Documents.Open(filename, missing, onlyread, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing)
oDoc.Activate()
End Sub
'打开一个文档
Public Sub open()
Dim missing
missing = System.Reflection.Missing.Value
oDoc = oWordApplic.Documents.Add(missing, missing, missing, missing)
oDoc.Activate()
End Sub
Public Sub quit()
Dim missing
missing = System.Reflection.Missing.Value
oWordApplic.Application.Quit(missing, missing, missing)
End Sub
Public Sub save()
oDoc.Save()
End Sub
Public Sub saveas(ByVal strfilename As String)
Dim missing
Dim filename As String
missing = System.Reflection.Missing.Value
filename = strfilename
oDoc.SaveAs(filename, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing)
End Sub
Public Sub saveashtml(ByVal strfilename As String)
Dim missing
missing = System.Reflection.Missing.Value
Dim filename As String
filename = strfilename
Dim format
format = CInt(Word.WdSaveFormat.wdFormatHTML)
oDoc.SaveAs(filename, format, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing)
End Sub
Public Sub inserttext(ByVal strtext)
oWordApplic.Selection.TypeText(strtext)
End Sub
Public Sub insertlinebreak()
oWordApplic.Selection.TypeParagraph()
End Sub
Public Sub insertlinebreak(ByVal nline As Integer)
Dim i
For i = 1 To nline
oWordApplic.Selection.TypeParagraph()
Next
End Sub
Public Sub setalignment(ByVal strtype As String)
Select Case strtype
Case "center"
oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
Case "left"
oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft
Case "right"
oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
Case "justify"
oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphJustify
End Select
End Sub
Public Sub setfont(ByVal strtype As String)
Select Case strtype
Case "bold"
oWordApplic.Selection.Font.Bold = 1
Case "italic"
oWordApplic.Selection.Font.Italic = 1
Case "underlined"
oWordApplic.Selection.Font.Subscript = 0
End Select
End Sub
' disable all the style
Public Sub SetFont()
oWordApplic.Selection.Font.Bold = 0
oWordApplic.Selection.Font.Italic = 0
oWordApplic.Selection.Font.Subscript = 0
End Sub
Public Sub SetFontName(ByVal strType As String)
oWordApplic.Selection.Font.Name = strType
End Sub
Public Sub SetFontSize(ByVal nSize As Integer)
oWordApplic.Selection.Font.Size = nSize
End Sub
Public Sub insertpagebreak()
Dim pBreak As Integer
pBreak = CInt(Word.WdBreakType.wdPageBreak)
oWordApplic.Selection.InsertBreak(pBreak)
End Sub
' Go to a predefined bookmark, if the bookmark doesn't exists the application will raise an error
Public Sub GotoBookMark(ByVal strBookMarkName As String)
Dim missing
missing = System.Reflection.Missing.Value
Dim Bookmark
Bookmark = CInt(Word.WdGoToItem.wdGoToBookmark)
Dim namebookmark
NameBookMark = strBookMarkName
oWordApplic.Selection.GoTo(Bookmark, missing, missing, namebookmark)
End Sub
Public Sub GoToTheEnd()
Dim missing, unit
missing = System.Reflection.Missing.Value
unit = Word.WdUnits.wdStory
oWordApplic.Selection.EndKey(unit, missing)
End Sub
Public Sub GoToTheBeginning()
Dim missing, unit
missing = System.Reflection.Missing.Value
unit = Word.WdUnits.wdStory
oWordApplic.Selection.HomeKey(unit, missing)
End Sub
Public Sub GoToTheTable(ByVal ntable As Integer)
Dim missing, what, which, count
missing = System.Reflection.Missing.Value
what = Word.WdUnits.wdTable
which = Word.WdGoToDirection.wdGoToFirst
count = 1
oWordApplic.Selection.GoTo(what, which, count, missing)
oWordApplic.Selection.Find.ClearFormatting()
oWordApplic.Selection.Text = ""
End Sub
Public Sub GoToRightCell()
Dim missing, direction
missing = System.Reflection.Missing.Value
direction = Word.WdUnits.wdCell
oWordApplic.Selection.MoveRight(direction, missing, missing)
End Sub
Public Sub GoToLeftCell()
Dim missing, direction
missing = System.Reflection.Missing.Value
direction = Word.WdUnits.wdCell
oWordApplic.Selection.MoveLeft(direction, missing, missing)
End Sub
Public Sub GoToDownCell()
Dim missing, direction
missing = System.Reflection.Missing.Value
direction = Word.WdUnits.wdLine
oWordApplic.Selection.MoveDown(direction, missing, missing)
End Sub
Public Sub GoToUpCell()
Dim missing, direction
missing = System.Reflection.Missing.Value
direction = Word.WdUnits.wdLine
oWordApplic.Selection.MoveUp(direction, missing, missing)
End Sub
' this function doesn't work
Public Sub InsertPageNumber(ByVal strType As String, ByVal bHeader As Boolean)
Dim missing, alignment, bfirstpage, bf
missing = System.Reflection.Missing.Value
bfirstpage = False
bf = True
Select Case strType
Case "Center"
alignment = Word.WdPageNumberAlignment.wdAlignPageNumberCenter
oWordApplic.Selection.HeaderFooter.PageNumbers.Item(1).Alignment = Word.WdPageNumberAlignment.wdAlignPageNumberCenter
Case "Right"
alignment = Word.WdPageNumberAlignment.wdAlignPageNumberRight
oWordApplic.Selection.HeaderFooter.PageNumbers.Item(1).Alignment = Word.WdPageNumberAlignment.wdAlignPageNumberRight
Case "Left"
alignment = Word.WdPageNumberAlignment.wdAlignPageNumberLeft
oWordApplic.Selection.HeaderFooter.PageNumbers.Add(alignment, bfirstpage)
End Select
End Sub
End Class