从服务通过JACOB调用时,Office 2007无法打开文件

Sin*_*son 7 java com powerpoint ms-office jacob

我正在使用JACOB从Java对PowerPoint和其他Office应用程序进行COM调用.在特定的Windows 7机器上,我经常收到以下消息,但并非总是如此:

Source: Microsoft Office PowerPoint 2007
Description: PowerPoint could not open the file.
Run Code Online (Sandbox Code Playgroud)

从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.
Run Code Online (Sandbox Code Playgroud)

Word错误只是:

VariantChangeType failed
Run Code Online (Sandbox Code Playgroud)

以下是我正在运行的,错误来自最后一行.

        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();
Run Code Online (Sandbox Code Playgroud)

我已经尝试在Open调用之前设置断点并且文件在那里,我实际上可以在GUI中使用PowerPoint打开它,但是当我执行步骤时抛出异常.

关于这个问题的令人讨厌的事情是,它似乎不断开始,但在对它进行了一段时间的重复(重新运行相同的代码)之后,它最终成功完成,之后再也不会再出现.

进一步的研究我发现这只发生在.ppt,.doc和.xls文件中,而不是.pptx,.docx和.xlsx.据我所知,它不是文件系统相关的(我已经换掉了复制文件并尝试将文件放在不同文件系统上的机制).

我刚刚注意到这只发生在Java应用程序作为服务运行时,而不是在我从命令行运行时catalina.bat start.

Tod*_*ain 1

这对你有用吗?

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class PPT {
    private static final String inputFile = "c:\\learning.ppt";
    public static void main(String[] args) {
        ActiveXComponent slideApp = new ActiveXComponent("PowerPoint.Application");
        slideApp.setProperty("Visible", new Variant(true));
        ActiveXComponent presentations = slideApp.getPropertyAsComponent("Presentations");
        ActiveXComponent presentation = presentations.invokeGetComponent("Open",new Variant(inputFile), new Variant(true));
        ComThread.Release();
            }
        }
Run Code Online (Sandbox Code Playgroud)

更新:如果客户端正在运行,并且只是自动化导致了问题,您可以查看http://support.microsoft.com/kb/257757以查看可能的问题。这些错误代码显然不同,但它仍然可以帮助您排除故障。