VB.NET 操作 WORD(VBA) VB.NET 操作 WORD 1Public Class WordOpLib 2 3 4 Private oWordApplic As Word.ApplicationClass 5 Private oDocument As Word.Document 6 Private oRange As Word.Range 7 Private oShape As Word.Shape 8 Private oSelection As Word.Selection 9 10 11 Public Sub New() 12 '激活 com word 接口 13 oWordApplic = New Word.ApplicationClass 14 oWordApplic.Visible = False 15 16 End Sub 17 '设置选定文本 18 Public Sub SetRange(ByVal para As Integer) 19 oRange = oDocument.Paragraphs(para).Range 20 oRange.Select() 21 End Sub 22 Public Sub SetRange(ByVal para As Integer, ByVal sent As Integer) 23 oRange = oDocument.Paragraphs(para).Range.Sentences(sent) 24 oRange.Select() 25 End Sub 26 Public Sub SetRange(ByVal startpoint As Integer, ByVal endpoint As Integer, ByVal flag As Boolean) 27 If flag = True Then 28 oRange = oDocument.Range(startpoint, endpoint) 29 oRange.Select() 30 Else 31 32 End If 33 End Sub 34 35 '生成空的新文档 36 Public Sub NewDocument() 37 Dim missing = System.Reflection.Missing.Value 38 Dim isVisible As Boolean = True 39 oDocument = oWordApplic.Documents.Add(missing, missing, missing, missing) 40 oDocument.Activate() 41 End Sub 42 '使用模板生成新文档 43 Public Sub NewDocWithModel(ByVal FileName As String) 44 Dim missing = System.Reflection.Missing.Value 45 Dim isVisible As Boolean = False 46 Dim strName As String 47 strName = FileName 48 oDocument = oWordApplic.Documents.Add(strName, missing, missing, isVisible) 49 oDocument.Activate() 50 End Sub 51 '打开已有文档 52 Public Sub OpenFile(ByVal FileName As String) 53 Dim strName As String 54 Dim isReadOnly As Boolean 55 Dim isVisible As Boolean 56 Dim missing = System.Reflection.Missing.Value 57 58 strName = FileName 59 isReadOnly = False 60 isVisible = True ...