Microsoft Office VB.NET编程,获得当前编辑文档的对象,并打印到控制台

来源:互联网 发布:2016淘宝现在好做吗 编辑:程序博客网 时间:2024/05/17 08:09

Microsoft Office VB.NET编程,获得当前编辑文档的对象,并打印到控制台

PIA COM组件安装参见微软MSDN。

Imports Microsoft.Office.Interop.Word

Imports System.Runtime.InteropServices
Module Module1

    Sub Main()
        Dim wdGlobal As GlobalClass
        Dim docs As Documents

        Dim FileName As String
        Dim doc As Document
        Try

           '获得当前Word application对象
            wdGlobal = New GlobalClass
            docs = wdGlobal.Documents

            '获得当前编辑文档对象
            If docs.Count() <> 0 Then
                doc = docs(1)
            End If
            If Not doc Is Nothing Then
                Console.WriteLine(doc.Path + "//" + doc.Name)

               '在控制台打印文档内容
                Console.Write(doc.Content.Text)
                Console.WriteLine()
            End If
        Catch ex As COMException
            Console.WriteLine(ex.Message)
        End Try
        Console.WriteLine("input return key to end")
        Console.ReadLine()
    End Sub

End Module

原创粉丝点击