Vista、Office 互操作不工作
有谁知道如何让 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
在 Vista 和 Windows Server 2008 上,服务在称为 Session0 的对象中运行。 在 Vista 之前,常规程序将与服务一起在 Session0 中运行。
这意味着Session0已经成为一个没有桌面的荒地,你的服务甚至无法访问explorer.exe。 我很确定问题在于 Office 应用程序希望能够访问通常位于桌面上的一些组件。
由于 Excel、Word 等仅在桌面会话上受支持,因此您只有几个选择:
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:
您确实应该避免将 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.
我确实找到了一篇来自 Microsoft 的有趣的文章基本上是说不要做办公自动化。
I did find an interesting article from Microsoft basically saying don't do Office automation.
从以下位置获取安装包
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.
您是否在服务器上安装了主要互操作程序集? 它们通常位于 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.
这只是一个猜测,但可能是 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.
您是否留下登录到机器上的帐户? 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.
关于 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.