Can't open Office document with Interop

1

Code:

ApplicationClass projectApp = new ApplicationClass();

if (projectApp.FileOpen(path, true, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
PjPoolOpen.pjDoNotOpenPool, Missing.Value, Missing.Value, Missing.Value, Missing.Value))
{
    projects = new List<Project>(projectApp.Projects.Cast<Project>());
    foreach (Project project in projects)
    {
        MSProjectProject msProject = new MSProjectProject();

        msProject.Author = project.Author.ToString(); // ERROR HERE
        msProject.Name = project.Name;
    }
}

Exception:

Unable to cast COM object of type 'Microsoft.Office.Interop.MSProject.ProjectClass' to interface type 'Microsoft.Office.Interop.MSProject._IProjectDoc'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00020B00-0000-0000-C000-000000000046}' failed due to the following error: The RPC server is unavailable. (Exception from HRESULT: 0x800706BA).

Exception 2: (This one does not get caught by the try block)

DisconnectedContext was detected

Transition into COM context 0x265048 for this RuntimeCallableWrapper failed with the following error: The requested object does not exist. (Exception from HRESULT: 0x80010114). This is typically because the COM context 0x265048 where this RuntimeCallableWrapper was created has been disconnected or it is busy doing something else and cannot process the context transition. No proxy will be used to service the request on the COM component and calls will be made to the COM component directly. This may cause corruption or data loss. To avoid this problem, please ensure that all COM contexts/apartments/threads stay alive and are available for context transition, until the application is completely done with the RuntimeCallableWrappers that represents COM components that live inside them.

When I first access the project the exception is thrown. What am I doing wrong?

Also, I'm new to Interop and it bothers me that when I access a file, it opens with office. Isn't there a way to only peek its content and not open it?

Interop Runtime Version: v2.0.50727;

Interop Version: 14.0.0.0;

Project Version: 2013 (15.0.4569.1504);

c#
office-interop
com-interop
ms-project
asked on Stack Overflow Aug 26, 2014 by Matheus Simon • edited Aug 26, 2014 by Matheus Simon

1 Answer

0

Resolution is simple: Stop using Interop, it is GARBAGE. It's messy, takes forever to process documents, opens the documents before it can process it and sometimes will simply not work and not tell you why, except when it does and you have problems like the above, where nobody can help.

Though MPXJ implements Java inside .NET, it's still better than Interop!

answered on Stack Overflow Aug 28, 2014 by Matheus Simon

User contributions licensed under CC BY-SA 3.0