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

2

7

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.

Apogamy answered 7/9, 2010 at 13:8 Comment(0)
P
3

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

Pahl answered 1/4, 2011 at 13:54 Comment(2)
Here's an even easier fix. As admin, make the following two folders: "C:\Windows\SysWOW64\config\systemprofile\Desktop" and "C:\Windows\System32\config\systemprofile\Desktop". You probably only need one or the other, but this covers both cases on a 64-bit OS. From here: linkPahl
confirming this (creating Desktop folder as above) fixed the issue for me tooPenalty
L
1

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.

Lockman answered 14/9, 2010 at 5:2 Comment(4)
I get the same error using that. Also this runs as a service so having it headless was deliberate.Apogamy
Is your Win 7 box x64 or x86? If it is x64, have you deployed the 64 bit JVM?Lockman
Yes, it's x64, with a 64 bit JVM and the x64 version of jacob.dll.Apogamy
ok it is the problem of called by service vs. called by desktop. on the server the code runs when called from desktop and produces the above error when called from service (which is running under the account of the desktop user and is set to interactive and the "interactive services detection" service is running). the WINWORD process is spawned and the presentations/docs object is created but then boom.Heelpost

© 2022 - 2024 — McMap. All rights reserved.