views:

218

answers:

2

I'm using JACOB to do COM calls to PowerPoint and other Office applications from Java. On a particular Windows 7 box I'm getting the following message quite often, but not always:

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.

The Word error is just:

VariantChangeType failed

The following is what I'm running, the error comes from the last line.

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

I've tried putting a breakpoint just before doing the Open call and the file is there, and I can actually open it with PowerPoint in the GUI but when I step the exception is thrown.

The annoying thing about this issue is that it seems to happen continuously to begin with, but after poking at it for a while (rerunning the same code), it eventually completes successfully, and after that never reoccurs.

Further research I've found this only happens with to .ppt, .doc and .xls files, not .pptx, .docx and .xlsx. And as far as I can tell it's not file system related (I've swapped out the mechanism that copies the files and tried putting the files on a different file system).

I've just noticed that this only happens when the Java application is running as a service, not when I run catalina.bat start from command line.

+2  A: 

Does this work for you?

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

Update: If this is working the client and it's just automation that is causing the issues, you can view http://support.microsoft.com/kb/257757 to look at possible issues. There error codes are obviously different, but it may help you troubleshoot all the same.

Otaku
I get the same error using that. Also this runs as a service so having it headless was deliberate.
Sindri Traustason
Is your Win 7 box x64 or x86? If it is x64, have you deployed the 64 bit JVM?
Otaku
Yes, it's x64, with a 64 bit JVM and the x64 version of jacob.dll.
Sindri Traustason
+2  A: 

I have a similar error:

In my development environment (win 7 64 bit, 64 bit jvm, tomcat, jacob 64bit dll) everything works fine (similar code to Sindri Traustason's but using Word.Application and Documents) but on the server (win 2008 64 bit, 64 bit jvm, tomcat, jacob 64bit dll) i get the VariantChangeType failed in the call ... "Open" line.

The same happens when I use Otaku's code or use "Add" instead of "Open".

When I use .getDispatch() instead of .toDispatch() it works in development environment, but the error on the server is

java.lang.IllegalStateException: getDispatch() only legal on Variants of type VariantDispatch, not 0
  at com.jacob.com.Variant.getDispatch(Variant.java:499)

Any help would be greatly appreciated :)

Alexander