田草博客

互联网田草博客


网友交流QQ群:11740834 需注明申请加入原因

微信 公众号:ByCAD

邮箱:tiancao1001x126.com
ByCAD,微信公众号
首页 | 普通 | 电脑 | AutoCAD | VB/VB.NET | FLash | 结构 | 建筑 | 电影 | BIM | 规范 | 软件 | ID
-随机-|-分布-
-博客论坛-|-﨣﨤﨧﨨-
-网站导航-|-规范下载-
-BelovedFLash欣赏-

用户登陆
用户:
密码:
 

站点日历
73 2024 - 3 48
     12
3456789
10111213141516
17181920212223
24252627282930
31


站点统计

最新评论



VB.Net 让DataGridView显示行号 网页在线视频置顶观看
未知 Vb.net 操作Word的类   [ 日期:2011-10-16 ]   [ 来自:转帖 ]  HTML

Imports Microsoft.Office.Interop
Public Class WordCls
    Private oWordApplic As Word.Application
    Private oDocument As Word.Document
    Private oRange As Word.Range
    Private oSelection As Word.Selection
    Public Sub New()
        '激活com word接口
        oWordApplic = New Word.Application
        oWordApplic.Visible = True
    End Sub
    '设置选定文本
    Public Sub SetRange(ByVal para As Integer)
        oRange = oDocument.Paragraphs(para).Range
        oRange.Select()
    End Sub
    Public Sub SetRange(ByVal para As Integer, ByVal sent As Integer)
        oRange = oDocument.Paragraphs(para).Range.Sentences(sent)
        oRange.Select()
    End Sub
    Public Sub SetRange(ByVal startpoint As Integer, ByVal endpoint As Integer, ByVal flag As Boolean)
        If flag = True Then
            oRange = oDocument.Range(startpoint, endpoint)
            oRange.Select()
        Else

        End If
    End Sub

    '生成空的新文档
    Public 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(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(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(ByVal FileName As String, ByVal 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()
        Dim missing = System.Reflection.Missing.Value
        oWordApplic.Quit()
        System.Runtime.InteropServices.Marshal.ReleaseComObject(oWordApplic)
        oWordApplic = Nothing
    End Sub
    '关闭所有打开的文档
    Public Sub CloseAllDocuments()
        oWordApplic.Documents.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
    End Sub
    '关闭当前的文档
    Public Sub CloseCurrentDocument()
        oDocument.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
    End Sub
    '保存当前文档
    Public Sub Save()
        Try
            oDocument.Save()
        Catch
            MsgBox(Err.Description)
        End Try
    End Sub
    '另存为文档
    Public 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(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(ByVal text As String)
        oWordApplic.Selection.TypeText(text)
    End Sub
    '插入一个空行
    Public Sub InsertLineBreak()
        oWordApplic.Selection.TypeParagraph()
    End Sub
    '插入指定行数的空行
    Public Sub InsertLineBreak(ByVal lines As Integer)
        Dim i As Integer
        For i = 1 To lines
            oWordApplic.Selection.TypeParagraph()
        Next
    End Sub
    '插入表格
    Public 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(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(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()
        oWordApplic.Selection.Font.Bold = 0
        oWordApplic.Selection.Font.Italic = 0
        oWordApplic.Selection.Font.Subscript = 0
    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
    '转到书签
    Public 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(ByVal strBookMark As String) As Boolean
        Dim Exist As Boolean
        Exist = oDocument.Bookmarks.Exists(strBookMark)
        Return Exist
    End Function
    '转到文档结尾
    Public Sub GotoTheEnd()
        Dim missing = System.Reflection.Missing.Value
        Dim unit = Word.WdUnits.wdStory
        oWordApplic.Selection.EndKey(unit, missing)
    End Sub
    '转到文档开头
    Public Sub GotoTheBegining()
        Dim missing = System.Reflection.Missing.Value
        Dim unit = Word.WdUnits.wdStory
        oWordApplic.Selection.HomeKey(unit, missing)
    End Sub
    '转到表格
    Public 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(1, 1).Range
        oRange.Select()
    End Sub
    '转到表格的某个单元格
    Public Sub GotoTableCell(ByVal ntable As Integer, ByVal nRow As Integer, ByVal nColumn As Integer)
        oRange = oDocument.Tables(ntable).Cell(nRow, nColumn).Range
        oRange.Select()
    End Sub
    '表格中转到右面的单元格
    Public Sub GotoRightCell()
        Dim missing = System.Reflection.Missing.Value
        Dim direction = Word.WdUnits.wdCell
        oWordApplic.Selection.MoveRight(direction, missing, missing)
    End Sub
    '表格中转到左面的单元格
    Public Sub GotoLeftCell()
        Dim missing = System.Reflection.Missing.Value
        Dim direction = Word.WdUnits.wdCell
        oWordApplic.Selection.MoveLeft(direction, missing, missing)
    End Sub
    '表格中转到下面的单元格
    Public Sub GotoDownCell()
        Dim missing = System.Reflection.Missing.Value
        Dim direction = Word.WdUnits.wdCell
        oWordApplic.Selection.MoveDown(direction, missing, missing)
    End Sub
    '表格中转到上面的单元格
    Public Sub GotoUpCell()
        Dim missing = System.Reflection.Missing.Value
        Dim direction = Word.WdUnits.wdCell
        oWordApplic.Selection.MoveUp(direction, missing, missing)
    End Sub
    '插入图片
    Public Sub InsertPic(ByVal FileName As String)
        Dim missing = System.Reflection.Missing.Value
        oWordApplic.Selection.InlineShapes.AddPicture(FileName, False, True, missing)
    End Sub

End Class




暂时没有评论
发表评论 - 不要忘了输入验证码哦!
作者: 用户:  密码:   注册? 验证:  防止恶意留言请输入问题答案:2*5=?  
评论:

禁止表情
禁止UBB
禁止图片
识别链接
识别关键字

字体样式 文字大小 文字颜色
插入粗体文本 插入斜体文本 插入下划线
左对齐 居中对齐 右对齐
插入超级链接 插入邮件地址 插入图像
插入 Flash 插入代码 插入引用
插入列表 插入音频文件 插入视频文件
插入缩进符合
点击下载按钮 下标 上标
水平线 简介分割标记
表  情
 
Tiancao Blog All Rights Reserved 田草博客 版权所有
Copyright ©