freemansoft / jacob-project

JACOB is a JAVA-COM Bridge that allows you to call COM Automation components from Java. It uses JNI to make native calls to the COM libraries. JACOB runs on x86 and x64 environments supporting 32 bit and 64 bit JVMs. This repository was migrated from Sourceforge 2020 Sep
GNU Lesser General Public License v2.1
507 stars 112 forks source link

com.jacob.com.ComFailException: Invoke of: Open Source: Description #46

Open yujunchengg opened 8 months ago

yujunchengg commented 8 months ago

My program runs in the following environmen windows server 2016 Standard 64 bit Microsoft Office Professional 2016 jdk version: 1.8.0 (build 1.8.0_221-b11) jacob version: 1.20 The program code is as follows public static FileConvertResult ppt2PDF(String pptfile, String pdffile){ if(Files.exists(Paths.get(pdffile))){ return new FileConvertResult(true,false); } synchronized (pptfile.intern()){ FileConvertResult result = new FileConvertResult(true,false); if(Files.exists(Paths.get(pdffile))){ log.info("current thread: {} do not need to execute ppt2PDF method, because other thread has execute ppt2PDF method",Thread.currentThread().getName()); return result; } ActiveXComponent ppt = null; Dispatch presentations = null; try { ComThread.InitMTA(true); ppt = new ActiveXComponent("PowerPoint.application"); Dispatch.put(ppt.getObject(), "DisplayAlerts", new Variant(false)); presentations = ppt.invokeGetComponent("Presentations").invokeGetComponent("Open", new Variant(pptfile)); Dispatch.invoke(presentations, "SaveAs", Dispatch.Method, new Object[] {pdffile , new Variant(32) }, new int[1]); } catch (Exception e) { log.error("-------------ppt2PDF occurred exception----------"); String message = e.getMessage(); result.setSuccess(false); result.setNeedReconvert(true); result.setDescription(message); /if(null!=message){ if(message.contains("Description: ")){ result.setNeedReconvert(false); } }/ log.error(result.getDescription(), e); } finally { try { if (ppt != null) { ppt.invoke("Quit"); } } catch (Exception e1) { log.error(e1.getMessage(),e1); } finally { if (presentations != null){ presentations.safeRelease(); } if (ppt != null){ ppt.safeRelease(); } ComThread.Release(); } } return result; } }

Strangely enough it can work well on Windows server 2012,but on Windows server 2016,it occurred exception,exception detail message is that: com.jacob.com.ComFailException: Invoke of: Open Source: Description:

at com.jacob.com.Dispatch.invokev(Native Method)
at com.jacob.com.Dispatch.invokev(Dispatch.java:625)
at com.jacob.com.Dispatch.callN(Dispatch.java:453)
at com.jacob.activeX.ActiveXComponent.invoke(ActiveXComponent.java:476)
at com.jacob.activeX.ActiveXComponent.invokeGetComponent(ActiveXComponent.java:391)
EJP286CRSKW commented 8 months ago

Where did you get new Variant(32) from? It's supposed to be 0 or -1. https://learn.microsoft.com/en-us/office/vba/api/powerpoint.presentations.open and https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.core.msotristate?view=office-pia