我正在使用 JACOB 从 Java 对 PowerPoint 和其他 Office 应用程序进行 COM 调用。在特定的 Windows 7 机器上,我经常收到以下消息,但并非总是如此:
Source: Microsoft Office PowerPoint 2007
Description: PowerPoint could not open the file.
从 excel 我得到:
ERROR - Invoke of: Open
Source: Microsoft Office Excel
Description: Microsoft Office Excel cannot access the file 'c:\marchena\marchena10\work\marchena\batch_58288\input\content_1.xlsx'. There are several possible reasons:
? The file name or path does not exist.
? The file is being used by another program.
? The workbook you are trying to save has the same name as a currently open workbook.
Word 错误只是:
VariantChangeType failed
下面是我正在运行的,错误来自最后一行。
ComThread.InitSTA();
slideApp = new ActiveXComponent("PowerPoint.Application");
Dispatch presentations = slideApp.getProperty("Presentations").toDispatch();
Dispatch presentation = Dispatch.call(presentations, "Open", inputFile.getAbsolutePath(),
MsoTriState.msoTrue.getInteger(), // ReadOnly
MsoTriState.msoFalse.getInteger(), // Untitled The Untitled parameter is used to create a copy of the presentation.
MsoTriState.msoFalse.getInteger() // WithWindow
).toDispatch();
我尝试在执行 Open 调用之前放置一个断点,并且文件就在那里,我实际上可以在 GUI 中使用 PowerPoint 打开它,但是当我执行步骤时抛出异常。
这个问题令人恼火的是,它似乎一开始就不断发生,但在研究了一段时间后(重新运行相同的代码),它最终成功完成,此后再也没有发生。
进一步研究我发现这只发生在 .ppt、.doc 和 .xls 文件上,而不是 .pptx、.docx 和 .xlsx。据我所知,它与文件系统无关(我已经换掉了复制文件的机制并尝试将文件放在不同的文件系统上)。
我刚刚注意到,这仅在 Java 应用程序作为服务运行时发生,而不是在我从命令行运行 catalina.bat start
时发生。
最佳答案
我有同样的问题(服务中的 jacob 不工作),这个有用的帖子(更改 dcomcnfg)起到了作用:
关于java - 从服务通过 JACOB 调用时,Office 2007 无法打开文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3658936/