none
Set custom document properties with Word interop RRS feed

  • Question

  • 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)

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

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


    PS: When I run a sample code as a word add-in;

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

    It runs without any problems and correctly sets the property to a random GUID. But why can't I do this outside of the add-in scope in my custom program?

    Monday, October 8, 2012 2:05 PM

Answers

  • Hi Flo

    The article which you read is specific for the VSTO technology. VSTO is taking care of some of the "plumbing" required to work with custom document properties when using C#. Without VSTO, it's still possible but a bit more complicated. See:
    http://support.microsoft.com/kb/303296


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, October 8, 2012 2:21 PM
    Moderator