VB.net读取Word文档属性

读取写入前需创建Word的引用,打开文件并获取Word的Document对象。

需要说明的一点是,Word自定义属性所能写入的长度是有限制的,约255个字符。

       '对自定义属性进行读取
Dim Properties = SourceDoc.CustomDocumentProperties Dim PropertyType As Type = Properties.GetType Try Dim Authorprop = PropertyType.InvokeMember("Item", Reflection.BindingFlags.Default Or Reflection.BindingFlags.GetProperty, Nothing, Properties, New Object() {"备注"}) ResultString = Authorprop.GetType.InvokeMember("Value", Reflection.BindingFlags.Default Or Reflection.BindingFlags.GetProperty, Nothing, Authorprop, New Object() {}) Catch ex As Exception End Try
   '写入 
    Dim Authorprop = PropertyType.InvokeMember("Item", Reflection.BindingFlags.Default Or Reflection.BindingFlags.SetProperty, Nothing, properties, New Object() {"备注", ResultString})

另一种写法:

’增加新属性
SourceDoc.CustomDocumentProperties.Add(Name := "PropertyName", LinkToContent := False, Type := Microsoft.Office.Core.MsoDocProperties.msoPropertyTypeString, Value := "PropertyValue")

'修改属性
SourceDoc.CustomDocumentProperties("PropertyName").Value = PropertyValue

'获取属性值
PropertyValue =SourceDoc.CustomDocumentProperties("PropertyName").Value 

 

 '读取内置属性,以备注为例
 ResultString= SourceDoc.BuiltInDocumentProperties(Microsoft.Office.Interop.Word.WdBuiltInProperty.wdPropertyComments).value

 

读取写入操作后可以通过在Word文档上右键->属性查看效果,但当Word文档处于打开状态时,文档上右键是没有“自定义”和“摘要”这两个Tab的。

转载于:https://www.cnblogs.com/YuanLH/archive/2013/02/19/2917740.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值