使用Word互操作设置自定义文档属性 [英] Set custom document properties with Word interop

查看:117
本文介绍了使用Word互操作设置自定义文档属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置一些用C#代码创建的Word文档的自定义文档属性.为此,我遵循了此MSDN文章,并提出了此代码:

I want to set some custom document properties of a word document I'm creating in my C# code. To do this, I followed this MSDN article and came up with this code:

using Word = Microsoft.Office.Interop.Word; // Version 12.0.0.0
word = new Word.Application();
word.Visible = false;
Word._Document doc = word.Documents.Add(ref missing, ref missing, ref missing, ref missing);
logger.Info("Setting document properties");
Core.DocumentProperties properties = (Core.DocumentProperties)doc.BuiltInDocumentProperties;
properties["Codice_documento"].Value = args[3];
properties["Versione_documento"].Value = args[4];

不幸的是,每当到达代码时,我都会收到此错误:

Unfortunately, I get this error whenever it reaches the code:

HRESULT:0x80004002(E_NOINTERFACE)

HRESULT: 0x80004002 (E_NOINTERFACE)

那是为什么?我完全按照我的MSDN所述使用了接口,为什么它不起作用?

Why is that? I used the interfaces exactly as described my MSDN, why doesn't it work?

我正在将Interop用于Office 2010和.net 3.5

I'm using Interop for office 2010 and .net 3.5

推荐答案

After asking the question in the MSDN forums, the answer was brought up. The problem is, that the way I tried was specific to VSTO. Due to my unknowing, I mixed up VSTO, Interop and other definitions, thus tagging this question wrong.

现在可以使用以下代码运行:

It now works using the following code:

logger.Info("Setting document properties");
object properties = doc.CustomDocumentProperties;
Type propertiesType = properties.GetType();

object[] documentCodeProperty = { "Codice_documento", false, Core.MsoDocProperties.msoPropertyTypeString, args[3] };
object[] documentVersionPoperty = { "Versione_documento", false, Core.MsoDocProperties.msoPropertyTypeString, args[4] };

propertiesType.InvokeMember("Add", BindingFlags.InvokeMethod, null, properties, documentCodeProperty);
propertiesType.InvokeMember("Add", BindingFlags.InvokeMethod, null, properties, documentVersionPoperty);

这篇关于使用Word互操作设置自定义文档属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆