操作word文档的类(vb.net编写)


有时候我们需要在程序中操作Word文档,虽然C#可以用来写这样的一个类,但由于VB先天的优势(对VBA的完全支持),用VB来写显然是更好的选择。下面这个类基于网上的一个类,修改了其中的一些错误,添加了几个方法,在我的运行环境(VS.NET,Office2003)中运行通过。使用时,在“引用”中添加这个类的引用,就可以直接用C#调用类中的方法来操作Word文档了。
Public  Class WordOpLib Class WordOpLib
    
Private oWordApplic As Word.Application
    
Private oDocument As Word.Document
    
Private oRange As Word.Range
    
Private oSelection As Word.Selection
    
Public Sub New()Sub New()
        
'激活com  word接口
        oWordApplic = New Word.Application
        oWordApplic.Visible 
= True
    
End Sub

    
'设置选定文本
    Public Sub SetRange()Sub SetRange(ByVal para As Integer)
        oRange 
= oDocument.Paragraphs(para).Range
        oRange.Select()
    
End Sub

    
Public Sub SetRange()Sub SetRange(ByVal para As IntegerByVal sent As Integer)
        oRange 
= oDocument.Paragraphs(para).Range.Sentences(sent)
        oRange.Select()
    
End Sub

    
Public Sub SetRange()Sub SetRange(ByVal startpoint As IntegerByVal endpoint As IntegerByVal flag As Boolean)
        
If flag = True Then
            oRange 
= oDocument.Range(startpoint, endpoint)
            oRange.Select()
        
Else

        
End If
    
End Sub


    
'生成空的新文档
    Public Sub NewDocument()Sub NewDocument()
        
Dim missing = System.Reflection.Missing.Value
        
Dim isVisible As Boolean = True
        oDocument 
= oWordApplic.Documents.Add(missing, missing, missing, missing)
        oDocument.Activate()
    
End Sub

    
'使用模板生成新文档
    Public Sub NewDocWithModel()Sub NewDocWithModel(ByVal FileName As String)
        
Dim missing = System.Reflection.Missing.Value
        
Dim isVisible As Boolean = True
        
Dim strName As String
        strName 
= FileName

        oDocument 
= oWordApplic.Documents.Add(strName, missing, missing, isVisible)
        oDocument.Activate()
    
End Sub

    
'打开已有文档
    Public Sub OpenFile()Sub OpenFile(ByVal FileName As String)
        
Dim strName As String
        
Dim isReadOnly As Boolean
        
Dim isVisible As Boolean
        
Dim missing = System.Reflection.Missing.Value

        strName 
= FileName
        isReadOnly 
= False
        isVisible 
= True

        oDocument 
= oWordApplic.Documents.Open(strName, missing, isReadOnly, missing, missing, missing, missing, missing, missing, missing, missing, isVisible, missing, missing, missing, missing)
        oDocument.Activate()

    
End Sub

    
Public Sub OpenFile()Sub OpenFile(ByVal FileName As StringByVal isReadOnly As Boolean)
        
Dim strName As String
        
Dim isVisible As Boolean
        
Dim missing = System.Reflection.Missing.Value

        strName 
= FileName
        isVisible 
= True

        oDocument 
= oWordApplic.Documents.Open(strName, missing, isReadOnly, missing, missing, missing, missing, missing, missing, missing, missing, isVisible, missing, missing, missing, missing)
        oDocument.Activate()
    
End Sub

    
'退出Word
    Public Sub Quit()Sub Quit()
        
Dim missing = System.Reflection.Missing.Value
        oWordApplic.Quit()
        System.Runtime.InteropServices.Marshal.ReleaseComObject(oWordApplic)
        oWordApplic 
= Nothing
    
End Sub

    
'关闭所有打开的文档
    Public Sub CloseAllDocuments()Sub CloseAllDocuments()
        oWordApplic.Documents.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
    
End Sub

    
'关闭当前的文档
    Public Sub CloseCurrentDocument()Sub CloseCurrentDocument()
        oDocument.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
    
End Sub

    
'保存当前文档
    Public Sub Save()Sub Save()
        
Try
            oDocument.Save()
        
Catch
            
MsgBox(Err.Description)
        
End Try
    
End Sub

    
'另存为文档
    Public Sub SaveAs()Sub SaveAs(ByVal FileName As String)
        
Dim strName As String
        
Dim missing = System.Reflection.Missing.Value

        strName 
= FileName

        oDocument.SaveAs(strName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing)
    
End Sub

    
'保存为Html文件
    Public Sub SaveAsHtml()Sub SaveAsHtml(ByVal FileName As String)
        
Dim missing = System.Reflection.Missing.Value
        
Dim strName As String

        strName 
= FileName
        
Dim format = CInt(Word.WdSaveFormat.wdFormatHTML)

        oDocument.SaveAs(strName, 
format, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing)
    
End Sub

    
'插入文本
    Public Sub InsertText()Sub InsertText(ByVal text As String)
        oWordApplic.Selection.TypeText(text)
    
End Sub

    
'插入一个空行
    Public Sub InsertLineBreak()Sub InsertLineBreak()
        oWordApplic.Selection.TypeParagraph()
    
End Sub

    
'插入指定行数的空行
    Public Sub InsertLineBreak()Sub InsertLineBreak(ByVal lines As Integer)
        
Dim i As Integer
        
For i = 1 To lines
            oWordApplic.Selection.TypeParagraph()
        
Next
    
End Sub

    
'插入表格
    Public Sub InsertTable()Sub InsertTable(ByRef table As DataTable)
        
Dim oTable As Word.Table
        
Dim rowIndex, colIndex, NumRows, NumColumns As Integer
        rowIndex 
= 1
        colIndex 
= 0

        NumRows 
= table.Rows.Count + 1
        NumColumns 
= table.Columns.Count
        oTable 
= oDocument.Tables.Add(oWordApplic.Selection.Range(), NumRows, NumColumns)


        
'初始化列
        Dim Row As DataRow
        
Dim Col As DataColumn
        
For Each Col In table.Columns
            colIndex 
= colIndex + 1
            oTable.Cell(
1, colIndex).Range.InsertAfter(Col.ColumnName)
        
Next

        
'将行添入表格
        For Each Row In table.Rows
            rowIndex 
= rowIndex + 1
            colIndex 
= 0
            
For Each Col In table.Columns
                colIndex 
= colIndex + 1
                oTable.Cell(rowIndex, colIndex).Range.InsertAfter(Row(Col.ColumnName))
            
Next
        
Next
        oTable.AllowAutoFit 
= True
        oTable.ApplyStyleFirstColumn 
= True
        oTable.ApplyStyleHeadingRows 
= True
    
End Sub

    
'设置对齐
    Public Sub SetAlignment()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 SetStyle()Sub SetStyle(ByVal strFont As String)
        
Select Case strFont
            
Case "bold"
                oWordApplic.Selection.Font.Bold 
= 1
            
Case "italic"
                oWordApplic.Selection.Font.Italic 
= 1
            
Case "underlined"
                oWordApplic.Selection.Font.Subscript 
= 1
        
End Select
    
End Sub

    
'取消字体风格
    Public Sub DissableStyle()Sub DissableStyle()
        oWordApplic.Selection.Font.Bold 
= 0
        oWordApplic.Selection.Font.Italic 
= 0
        oWordApplic.Selection.Font.Subscript 
= 0
    
End Sub

    
'设置字体字号
    Public Sub SetFontSize()Sub SetFontSize(ByVal nSize As Integer)
        oWordApplic.Selection.Font.Size 
= nSize
    
End Sub

    
'跳过本页
    Public Sub InsertPageBreak()Sub InsertPageBreak()
        
Dim pBreak As Integer
        pBreak 
= CInt(Word.WdBreakType.wdPageBreak)
        oWordApplic.Selection.InsertBreak(pBreak)
    
End Sub

    
'转到书签
    Public Sub GotoBookMark()Sub GotoBookMark(ByVal strBookMark As String)
        
Dim missing = System.Reflection.Missing.Value
        
Dim BookMark = CInt(Word.WdGoToItem.wdGoToBookmark)
        oWordApplic.Selection.GoTo(BookMark, missing, missing, strBookMark)
    
End Sub

    
'判断书签是否存在
    Public Function BookMarkExist()Function BookMarkExist(ByVal strBookMark As StringAs Boolean
        
Dim Exist As Boolean
        Exist 
= oDocument.Bookmarks.Exists(strBookMark)
        
Return Exist
    
End Function

    
'转到文档结尾
    Public Sub GotoTheEnd()Sub GotoTheEnd()
        
Dim missing = System.Reflection.Missing.Value
        
Dim unit = Word.WdUnits.wdStory
        oWordApplic.Selection.EndKey(unit, missing)
    
End Sub

    
'转到文档开头
    Public Sub GotoTheBegining()Sub GotoTheBegining()
        
Dim missing = System.Reflection.Missing.Value
        
Dim unit = Word.WdUnits.wdStory
        oWordApplic.Selection.HomeKey(unit, missing)
    
End Sub

    
'转到表格
    Public Sub GotoTheTable()Sub GotoTheTable(ByVal ntable As Integer)
        
'Dim missing = System.Reflection.Missing.Value
        'Dim what = Word.WdGoToItem.wdGoToTable
        'Dim which = Word.WdGoToDirection.wdGoToFirst
        'Dim count = ntable

        
'oWordApplic.Selection.GoTo(what, which, count, missing)
        'oWordApplic.Selection.ClearFormatting()

        
'oWordApplic.Selection.Text = ""
        oRange = oDocument.Tables(ntable).Cell(11).Range
        oRange.Select()
    
End Sub

    
'转到表格的某个单元格
    Public Sub GotoTableCell()Sub GotoTableCell(ByVal ntable As IntegerByVal nRow As IntegerByVal nColumn As Integer)
        oRange 
= oDocument.Tables(ntable).Cell(nRow, nColumn).Range
        oRange.Select()
    
End Sub

    
'表格中转到右面的单元格
    Public Sub GotoRightCell()Sub GotoRightCell()
        
Dim missing = System.Reflection.Missing.Value
        
Dim direction = Word.WdUnits.wdCell
        oWordApplic.Selection.MoveRight(direction, missing, missing)
    
End Sub

    
'表格中转到左面的单元格
    Public Sub GotoLeftCell()Sub GotoLeftCell()
        
Dim missing = System.Reflection.Missing.Value
        
Dim direction = Word.WdUnits.wdCell
        oWordApplic.Selection.MoveLeft(direction, missing, missing)
    
End Sub

    
'表格中转到下面的单元格
    Public Sub GotoDownCell()Sub GotoDownCell()
        
Dim missing = System.Reflection.Missing.Value
        
Dim direction = Word.WdUnits.wdCell
        oWordApplic.Selection.MoveDown(direction, missing, missing)
    
End Sub

    
'表格中转到上面的单元格
    Public Sub GotoUpCell()Sub GotoUpCell()
        
Dim missing = System.Reflection.Missing.Value
        
Dim direction = Word.WdUnits.wdCell
        oWordApplic.Selection.MoveUp(direction, missing, missing)
    
End Sub

    
'插入图片
    Public Sub InsertPic()Sub InsertPic(ByVal FileName As String)
        
Dim missing = System.Reflection.Missing.Value
        oWordApplic.Selection.InlineShapes.AddPicture(FileName, 
FalseTrue, missing)
    
End Sub


End Class
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: VB.Net是一种基于.NET平台的编程语言,可以用来开发各种应用程序,包括与Microsoft Office进行交互的应用程序。VB.Net Office控件是一组用于操作和控制Microsoft Office软件(如Word、Excel、PowerPoint等)的工具集合。 VB.Net Office控件提供了丰富的功能和接口,使我们能够在应用程序中创建、打开、编辑和保存Office文档。它提供了简单而强大的对象模型,可以访问Office应用程序的各个组件(如文档、工作表、幻灯片等)以及它们的属性和方法。 使用VB.Net Office控件,我们可以编写代码来自动执行一系列操作,例如创建新的Word文档,将数据填充到Excel工作表中,从PowerPoint幻灯片中提取数据等等。我们可以通过编程方式控制Office应用程序的各个方面,如格式设置、数据导入导出、图表创建、自定义菜单等。 此外,VB.Net Office控件还提供了与Office应用程序进行交互的功能,例如打开和关闭Office应用程序、调用Office应用程序的功能和命令、处理Office应用程序的事件等。通过这些功能,我们可以根据具体需求来定制应用程序,以实现更高的自动化和扩展性。 总结来说,VB.Net Office控件是一种方便的工具,可以帮助我们在VB.Net应用程序中利用Microsoft Office软件的强大功能。它提供了丰富的功能和接口,使我们能够轻松地与Office应用程序进行交互,并能够编写代码来自动化执行各种操作。 ### 回答2: VB.NET是一种面向对象的编程语言,可以通过使用Office控件来操作Microsoft Office软件。Office控件库为开发人员提供了一组功能强大的工具,使其能够在自己的应用程序中嵌入和自动化Office应用程序。以下是VB.NET Office控件的一些主要功能和用途: 1.自动化Office应用程序:使用VB.NET和Office控件,可以自动化执行许多Office应用程序的操作,如创建和编辑Word文档、Excel表格和PowerPoint演示文稿。 2.读写Office文档:通过VB.NET和Office控件,可以轻松读取和修改Office文档中的内容,例如从Excel表格中读取数据、向Word文档中添加文本、创建幻灯片等。 3.与Outlook集成:使用VB.NET和Office控件,可以通过创建和发送电子邮件、管理日历和联系人,与Outlook进行集成。 4.自定义菜单和工具栏:通过VB.NET和Office控件,可以在Office应用程序中创建自定义菜单和工具栏,以实现特定的操作和功能。 5.数据分析和报告生成:使用VB.NET和Office控件,可以将数据导入到Excel中进行分析和报告生成,将数据展示为图表、图形和表格。 通过VB.NET和Office控件,开发人员可以轻松地利用Microsoft Office软件的功能,实现自定义的业务需求。无论是自动化处理大量文件、与Outlook进行交互还是生成专业的报告,VB.NET和Office控件都是非常有用的开发工具。 ### 回答3: VB.NET是一种流行的编程语言,用于开发Windows平台上的应用程序。Office控件是VB.NET中的一组特殊控件,用于与Microsoft Office套件中的各种应用程序进行交互和集成。 VB.NET中的Office控件包括Word控件、Excel控件和PowerPoint控件等,它们都是通过与Office应用程序的COM接口进行通信来实现功能的。 使用VB.NET中的Office控件,我们可以通过编程方式创建、打开、编辑和保存Word文档、Excel电子表格和PowerPoint演示文稿。我们可以使用这些控件来实现自定义的表格、图表和图形,以及添加、修改和删除文本、图像和媒体内容。 除了处理文档内容,Office控件还提供了与文档格式和样式相关的功能。我们可以通过控件修改字体、颜色、对齐方式等文本格式,并可以应用预定义的样式和主题。 此外,Office控件还允许我们执行一些高级操作,比如使用一组预定义的功能和方法来进行邮件合并、数据筛选和排序、图表生成和编辑等。 总之,VB.NET中的Office控件为我们提供了方便、快捷的方法来与Microsoft Office应用程序进行交互。无论是创建个人应用程序还是企业级解决方案,Office控件都能帮助我们实现与Office应用程序的无缝集成,从而提高工作效率和用户体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值