none
Microsoft Project issue: Unable to cast object of type 'System.__ComObject' to type 'Microsoft.Office.Interop.MSProject.ApplicationClass'. RRS feed

  • Question

  • Code:

    C# VSTO, click a button to open an existing .MPP file, will get error. "Unable to cast object of type 'System.__ComObject' to type 'Microsoft.Office.Interop.MSProject.ApplicationClass'."

    I have Office 2013, VS 2012/VS2015 installed on my laptop. Do you know how to fix this issue? thx.

            private void btnLoad_Click(object sender, EventArgs e)
            {
    
                Microsoft.Office.Interop.MSProject.Application app = new Microsoft.Office.Interop.MSProject.Application();
                app.FileOpenEx(
                    "C:\\Project\\xxx.mpp",
                    false,
                    Type.Missing,
                    Type.Missing,
                    Type.Missing,
                    Type.Missing,
                    Type.Missing,
                    Type.Missing,
                    Type.Missing,
                    Type.Missing,
                    Type.Missing,
                    Microsoft.Office.Interop.MSProject.PjPoolOpen.pjPoolReadWrite,
                    Type.Missing,
                    Type.Missing,
                    Type.Missing,
                    Type.Missing,
                    Type.Missing);
    
    }

    Wednesday, April 6, 2016 6:46 AM

Answers

  • >>>C# VSTO, click a button to open an existing .MPP file, will get error. "Unable to cast object of type 'System.__ComObject' to type 'Microsoft.Office.Interop.MSProject.ApplicationClass'."

    I have Office 2013, VS 2012/VS2015 installed on my laptop. Do you know how to fix this issue?<<<

    According to your description and sample codes, I have made a sample and reproduced this issue. So I suggest that you could overload the constructor of the Form with the Project application object:

    FormProjects projectsForm = new FormProjects(Globals.ThisAddIn.Application);
    projectsForm.Show();

    public partial class FormProjects : Form
    {
        public FormProjects()
        {
            InitializeComponent();
        }
    
        Microsoft.Office.Interop.MSProject.Application ProjApp = null;
           
        public FormProjects(Microsoft.Office.Interop.MSProject.Application projApp)
        {
            InitializeComponent();
            ProjApp = projApp;
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
    
            //Microsoft.Office.Interop.MSProject.Application app = new Microsoft.Office.Interop.MSProject.Application();
            ProjApp.FileOpenEx(
                "D:\\test.mpp",
                false,
                Type.Missing,
                Type.Missing,
                Type.Missing,
                Type.Missing,
                Type.Missing,
                Type.Missing,
                Type.Missing,
                Type.Missing,
                Type.Missing,
                Microsoft.Office.Interop.MSProject.PjPoolOpen.pjPoolReadWrite,
                Type.Missing,
                Type.Missing,
                Type.Missing,
                Type.Missing,
                Type.Missing);
        }
    }



    • Edited by David_JunFeng Thursday, April 7, 2016 7:23 AM
    • Marked as answer by Terry NX Wednesday, April 13, 2016 1:49 AM
    Thursday, April 7, 2016 7:21 AM

All replies

  • Hello Terry,

    It looks like something is wrong with your windows registry keys. Take a look at the following page - unable to cast COM object of type 'microsoft.Office.Interop.Excel.ApplicationClass' to 'microsoft.Office.Interop.Excel.Application'" which describes exactly the same issue.

    Also you may try to use the late-binding technology for creating an instance of the Application class. See Activator.CreateInstance for more information.


    [custom.development]

    Wednesday, April 6, 2016 11:03 AM
  • Hi Eugene,

    Thanks for your help, I'll try your approach and then give an update.

    --Terry

    Thursday, April 7, 2016 2:36 AM
  • >>>C# VSTO, click a button to open an existing .MPP file, will get error. "Unable to cast object of type 'System.__ComObject' to type 'Microsoft.Office.Interop.MSProject.ApplicationClass'."

    I have Office 2013, VS 2012/VS2015 installed on my laptop. Do you know how to fix this issue?<<<

    According to your description and sample codes, I have made a sample and reproduced this issue. So I suggest that you could overload the constructor of the Form with the Project application object:

    FormProjects projectsForm = new FormProjects(Globals.ThisAddIn.Application);
    projectsForm.Show();

    public partial class FormProjects : Form
    {
        public FormProjects()
        {
            InitializeComponent();
        }
    
        Microsoft.Office.Interop.MSProject.Application ProjApp = null;
           
        public FormProjects(Microsoft.Office.Interop.MSProject.Application projApp)
        {
            InitializeComponent();
            ProjApp = projApp;
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
    
            //Microsoft.Office.Interop.MSProject.Application app = new Microsoft.Office.Interop.MSProject.Application();
            ProjApp.FileOpenEx(
                "D:\\test.mpp",
                false,
                Type.Missing,
                Type.Missing,
                Type.Missing,
                Type.Missing,
                Type.Missing,
                Type.Missing,
                Type.Missing,
                Type.Missing,
                Type.Missing,
                Microsoft.Office.Interop.MSProject.PjPoolOpen.pjPoolReadWrite,
                Type.Missing,
                Type.Missing,
                Type.Missing,
                Type.Missing,
                Type.Missing);
        }
    }



    • Edited by David_JunFeng Thursday, April 7, 2016 7:23 AM
    • Marked as answer by Terry NX Wednesday, April 13, 2016 1:49 AM
    Thursday, April 7, 2016 7:21 AM
  • Hi David,

    Thank you!! your solution worked well. This issue happens just because it is a MSP Addin not a standalone application, so I need to new application in this way: "Globals.ThisAddIn.Application"

    --Terry


    • Edited by Terry NX Wednesday, April 13, 2016 1:51 AM
    Wednesday, April 13, 2016 1:49 AM