Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Office 2007 is unable to open files when called through JACOB from a service

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.

like image 884
Sindri Traustason Avatar asked Sep 07 '10 13:09

Sindri Traustason


People also ask

How do I unblock files in trust center?

In the Options window, select Trust Center > Trust Center Settings. In the Trust Center window, select File Block Settings, and then clear the "Open" or "Save" check box for the file type that you want to open or save. Clear the option means allow user to open or save the file.

How do you resolve Cannot open file because the file path is more than 259 characters?

The answer is to rename the file, the folders or move the file to a shorter path. There is a registry key that allows you to have long file paths now.

Why my MS Word is not opening?

If a Word file won't open, check the file association. Right-click the file, select Open With, and choose Microsoft Word. To repair a damaged file in Word, go to File > Open > Browse, and highlight the file you want. Select the Open drop-down arrow, then choose Open and Repair.


2 Answers

I had the same problem (jacob in service not working), and this helpful posting (changing the dcomcnfg) did the trick:

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

like image 136
Steve S Avatar answered Sep 29 '22 12:09

Steve S


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.

like image 43
Todd Main Avatar answered Sep 29 '22 12:09

Todd Main