none
How to delete custom properties from a word doc RRS feed

  • Question

  • Hi,

    I'm adding and editing custom properties in my word documents, with the methods below. BUT, i would like a way to delete the properties again.

    How would i do that?

      public static void AddCustomProperty(MSWord.Document WordDoc, string strPropName, string strPropValue)
      {
        object oDocCustomProps;
        Type typeDocCustomProps;
    
        oDocCustomProps = WordDoc.CustomDocumentProperties;
        typeDocCustomProps = oDocCustomProps.GetType();
    
        // Add property
        object[] oArgs = {strPropName,false,
                  MsoDocProperties.msoPropertyTypeString,
                  strPropValue};
    
        object returnValue = typeDocCustomProps.InvokeMember("Add", System.Reflection.BindingFlags.Default |
                      System.Reflection.BindingFlags.InvokeMethod, null,
                      oDocCustomProps, oArgs);
      }
    
      public static void SetCustomProperty(MSWord.Document WordDoc, string strPropName, string strPropValue)
      {
        object oDocCustomProps = WordDoc.CustomDocumentProperties;
        Type typeDocCustomProps = oDocCustomProps.GetType();
    
        object oCustomProp = typeDocCustomProps.InvokeMember("Item",
                      System.Reflection.BindingFlags.GetProperty,
                      null, oDocCustomProps,
                      new object[] { strPropName });
    
        Type typeDocItemProp = oCustomProp.GetType();
        typeDocItemProp.InvokeMember("Item",
                      System.Reflection.BindingFlags.SetProperty,
                      null, oDocCustomProps,
                      new object[] { strPropName, strPropValue });
      }
    


    Nicolai Søndergaard - LM Wind Power A/S
    Tuesday, February 1, 2011 11:44 AM

Answers

  • Hi Nicolai

    There's a Delete method for a DocumentProperty object. Have you tried using that? You'd need BindingFlags.InvokeMethod to execute a method.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Nicoolai Wednesday, February 2, 2011 8:54 AM
    Tuesday, February 1, 2011 4:34 PM
    Moderator

All replies

  • Hi Nicolai

    There's a Delete method for a DocumentProperty object. Have you tried using that? You'd need BindingFlags.InvokeMethod to execute a method.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Nicoolai Wednesday, February 2, 2011 8:54 AM
    Tuesday, February 1, 2011 4:34 PM
    Moderator
  • Hi Nicolai

    There's a Delete method for a DocumentProperty object. Have you tried using that? You'd need BindingFlags.InvokeMethod to execute a method.


    Cindy Meister, VSTO/Word MVP

    It sounds like that is what i need, but how do i cast WordDoc.CustomDocumentProperties?

    I have tried this, and that didn't work out too well..

    DocumentProperty[] DocProps = (DocumentProperty[])WordDoc.CustomDocumentProperties;
    


    Nicolai Søndergaard - LM Wind Power A/S
    Wednesday, February 2, 2011 8:01 AM
  • Got it to work with this

      public static void DelCustomProperty(MSWord.Document WordDoc, string strPropName)
      {
        object oDocCustomProps = WordDoc.CustomDocumentProperties;
        Type typeDocCustomProps = oDocCustomProps.GetType();
    
        object oCustomProp = typeDocCustomProps.InvokeMember("Item",
                      System.Reflection.BindingFlags.GetProperty,
                      null, oDocCustomProps,
                      new object[] { strPropName });
        
        DocumentProperty DocProp = (DocumentProperty)oCustomProp;
        DocProp.Delete();
      }
    


    Nicolai Søndergaard - LM Wind Power A/S
    Wednesday, February 2, 2011 8:54 AM