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

7

我正在使用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时。
2个回答

3

3
这里有一个更简单的解决方法。作为管理员,创建以下两个文件夹:“C:\Windows\SysWOW64\config\systemprofile\Desktop”和“C:\Windows\System32\config\systemprofile\Desktop”。您可能只需要其中一个,但这涵盖了64位操作系统上的两种情况。从这里开始:链接 - Steve S
确认这个(像上面创建桌面文件夹那样)也为我解决了问题。 - hello_earth

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

更新:如果客户端正常工作,只是自动化引起了问题,您可以查看http://support.microsoft.com/kb/257757以查看可能的问题。错误代码显然不同,但它可能仍然有助于您进行故障排除。


我使用它时也遇到了相同的错误。此外,这是作为服务运行的,因此无头运行是有意的。 - Sindri Traustason
你的Win 7系统是x64还是x86?如果是x64,你是否已经部署了64位JVM? - Todd Main
是的,它是x64,带有64位JVM和jacob.dll的x64版本。 - Sindri Traustason
好的,这是由服务调用与桌面调用引起的问题。在服务器上,当从桌面调用时,代码运行并产生上述错误,而当从服务调用时(该服务在桌面用户帐户下运行,并设置为交互式,且“交互式服务检测”服务正在运行)则会出现错误。WINWORD进程被生成并创建了演示文稿/文档对象,但随后出现了问题。 - Alexander

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接