创建word应用程序时出错

本文关键字:出错 应用程序 word 创建 | 更新日期: 2023-09-27 18:27:05

这里有一个非常简单的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: 

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

创建word应用程序时出错

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