我嘗試使用c#工具將自定義文檔屬性添加到單詞。 我可以像編輯等編輯內置屬性的值。將自定義文檔屬性添加到單詞用c#
但是,使用該代碼,我沒有得到任何例外,但在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);
}
}
我的編輯適合我......但我現在不爲什麼:D – Ezak