Set custom document properties with Word interop

前端 未结 4 1302
春和景丽
春和景丽 2020-12-10 17:01

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:



        
4条回答
  •  北海茫月
    2020-12-10 17:37

    You need to use CustomDocumentProperties, not BuiltInDocumentProperties. See MSDN reference on using Custom Document Properties in Word (and MSDN video here). You also need to check if the property exists and create it before trying to assign its value.

    Core.DocumentProperties properties = (Core.DocumentProperties)this.Application.ActiveDocument.CustomDocumentProperties;
    if (properties.Cast().Where(c => c.Name == "DocumentID").Count() == 0)
      properties.Add("DocumentID", false, MsoDocProperties.msoPropertyTypeString, Guid.NewGuid().ToString());
    var docID = properties["DocumentID"].Value.ToString();
    

提交回复
热议问题