我正在使用JACOB在Java中进行与PowerPoint和其他Office应用程序的COM调用。 在特定的Windows 7计算机上,我经常会收到以下消息,但并非总是:
Source: Microsoft Office PowerPoint 2007
Description: PowerPoint could not open the file.
From excel I get:
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();
我已经尝试在执行“打开”调用之前设置断点,文件是存在的,并且我实际上可以在GUI中使用PowerPoint打开它,但是当我步进时,异常被抛出。
这个问题很烦人,因为它似乎一开始就不断发生,但是在尝试了一段时间后(重新运行相同的代码),最终成功完成,之后再也没有重复出现。
进一步的研究发现,这仅适用于.ppt、.doc和.xls文件,而不适用于.pptx、.docx和.xlsx文件。据我所知,这与文件系统无关(我已经更换了复制文件的机制,并尝试将文件放在不同的文件系统上)。
我刚刚注意到,当Java应用程序作为服务运行时才会出现此问题,而不是从命令行运行catalina.bat start时。