MTeck
MTeck

Reputation: 1688

Error creating word application

I have a pretty simple block of C# code here...

    static void ReadMSOfficeWordFile(string file) {
        Microsoft.Office.Interop.Word.Application msWordApp = new Microsoft.Office.Interop.Word.Application();
        object nullobj = System.Reflection.Missing.Value;
        object ofalse = false;
        object ofile = file;

        Microsoft.Office.Interop.Word.Document doc = msWordApp.Documents.Open(
                                                    ref ofile, ref nullobj, ref nullobj,
                                                    ref nullobj, ref nullobj, ref nullobj,
                                                    ref nullobj, ref nullobj, ref nullobj,
                                                    ref nullobj, ref nullobj, ref nullobj,
                                                    ref nullobj, ref nullobj, ref nullobj,
                                                    ref nullobj);
        string result = doc.Content.Text.Trim();
        doc.Close(ref ofalse, ref nullobj, ref nullobj);
        CheckLineMatch(file, result);
    }

All I want to do is get the text of the file so I can run some regular expressions against it. (Do I actually have to open it this way?)

When I try to run this, I'm getting an error on the first line in this function.

        Microsoft.Office.Interop.Word.Application msWordApp = new Microsoft.Office.Interop.Word.Application();

The error is:

System.Runtime.InteropServices.COMException was unhandled
Message="Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80040154."
Source="NPC_Check"
ErrorCode=-2147221164
StackTrace:
     at NPC_Check.Program.ReadMSOfficeWordFile(String file) in C:\Documents and Settings\mlustfie\My Documents\Visual Studio 2008\Projects\NPC_Check\NPC_Check\Program.cs:line 83
     at NPC_Check.Program.FindFiles() in C:\Documents and Settings\mlustfie\My Documents\Visual Studio 2008\Projects\NPC_Check\NPC_Check\Program.cs:line 70
     at NPC_Check.Program.Main(String[] args) in C:\Documents and Settings\mlustfie\My Documents\Visual Studio 2008\Projects\NPC_Check\NPC_Check\Program.cs:line 61
     at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
     at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
     at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
     at System.Threading.ThreadHelper.ThreadStart()
InnerException: 

I have no idea why this isn't working...

Upvotes: 1

Views: 1237

Answers (1)

Gambrinus
Gambrinus

Reputation: 2136

If the DLL that your application is attempting to use is not registered (ie the application is not installed) then you will see this error. I guess you have word installed on the computer, you're developing/running this application. Make sure that the dll needed is available.

Upvotes: 2

Related Questions