none
Unable to cast object of type System._COMObject type to Microsoft.Office.Interop.MSProject.Application Class?? RRS feed

  • Question

  • I am getting the mentioned error in the code:

      mppobj= new Microsoft.Office.Interop.MSProject.Application();

     where mppobj is the object for the Application().

      What could be the problem and How can I able to fix it?
    Go Green.Save Earth.
    Tuesday, July 26, 2011 5:40 AM

Answers

  • In that case, what I usually do is overload the constructor for the Windows form with a method that accepts an object of the datatype of what I want the Windows Form to work with. In this case, it could be the Project application or it could be the file name. For example:

    Project.Application ProjApp = null;
    string fileName = "";
    public void MyForm()
    {
        InitializeComponent();
    }

    public void MyForm(Project.Application projApp)
    {
        InitializeComponent();
        ProjApp = projApp;
    }

    public void MyForm(string projFileName)
    {
        InitializeComponent();
        fileName = projFileName;
    }

     


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by venturert Tuesday, August 9, 2011 12:01 PM
    Tuesday, July 26, 2011 9:17 AM
    Moderator

All replies

  • Could you please show us:

    1. The using statements at the top of your class
    2. How mppobj is declared, exactly?
    3. What kind of solution is this? (VSTO Add-in, Windows Forms app...)


    Cindy Meister, VSTO/Word MVP
    Tuesday, July 26, 2011 6:41 AM
    Moderator
  • Using Statements:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;
    using System.Data;
    using System.Collections;
    using Microsoft.Office.Interop.MSProject;
    using System.Runtime.InteropServices;

    Declaration:

     Microsoft.Office.Interop.MSProject.Application mppobj = null;(declared globally)

    mppobj= new Microsoft.Office.Interop.MSProject.Application();

    It is a Shared Add-in where I wrote a code in the windows form class


    Go Green.Save Earth.
    Tuesday, July 26, 2011 7:17 AM
  • Thank you for the additional information.

    In which Office application is the Shared Add-in running?

    Basically, what concerns me is that the Add-in is running in Project and what you really want is to access that application instance?

    Also, please note that Shared Add-ins aren't using the VSTO technology, so at some point I'm going to move this to the General Office development forum where Shared Add-ins (and Project) are on-topic. But for the moment I'll leave it here until we establish exactly what's going on :-)


    Cindy Meister, VSTO/Word MVP
    Tuesday, July 26, 2011 7:26 AM
    Moderator
  • Hi Cindy,

     

      You are correct.. I am using the Shared Add-ins in the MS Project application. From that I want to fetch the opened file name.


    Go Green.Save Earth.
    Tuesday, July 26, 2011 8:29 AM
  • In that case, what I usually do is overload the constructor for the Windows form with a method that accepts an object of the datatype of what I want the Windows Form to work with. In this case, it could be the Project application or it could be the file name. For example:

    Project.Application ProjApp = null;
    string fileName = "";
    public void MyForm()
    {
        InitializeComponent();
    }

    public void MyForm(Project.Application projApp)
    {
        InitializeComponent();
        ProjApp = projApp;
    }

    public void MyForm(string projFileName)
    {
        InitializeComponent();
        fileName = projFileName;
    }

     


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by venturert Tuesday, August 9, 2011 12:01 PM
    Tuesday, July 26, 2011 9:17 AM
    Moderator
  • Thanks for the post. But it is not working. :(
    Go Green.Save Earth.
    Tuesday, July 26, 2011 11:49 AM
  • You need to describe how it's "not working", otherwsie no one can help you.
    Cindy Meister, VSTO/Word MVP
    Tuesday, July 26, 2011 2:26 PM
    Moderator
  • Workflow of my logic is:

      Add-In Button-->if clicked-->Showing a Form--> Browse and select a file--> submit-->Open the browsed file to read.

      Here, The opening of browsed file i used in a Class1.cs file. Here only I`m getting the mentioned error.

    Wednesday, July 27, 2011 3:18 AM
  • I'm afraid you're not providing enough information to correctly follow exactly what you're doing. I suggest you post the code for

    1. Displaying the Windows Form

    2. The constructors for the Windows Form (as per my previous suggestion)

    3. The code that should open the file

    Also, what do you mean by "submit" between selecting and opening a file?


    Cindy Meister, VSTO/Word MVP
    Wednesday, July 27, 2011 10:42 AM
    Moderator