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

本文档介绍如何使用VB.NET通过COM组件与Microsoft Office交互,获取当前正在编辑的Word文档对象,并将其内容打印到控制台。详细步骤包括安装PIA组件,导入必要的库,获取Application对象,访问Documents集合,以及处理可能的COMException。

3826

被折叠的 条评论
为什么被折叠?



