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

Java 通过服务调用Office 2007时,无法打开文件,java,com,ms-office,powerpoint,jacob,Java,Com,Ms Office,Powerpoint,Jacob,我使用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: M

我使用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.
“错误”一词只是:

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
时。

这对您有用吗

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();
            }
        }
更新:如果这在客户端起作用,而导致问题的只是自动化,您可以查看以查看可能的问题。这些错误代码明显不同,但它可能会帮助您解决所有问题。

我也有同样的问题(jacob在服务中不工作),而这篇有用的帖子(更改dcomcnfg)起到了关键作用:


我用它得到了同样的错误。这也是作为一项服务运行的,所以让它无头是故意的。您的Win 7 box是x64还是x86?如果是x64,您是否部署了64位JVM?是的,它是x64,带有64位JVM和x64版本的jacob.dll。好的,这是服务调用与桌面调用的问题。在服务器上,当从桌面调用时,代码会运行,当从服务调用时,代码会产生上述错误(该服务在桌面用户帐户下运行,设置为interactive,并且“interactive services detection”服务正在运行)。WINWORD进程已经生成,演示文稿/docs对象也已经创建,但随后出现了boom。作为管理员,创建以下两个文件夹:“C:\Windows\SysWOW64\config\systemprofile\Desktop”和“C:\Windows\System32\config\systemprofile\Desktop”。您可能只需要一个或另一个,但这在64位操作系统上涵盖了这两种情况。从这里开始:确认(如上所述创建桌面文件夹)也解决了这个问题