C# Interop.Word Documents.Open 为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10837437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:28:21  来源:igfitidea点击:

Interop.Word Documents.Open is null

c#asp.netms-wordasp.net-4.0openxml

提问by nav100

I am trying to open .docx file and convert to PDF using Interop.Word.Application. It works as console application but If I use the same in my web application it doesn't work. I tried to see the permissions on the folder. I gave 'Network Service' with full control, but still I get object reference not set at word.Documents.Open. Could you please let me know what could be the problem? I am stuck with this error. Please let me know. I appreciate any suggestions. Thank you.

我正在尝试打开 .docx 文件并使用 Interop.Word.Application 转换为 PDF。它作为控制台应用程序工作,但如果我在我的 Web 应用程序中使用它,它就不起作用。我试图查看文件夹的权限。我完全控制了“网络服务”,但仍然没有在 word.Documents.Open 中设置对象引用。你能告诉我可能是什么问题吗?我被这个错误困住了。请告诉我。我很感激任何建议。谢谢你。

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

    object oMissing = System.Reflection.Missing.Value;

    word.Visible = false;
    word.ScreenUpdating = false;
    string fileName = @"c:\OUTPUT\test.docx");
    Document doc = word.Documents.Open(filename, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing, ref oMissing);
        doc.Activate();

回答by Killnine

As Paulie stated in his comment on OP, I think the issue is largely because of the libraries on the webserver. YOu might be able to hack to get this working on a web app, but I'd advise against it.

正如保利在他对 OP 的评论中所说的那样,我认为这个问题主要是因为网络服务器上的库。您可能可以通过 hack 使其在网络应用程序上运行,但我建议您不要这样做。

EDIT: Argh, I misread the post and just assumed Excel. That's what most people look for when using Office.Interop in my experience ;)

编辑:啊,我误读了这篇文章,只是假设了 Excel。根据我的经验,这就是大多数人在使用 Office.Interop 时所寻找的;)

There is an open library that works with DocX formats (enter link description here). Unfortunatelyhe admits on the page that conversions to formats like PDF is pretty much impossible without the Office.Interop libraries (http://cathalscorner.blogspot.com/2009/10/converting-docx-into-doc-pdf-html.html)

有一个使用 DocX 格式的开放库(在此处输入链接描述)。不幸的是,他在页面上承认,如果没有 Office.Interop 库,几乎不可能转换为 PDF 等格式(http://cathalscorner.blogspot.com/2009/10/converting-docx-into-doc-pdf-html.html)

Hereis a 'promising' alternative, but YMMV.

是一个“有希望”的替代方案,但 YMMV。

回答by Ben

It is most likely that you have a different word dll on your web server, perhaps an older one. I suggest that you reference a specific dll and copy it to the web server, so you can be sure that you are using the same word dll that you tested your code with and you know it works. There might be some problems when using interop, however, word interop is the only one which you can be sure will convert word files most correctly.

很可能您的 Web 服务器上有一个不同的词 dll,可能是旧的。我建议您引用一个特定的 dll 并将其复制到 Web 服务器,这样您就可以确定您使用的 dll 与您测试代码时所用的词相同,并且您知道它可以工作。使用互操作时可能会出现一些问题,但是,单词互操作是唯一可以确定最正确地转换单词文件的操作。

回答by Gianni B.

If you are still looking for the answer I have found it just a moment ago for my project.

如果您仍在寻找答案,我刚刚为我的项目找到了答案。

Open DCOM Config Settings:

打开 DCOM 配置设置:

  1. Start -> dcomcnfg.exe
  2. Computer
  3. Local Computer
  4. Config DCOM
  5. Search for Microsoft Word 97-2003 Documents-> Properties
  1. 开始 -> dcomcnfg.exe
  2. 计算机
  3. 本地计算机
  4. 配置DCOM
  5. 搜索Microsoft Word 97-2003 Documents-> 属性

Tab Identity, change from Launching Userto Interactive User

Tab Identity,从 更改Launching UserInteractive User

回答by nacx08

Try this it may help you.

试试这个它可能会帮助你。

  • Create a new "Desktop" directory inside of "C:\Windows\SysWOW64\config\systemprofile\"
  • 在“C:\Windows\SysWOW64\config\systemprofile\”中创建一个新的“Desktop”目录

it works for me after a long long long day searching for the solution.

经过漫长漫长的一天寻找解决方案后,它对我有用。

It seams to be a profile problem.

这似乎是一个配置文件问题。

回答by vmassuchetto

To solve this I had to do both things:

为了解决这个问题,我必须做两件事:

  • Create the directories:

    • C:\Windows\System32\config\systemprofile\AppData\Local\Microsoft \Windows\INetCac?he
    • C:\Windows\System32\config\systemprofile\Desktop
  • Enable DCOM setting as @gianni-b says.

  • 创建目录:

    • C:\Windows\System32\config\systemprofile\AppData\Local\Microsoft \Windows\INetCac?he
    • C:\Windows\System32\config\systemprofile\Desktop
  • 如@gianni-b 所说,启用 DCOM 设置。

If you're struggling to find the DCOM entry because of a different language, you can do this:

如果您因为使用不同的语言而难以找到 DCOM 条目,您可以这样做:

  1. Start Menu > regedit
  2. Navigate to HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AppID\{00020906-0000-0000-C000-000000000046}
  3. Right click > New > String value

    • For Key, put RunAs
    • For Value, put Interactive User
  1. 开始菜单 > regedit
  2. 导航 HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AppID\{00020906-0000-0000-C000-000000000046}
  3. 右键单击 > 新建 > 字符串值

    • 对于 Key,输入 RunAs
    • 对于价值,把 Interactive User

You can do the same for Excel on AppID {00020812-0000-0000-C000-000000000046}.

您可以在 AppID 上对 Excel 执行相同的操作{00020812-0000-0000-C000-000000000046}

回答by Kevin Cloet

I did the following steps and it worked all right:

我做了以下步骤,一切正常:

  • Create the desktop folder
  • Change the DCOM element to login with a specific user instead of an launching/interactive user
  • Changed the service so it started as the same specific user
  • 创建桌面文件夹
  • 更改 DCOM 元素以使用特定用户而不是启动/交互用户登录
  • 更改了服务,使其以相同的特定用户身份启动

Edit on 2019-09-29.Due to a big windows update I had to do the exact progress all over again.

编辑于 2019-09-29。由于一个大的 Windows 更新,我不得不重新做一遍确切的进度。