vb.net操作Word文档类

首先在工程添加引用Microsoft Word 12.0 Object Library(添加的类型库版本会依据用户机子不同而不同)


Public Class Word
    Private WordApp As Microsoft.Office.Interop.Word.Application
    Private WordDocs As Microsoft.Office.Interop.Word.Document

    '构造函数
    Public Sub New()
        WordApp = New Microsoft.Office.Interop.Word.Application
    End Sub

    '是否显示Word文件
    Public Sub IsVisible(ByVal visible As Boolean)
        WordApp.Visible = visible
    End Sub

    '按照filename给出路径打开Excel文件
    Public Sub OpenWord(ByVal filename As String)
        WordDocs = WordApp.Documents.Open(filename)
    End Sub

    '删除光标处内容
    Public Sub DeleteSelection()
        WordApp.Selection.Delete()
    End Sub

    '在光标处插入图片
    Public Sub InsertPic(ByVal picname As String)
        WordApp.Selection.InlineShapes.AddPicture(picname)
    End Sub

    '光标处插入文字
    Public Sub InsertText(ByVal text As String)
        WordApp.Selection.TypeText(text)
    End Sub

    '光标右移一位
    Public Sub SelectionMoveRight()
        WordApp.Selection.MoveRight()
    End Sub

    '字体是否粗体
    Public Sub IsBold(ByVal bold As Boolean)
        WordApp.Selection.Font.Bold = bold
    End Sub

    '保存Word文档
    Public Sub Save()
        WordApp.Documents.Save()
    End Sub

    '打印Word文档
    Public Sub Print()
        WordApp.ActiveDocument.PrintOut()
    End Sub

    '关闭Word文档
    Public Sub Close()
        WordDocs = Nothing
        WordApp.Quit()
        System.Runtime.InteropServices.Marshal.ReleaseComObject(WordApp)
        WordApp = Nothing
    End Sub
End Class


发布了2 篇原创文章 · 获赞 1 · 访问量 3646
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览