创建word应用程序出错

时间:2012-03-08 14:47:54

标签: 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;
        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: 

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

1 个答案:

答案 0 :(得分:2)

如果您的应用程序尝试使用的DLL未注册(即未安装应用程序),那么您将看到此错误。我猜你在计算机上安装了单词,你正在开发/运行这个应用程序。确保所需的dll可用。