none
Strange Problem with MS Project 2007 Add-in when get CustomDocumentProperties RRS feed

  • Question

  • I used VS 2010 created a very simple Project 2007 Add-in. following is the code

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
          var project = this.Application.ActiveProject;
          var properties = (DocumentProperties)project.CustomDocumentProperties;
          var resources = project.Resources;
          var tasks = project.Tasks;
    
    }
    

    there are only 4 line of code, but it will get an exception in 2nd line, the error message say cannot convert type "string" to "Microsoft.Office.Core.DocumentProperties"

    but most strange things is if I comment out any line of following code it will work well, but if both of them exists, the exception happen, be note the both line is after the line of exception happen, it is even not be executed when exception happen.

    var resources = project.Resources;
    var tasks = project.Tasks;
    



    Tianhon
    Tuesday, January 17, 2012 2:27 AM

Answers

  • Finally, we found a workaround: to change second line of code as below:

    var properties = (DocumentProperties)project.GetType().InvokeMember("CustomDocumentProperties", BindingFlags.GetProperty, null, project, null);

    then everything working well.

     

    Still doubt why my orginal code not working?


    Tianhon
    • Marked as answer by TianHon Thursday, January 19, 2012 6:51 AM
    • Edited by TianHon Thursday, January 19, 2012 6:53 AM
    Thursday, January 19, 2012 6:51 AM

All replies

  • Hi Tianhon,

    I am doing the research about your problem. Appreciate your patience.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Thursday, January 19, 2012 2:03 AM
  • Tianhon, I did some research about this problem and I think the second line code need to be changed like follows to avoid this issue:

     var properties = project.CustomDocumentProperties;
    

    We do not have to explicity cast the return value to be DocumentProperties.

    Hope this can help you.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Thursday, January 19, 2012 3:20 AM
  • Thanks Bruce, it will can avoid the exception if made the change, but I will then cannot get the DocumentProperties object (the variable "properties" will become string type with value is my login name). I need it to read and modify the document properties.

    All of the lines of code is simple and should support according document, I guess it maybe a prduct bug, but is there any workaround? my objective is to manage the document properties and at same time I also want manage the resouces and tasks.


    Tianhon
    • Edited by TianHon Thursday, January 19, 2012 3:32 AM
    Thursday, January 19, 2012 3:29 AM
  • Finally, we found a workaround: to change second line of code as below:

    var properties = (DocumentProperties)project.GetType().InvokeMember("CustomDocumentProperties", BindingFlags.GetProperty, null, project, null);

    then everything working well.

     

    Still doubt why my orginal code not working?


    Tianhon
    • Marked as answer by TianHon Thursday, January 19, 2012 6:51 AM
    • Edited by TianHon Thursday, January 19, 2012 6:53 AM
    Thursday, January 19, 2012 6:51 AM