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

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

你可能感兴趣的:(编程,Microsoft,Office,vb,VB.NET)