Storing custom metadata in a Word 2010 document(Interop)

Odpovědět Storing custom metadata in a Word 2010 document(Interop)

  • 13. března 2012 7:26
     
      Obsahuje kód

    Good Morning

    I would like to know if there is a way to store a custom object inside a Word 2010 document and if you could please point me to any helpful resources on how this can be done?

    Basically while the document is open in Word I create a new instance of my Metadata class shown below and populate it with custom data,when the document is saved on the local machine I want to somehow embed this object in the Word document so next time it's open i have access to the properties defined in the class below:

    I'm using C# .NET 4.0 and Word 2010 vsto

        public class Metadata
        {
            public string TemplateName { getset; }
            public string TemplateDescription { getset; }
            public List<string> Languages { getset; }
            public List<string> Categories { getset; }
            public List<string> Types { getset; }
        }

Všechny reakce

  • 13. března 2012 9:19
     
     Odpovědět Obsahuje kód

    Hi Denys,

    You can use the Documents Variables property. I don't think you can store entire .net objects innside. But you can create a custom variable for each of your metadata properties.

    EXAMPLE
    To create a variable on a Word document:

    wordDocument.Variables.Add("TemplateName", Metadata.TemplateName.ToString());

    To retreive the value at a later time:

    Variable templateNameVariable = wordDocument.Variables["TemplateName"];
    string templateName = templateNameVariable.ToString();
    Give it a try :)

  • 13. března 2012 10:01
     
     
    or you can use JSON or XML serialization.
  • 14. března 2012 4:55
     
     Odpovědět Obsahuje kód

    Thanks, used serialization and custom properties

            private void AddCustomProperty(string name, string value, Office.MsoDocProperties type)
            {
                object missing = System.Reflection.Missing.Value;
                Office.DocumentProperties props = (Office.DocumentProperties)CurrentDocument.CustomDocumentProperties;
                props.Add(name, false, type, value, missing);
            }
  • 14. března 2012 8:42
     
     
    just remember that user will see it in document panel.