使用c#

时间:2015-09-04 09:17:02

标签: c# properties ms-word

我尝试使用c#工具将自定义文档属性添加到word中。 我可以编辑内置属性的值,如作者等。

但是使用该代码,我没有例外,但是word文档中没有自定义属性。

object oDocCustomProps;
string strIndex = String.Empty;
string strValue;
Microsoft.Office.Interop.Word.Application wordApp =
    new Microsoft.Office.Interop.Word.Application { Visible = false };
Microsoft.Office.Interop.Word.Document doc =
    wordApp.Documents.Open("C:\\test.docx", ReadOnly: false, Visible: false);
oDocCustomProps = document.CustomDocumentProperties;
Type typeDocCustomProps = oDocCustomProps.GetType();
strIndex = "Testindex";
strValue = "Testvalue";
object[] oArgs = {strIndex, false, MsoDocProperties.msoPropertyTypeString, strValue};
typeDocCustomProps.InvokeMember("Add", BindingFlags.Default | BindingFlags.InvokeMethod,
    null, oDocCustomProps, oArgs);
document.save();
document.close();

修改

    public void addCustomDocumentPropertys()
    {

        object oMissing = Missing.Value;
        object oDocBuiltInProps;
        object oDocCustomProps;


        oDocBuiltInProps = oDoc.BuiltInDocumentProperties;
        Type typeDocBuiltInProps = oDocBuiltInProps.GetType();

        //Get the Author property and display it.
        string strIndex = String.Empty;
        string strValue;

        oDocCustomProps = oDoc.CustomDocumentProperties;
        Type typeDocCustomProps = oDocCustomProps.GetType();

        foreach (DataRow row in dt_customAttributes.Rows)
        {
            strIndex = row[0].ToString();
            strValue = row[1].ToString();
            object[] oArgs = {strIndex,false,
                 MsoDocProperties.msoPropertyTypeString,
                 strValue};

            typeDocCustomProps.InvokeMember("Add", BindingFlags.Default |
                                       BindingFlags.InvokeMethod, null,
                                       oDocCustomProps, oArgs);
        }
    }

3 个答案:

答案 0 :(得分:2)

我有同样奇怪的事情,如果你只改变CustomDocumentProperties,事实证明这个词不会保存!你需要通过将doc.saved设置为false来告诉它必须保存。

希望这会有所帮助。 最好的祝福 弗兰克

答案 1 :(得分:2)

现在您可以执行以下操作:

  dynamic properties = null;
  try
  {
    properties = Globals.ThisAddIn.Application.ActiveDocument.CustomDocumentProperties;

    // Testing purposes:
    properties.Add("SomeCustomProp", false, MsoDocProperties.msoPropertyTypeString, "SomeCustomValue");
    properties.Add("SomeOtherCustomProp", false, MsoDocProperties.msoPropertyTypeString, "SomeOtherValue");
    properties.Add("SomeNumericCustomProp", false, MsoDocProperties.msoPropertyTypeNumber, 1982331);
  }
  finally
  {
    Marshal.ReleaseComObject(properties);
  }

曾经推荐使用释放COM对象,但是我不确定是否仍然需要它。

也可以代替

properties = Globals.ThisAddIn.Application.ActiveDocument.CustomDocumentProperties;

只需使用您自己对应用程序的引用,然后使用.ActiveDocument.CustomDocumentProperties;


注意:这与问题中的编辑相同-仅使用动态而不是手动反射。

答案 2 :(得分:0)

你可以做这样的事情

Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application { Visible = false };
Microsoft.Office.Interop.Word.Document Doc = wordApp.Documents.Open("C:\\test.docx", ReadOnly: false, Visible: false);

Doc.BuiltInDocumentProperties["Title"].Value = this.Title.Text;
Doc.BuiltInDocumentProperties["Subject"].Value = this.Subject.Text;
Doc.BuiltInDocumentProperties["Category"].Value = this.Category.Text;
Doc.BuiltInDocumentProperties["Keywords"].Value = this.Keywords.Text;
Doc.BuiltInDocumentProperties["Author"].Value = this.Author.Text;
Doc.BuiltInDocumentProperties["Comments"].Value = this.Comments.Text;