Interop.Word Documents.Open为null

时间:2021-08-25 16:02:48

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.

我正在尝试使用Interop.Word.Application打开.docx文件并转换为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();

6 个解决方案

#1


53  

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. 开始 - > dcomcnfg.exe
  3. Computer
  4. 电脑
  5. Local Computer
  6. 本地电脑
  7. Config DCOM
  8. 配置DCOM
  9. Search for Microsoft Word 97-2003 Documents -> Properties
  10. 搜索Microsoft Word 97-2003文档 - >属性

Tab Identity, change from Launching User to Interactive User

选项卡标识,从启动用户更改为交互式用户

#2


16  

Try this it may help you.

试试这可能会对你有所帮助。

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

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

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

It seams to be a profile problem.

它接缝成为个人资料问题。

#3


1  

This worked for me. https://social.msdn.microsoft.com/Forums/en-US/0f5448a7-72ed-4f16-8b87-922b71892e07/word-2007-documentsopen-returns-null-in-aspnet.

这对我有用。 https://social.msdn.microsoft.com/Forums/en-US/0f5448a7-72ed-4f16-8b87-922b71892e07/word-2007-documentsopen-returns-null-in-aspnet。

If this helpe you, please make it answer

如果这对你有帮助,请回答

#4


0  

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.

正如Paulie在他对OP的评论中所说,我认为这个问题主要是因为网络服务器上的库。你可能会破解让这个工作在一个网络应用程序,但我建议反对它。

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). Unfortunately he 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库(http://cathalscorner.blogspot.com/2009/10/converting-docx-into-doc-pdf-html.html),转换为PDF等格式几乎是不可能的。 )

Here is a 'promising' alternative, but YMMV.

这是一个“有前途”的选择,但是YMMV。

#5


0  

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,并且您知道它有效。使用互操作时可能会出现一些问题,但是,单词互操作是唯一可以确保最正确转换单词文件的单词。

#6


0  

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 \ AppData \ Local \ Microsoft \ Windows \ INetCache
    • C:\Windows\System32\config\systemprofile\Desktop
    • C:\ WINDOWS \ system32 \设置\ systemprofile \桌面
  • Enable DCOM setting as @gianni-b says.

    启用DCOM设置为@ gianni-b说。

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

如果由于语言不同而难以找到DCOM条目,则可以执行以下操作:

  1. Start Menu > regedit
  2. 开始菜单> regedit
  3. Navigate to HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AppID\{00020906-0000-0000-C000-000000000046}
  4. 导航到HKEY_LOCAL_MACHINE \ SOFTWARE \ Classes \ AppID \ {00020906-0000-0000-C000-000000000046}
  5. Right click > New > String value

    右键单击>新建>字符串值

    • For Key, put RunAs
    • 对于Key,放入RunAs
    • For Value, put Interactive User
    • 对于Value,请填写Interactive User

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

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

#1


53  

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. 开始 - > dcomcnfg.exe
  3. Computer
  4. 电脑
  5. Local Computer
  6. 本地电脑
  7. Config DCOM
  8. 配置DCOM
  9. Search for Microsoft Word 97-2003 Documents -> Properties
  10. 搜索Microsoft Word 97-2003文档 - >属性

Tab Identity, change from Launching User to Interactive User

选项卡标识,从启动用户更改为交互式用户

#2


16  

Try this it may help you.

试试这可能会对你有所帮助。

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

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

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

It seams to be a profile problem.

它接缝成为个人资料问题。

#3


1  

This worked for me. https://social.msdn.microsoft.com/Forums/en-US/0f5448a7-72ed-4f16-8b87-922b71892e07/word-2007-documentsopen-returns-null-in-aspnet.

这对我有用。 https://social.msdn.microsoft.com/Forums/en-US/0f5448a7-72ed-4f16-8b87-922b71892e07/word-2007-documentsopen-returns-null-in-aspnet。

If this helpe you, please make it answer

如果这对你有帮助,请回答

#4


0  

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.

正如Paulie在他对OP的评论中所说,我认为这个问题主要是因为网络服务器上的库。你可能会破解让这个工作在一个网络应用程序,但我建议反对它。

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). Unfortunately he 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库(http://cathalscorner.blogspot.com/2009/10/converting-docx-into-doc-pdf-html.html),转换为PDF等格式几乎是不可能的。 )

Here is a 'promising' alternative, but YMMV.

这是一个“有前途”的选择,但是YMMV。

#5


0  

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,并且您知道它有效。使用互操作时可能会出现一些问题,但是,单词互操作是唯一可以确保最正确转换单词文件的单词。

#6


0  

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 \ AppData \ Local \ Microsoft \ Windows \ INetCache
    • C:\Windows\System32\config\systemprofile\Desktop
    • C:\ WINDOWS \ system32 \设置\ systemprofile \桌面
  • Enable DCOM setting as @gianni-b says.

    启用DCOM设置为@ gianni-b说。

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

如果由于语言不同而难以找到DCOM条目,则可以执行以下操作:

  1. Start Menu > regedit
  2. 开始菜单> regedit
  3. Navigate to HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AppID\{00020906-0000-0000-C000-000000000046}
  4. 导航到HKEY_LOCAL_MACHINE \ SOFTWARE \ Classes \ AppID \ {00020906-0000-0000-C000-000000000046}
  5. Right click > New > String value

    右键单击>新建>字符串值

    • For Key, put RunAs
    • 对于Key,放入RunAs
    • For Value, put Interactive User
    • 对于Value,请填写Interactive User

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

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