C# 创建word应用程序时出错

C# 创建word应用程序时出错,c#,office-interop,C#,Office Interop,我这里有一个非常简单的C代码块 static void ReadMSOfficeWordFile(string file) { Microsoft.Office.Interop.Word.Application msWordApp = new Microsoft.Office.Interop.Word.Application(); object nullobj = System.Reflection.Missing.Value; objec

我这里有一个非常简单的C代码块

    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);
    }
我所要做的就是获取文件的文本,以便对其运行一些正则表达式。(我真的必须这样打开吗?)

当我尝试运行这个函数时,我在函数的第一行遇到了一个错误

        Microsoft.Office.Interop.Word.Application msWordApp = new Microsoft.Office.Interop.Word.Application();
错误是:

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: 

我不知道为什么这不起作用…

如果应用程序尝试使用的DLL未注册(即应用程序未安装),则您将看到此错误。我想你已经在电脑上安装了word,你正在开发/运行这个应用程序。确保所需的dll可用。

我添加了引用,但没有安装MS Office。我想这应该是显而易见的。。。。我会把它安装好,看看它是怎么回事。:)