none
Iterating Word 2007 CustomProperties RRS feed

  • Question

  • I need an example of iterating the customproperties in word 2007 via a wordaddin and if a property does not exist then add it.  If it does exist I need to update it.  Iterating may not be necessary if there is another way to check if such a property exists.

     

    Thanks

    • Edited by Mattaniah Thursday, June 17, 2010 8:29 AM
    Thursday, June 17, 2010 7:58 AM

Answers

  • Hi Mattaniah

    Something like this... Note that this is applicable only to a VSTO add-in, not a Shared Add-in. As you've posted in the VSTO forum, I'm assuming you have a VSTO add-in.

    private void btnUpdateCustomDocProp_Click(object sender, RibbonControlEventArgs e)
        {
          Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
          string propName = "testProp";
          string propValue = "testValue";
          Office.DocumentProperties props = (Office.DocumentProperties)doc.CustomDocumentProperties;
          Office.DocumentProperty docProp = ExistsDocProp(propName, props);
          if (docProp == null)
          {
            docProp = props.Add(propName, false, Office.MsoDocProperties.msoPropertyTypeString, propValue, false);
          }
          else
          {
            docProp.Value = propValue;
          }
        }
    
        private Office.DocumentProperty ExistsDocProp(string propName, Office.DocumentProperties props)
        {
          Office.DocumentProperty customDocProp = null;
          foreach (Office.DocumentProperty prop in props)
          {
            if (prop.Name == propName)
            {
              customDocProp = prop;
              break;
            }
          }
          return customDocProp;
        }

    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Mattaniah Friday, June 18, 2010 10:31 AM
    Thursday, June 17, 2010 6:25 PM
    Moderator

All replies

  • Hi Mattaniah

    No, there is unforutnately no way other than interating the collection (short of causing an error and trapping it).

    But if you want an actual code sample then you're going to have to tell us which programming language you're using, and which version of the .NET Framwork (4.0, or earlier?) you're targeting


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Mattaniah Thursday, June 17, 2010 8:27 AM
    • Unmarked as answer by Mattaniah Thursday, June 17, 2010 8:27 AM
    Thursday, June 17, 2010 8:16 AM
    Moderator
  • .net 3.0 C#
    Thursday, June 17, 2010 8:28 AM
  • Hi Mattaniah

    Something like this... Note that this is applicable only to a VSTO add-in, not a Shared Add-in. As you've posted in the VSTO forum, I'm assuming you have a VSTO add-in.

    private void btnUpdateCustomDocProp_Click(object sender, RibbonControlEventArgs e)
        {
          Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
          string propName = "testProp";
          string propValue = "testValue";
          Office.DocumentProperties props = (Office.DocumentProperties)doc.CustomDocumentProperties;
          Office.DocumentProperty docProp = ExistsDocProp(propName, props);
          if (docProp == null)
          {
            docProp = props.Add(propName, false, Office.MsoDocProperties.msoPropertyTypeString, propValue, false);
          }
          else
          {
            docProp.Value = propValue;
          }
        }
    
        private Office.DocumentProperty ExistsDocProp(string propName, Office.DocumentProperties props)
        {
          Office.DocumentProperty customDocProp = null;
          foreach (Office.DocumentProperty prop in props)
          {
            if (prop.Name == propName)
            {
              customDocProp = prop;
              break;
            }
          }
          return customDocProp;
        }

    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Mattaniah Friday, June 18, 2010 10:31 AM
    Thursday, June 17, 2010 6:25 PM
    Moderator