Vista、Office 互操作不工作

发布于 2024-07-07 05:54:23 字数 384 浏览 13 评论 0原文

有谁知道如何让 MS Office 2007 .NET C# 互操作库与 Vista 一起使用?

我有一个 .NET C# 应用程序,我已将其设置为作为 Windows 服务运行。 该程序将根据情况打开Word或Excel模板并修改其内容,然后将文档保存回来。 当我在使用 Office 2007 的 Windows Server 2003 或 XP 计算机上执行此操作时,所有这些都非常有效。当我将所有内容移至 Server 2008 机器时,所有内容都停止工作。 例如,在 Excel 中,我收到一个 COM 异常,告诉我当文件明显存在时无法打开 Excel 文件,而手动打开时可以正常打开它。 Windows 服务在我登录计算机的同一用户帐户下运行,并且该帐户是管理员。

有人知道该怎么做吗?

Does anyone know how to get the MS Office 2007 .NET C# Interop libraries to work with Vista?

I have a .NET C# application that I have setup to run as a Windows service. This program will open up a Word or Excel template depending on the situation and modify its contents and then save the document back out. All of this worked great when I was doing it on a Windows Server 2003 or XP machine using Office 2007. When I moved everything to a Server 2008 box, everything quit working. In Excel for example, I get a COM exception telling me that the Excel file cannot be opened when the file is clearly there and I can open it just fine when doing it manually. The windows service is running under the same user account that I log into the machine with and that account is an Administrator.

Does anyone have any idea what to do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

赠佳期 2024-07-14 05:54:23

在 Vista 和 Windows Server 2008 上,服务在称为 Session0 的对象中运行。 在 Vista 之前,常规程序将与服务一起在 Session0 中运行。

这意味着Session0已经成为一个没有桌面的荒地,你的服务甚至无法访问explorer.exe。 我很确定问题在于 Office 应用程序希望能够访问通常位于桌面上的一些组件。

由于 Excel、Word 等仅在桌面会话上受支持,因此您只有几个选择:

  1. 在服务属性的“登录”选项卡中设置“桌面”复选框,并祈祷它能安抚 Office 诸神。 (可能不会。)
    • 尝试 1 后,检查您的代码并尝试删除/解决导致其崩溃的任何问题。
  2. 使用远程处理/WCF 来创建执行互操作工作的服务器,并使您的服务与其通信。
    • 您需要有一个登录的交互式用户,并且该用户需要以某种方式启动服务器应用程序。 也许最好使用自动启动。
    • 您可以尝试开启自动登录。 http://support.microsoft.com/kb/324737
  3. 尝试使用以下命令模拟登录用户CreateProcessAsUser 和朋友。
    • 注意:除非用户实际登录,否则我不知道它的效果如何,因此它可能不会比上面的 2 更有用,而且更难实现。 (需要 P/调用)
  4. 重写您的程序以使用 OpenXML sdk 或使用 SpreadsheetGear 之类的东西。

On Vista and Windows Server 2008, services run in something called Session0. Before Vista, regular programs would run in Session0 alongside services.

This means that Session0 has become a desktop-less wasteland where your services can't even access explorer.exe. I'm pretty sure the problem is that Office applications are expecting to be able to access a few components that are normally on the desktop.

Since Excel, Word, etc. are only supported on a desktop-ed session, you only have a few choices:

  1. Set the Desktop checkbox in the LogOn tab of your Service's properties and pray that it appeases the Office gods. (It probably wont.)
    • After trying 1, go through your code and try and remove/work around anything that causes it to crash.
  2. Use remoting/WCF to make a server that performs the interop work, and make your service communicate with it.
    • You'll need to have a logged in interactive user and the user needs to start the server application somehow. Perhaps best to use autostart.
    • You can try turning on Automatic login. http://support.microsoft.com/kb/324737
  3. Try impersonating a Logged in user using CreateProcessAsUser and friends.
    • Note: I don't know how well this works unless a user is actually logged in, so it might not be any more useful than 2 above, and is much harder to pull off. (Needs P/Invoke)
  4. Rewrite your program to use the OpenXML sdk or use something like SpreadsheetGear.
隐诗 2024-07-14 05:54:23

您确实应该避免将 Office 客户端作为服务器端应用程序运行。 考虑使用 xml 作为文件格式(对于 Office 2007 为 xlsx,对于(稍微)较旧的版本使用 Excel 工作簿 xsd。)然后您就可以不用在服务器上使用 Excel API。

You should really avoiding running the Office clients as server side apps. Consider using xml as file format (xlsx for Office 2007, or using the Excel workbook xsd for (somewhat) older versions.) Then you would be freed from using the Excel API on the server.

喜你已久 2024-07-14 05:54:23

我确实找到了一篇来自 Microsoft 的有趣的文章基本上是说不要做办公自动化。

I did find an interesting article from Microsoft basically saying don't do Office automation.

栀梦 2024-07-14 05:54:23

从以下位置获取安装包
http://www.microsoft.com/downloads/details.aspx microsoft.com/downloads/details.aspx?FamilyID=59daebaa-bed4-4282-a28c-b864d8bfa513&displaylang=en

将其安装在您的系统上,并在您的解决方案中引用 excel dll,希望它能够正常工作。

Get the installables from
http://www.microsoft.com/downloads/details.aspx?FamilyID=59daebaa-bed4-4282-a28c-b864d8bfa513&displaylang=en

install it on your system and refer excel dll in your solution and hopefully it should work.

靑春怀旧 2024-07-14 05:54:23

您是否在服务器上安装了主要互操作程序集? 它们通常位于 GAC 中,并且在构建程序时不包含在 bin 目录中,因此需要将它们本地安装在服务器上。

Do you have the primary interop assemblies installed on the server? These are usually located in the GAC and not included in the bin directory when you build your program, so they would need to be installed locally on the server.

内心激荡 2024-07-14 05:54:23

这只是一个猜测,但可能是 UAC。 我知道特权(管理)应用程序和用户应用程序无法相互发送消息或以任何方式进行通信。 您的服务以管理员身份运行,但您的桌面以 UAC 下的普通用户身份运行,即使它们是同一用户。 我还希望 Office 在启动时启动(预加载)其自身的一部分,并且将以普通用户身份运行。

尝试关闭 UAC,看看是否有帮助。 如果是这样,至少你知道它是什么。

This is just a guess, but it might be UAC. I know that privileged (admin) applications and user applications cannot send messages to each other or communicate in any way. Your service is running as an administrator, but your desktop is running as a regular user under UAC even though they are the same user. I also expect that Office has started up (pre-loaded) parts of itself on startup and that would be running as a regular user.

Try turning off UAC and see if that helps. If so, at least you know what it is.

变身佩奇 2024-07-14 05:54:23

您是否留下登录到机器上的帐户? Office 不是服务器端应用程序,如果您尝试在没有桌面上下文的情况下启动任何可执行文件,您将收到随机错误。

Do you leave an account logged on to the machine? Office is not a server-side app, and you will get random errors if you try to launch any of the executables without a desktop context.

傲世九天 2024-07-14 05:54:23

关于 Office 需要记住的一些事情。 它仅限于 x86,因此如果您在 x64 中创建应用程序,您将无法访问 Office 底层 COM 对象。 您需要在 x86 中编译您的应用程序,然后它才能工作。

您可以通过进入项目的属性并在 Visual Studio 的构建选项卡下选择 x86 来完成此操作。

这一切都假设您的应用程序是在 x64 环境中开发/运行的。

Something to remember about Office. Its x86 only, so if your creating an application in x64, you will not be able to access the Office underlying COM objects. You will need to compile your application in x86, then it would work.

You can do that by going into the properties of your project, and selecting x86 under the build tab in visual studio.

This is all assuming that your app is being developed/run in a x64 enviroment that is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文