【问题标题】:Office 2007 is unable to open files when called through JACOB from a service从服务通过 JACOB 调用时,Office 2007 无法打开文件
【发布时间】:2011-04-09 04:56:21
【问题描述】:

我正在使用 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 时。

【问题讨论】:

    标签: java com ms-office powerpoint jacob


    【解决方案1】:

    我遇到了同样的问题(jacob 在服务中不起作用),这个有用的帖子(更改 dcomcnfg)起到了作用:

    http://bytes.com/topic/c-sharp/answers/819740-c-service-excel-application-workbooks-open-fails-when-called-service#post3466746

    【讨论】:

    • 这里有一个更简单的修复方法。作为管理员,创建以下两个文件夹:“C:\Windows\SysWOW64\config\systemprofile\Desktop”和“C:\Windows\System32\config\systemprofile\Desktop”。您可能只需要其中一种,但这涵盖了 64 位操作系统上的两种情况。从这里:link
    • 确认这一点(如上创建桌面文件夹)也为我解决了这个问题
    【解决方案2】:

    这对你有用吗?

    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();
                }
            }
    

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

    【讨论】:

    • 我使用它得到了同样的错误。这也是作为服务运行的,所以让它无头是故意的。
    • 你的 Win 7 机器是 x64 还是 x86?如果是x64,有没有部署64位的JVM?
    • 是的,它是 x64,带有 64 位 JVM 和 jacob.dll 的 x64 版本。
    • 好的,这是服务调用与桌面调用的问题。在服务器上,代码在从桌面调用时运行,并在从服务调用时产生上述错误(在桌面用户的帐户下运行并设置为交互式,并且“交互式服务检测”服务正在运行)。产生 WINWORD 进程并创建演示文稿/文档对象,但随后繁荣。
    猜你喜欢
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-25
    • 1970-01-01
    • 2015-01-23
    • 2018-03-27
    • 1970-01-01
    相关资源
    最近更新 更多