Menu

Error opening msWord documents in Windows Server 2008 R2 Sp1 witf jInterop 3.0 and 2.08.

javcrespo
2014-10-31
2014-11-05
  • javcrespo

    javcrespo - 2014-10-31

    Hello Vikram,

    I use jinterop with an applet for control the user edition of msWord documents and store them in a database.

    It works perfectly with windows XP, window 2003 server, windows 7(64), windows 8.1 (with office 2000, 2003, 2007, 2010 and 2013) with java32.

    But it doesn´t work with Windows Server 2008 R2 Datacenter SP1. (with terminal server services installed)
    I have tested with office 2003, 2007, 2010 and 2013. Neither of them works.
    I have registered and unregister office multiple times. I have rebooted win 2008 multiple times...

    I have configured w2008 the same way I do for w7 and w8.1

    I even try with and without a domain (for others versions it doesn´t matter, neither does for w2008 , it never works).

    It has always worked with jInterop 2.08, but I have tried too with 3.0, errors and traces are the same.

    I attach a sample program, and traces with jInterop 3.0.

    If in the dcom config console (mmc.exe comexp.msc /32) the "word document" object has the run as property as Initial User,
    (it shows the word app in the current user, ok. That way it works in win 2003 server (with terminal server))
    when it is going to open the document, it doesn't open and it throws an exception:
    ** ERROR opening doc java.lang.IllegalStateException: org.jinterop.dcom.core.VariantBody$EMPTY cannot be cast to org.jinterop.dcom.core.IJIComObject
    java.lang.IllegalStateException: org.jinterop.dcom.core.VariantBody$EMPTY cannot be cast to org.jinterop.dcom.core.IJIComObject
    at org.jinterop.dcom.core.VariantBody.getObjectAsComObject(JIVariant.java:1946)
    at org.jinterop.dcom.core.JIVariant.getObjectAsComObject(JIVariant.java:1371)
    at word.Word.getIJIDispatch(Word.java:107)
    at word.Word$Document.<init>(Word.java:253)
    at word.Word$Documents.open(Word.java:242)
    at word.Word.openDoc(Word.java:90)
    at word.Word.test(Word.java:37)
    at word.Word.main(Word.java:31)

    If the dcom config console (mmc.exe comexp.msc /32) the "word document" object has the run as property as Interactive User,
    then word app shows the document but it throws an exception:
    (it shows the word app in another user session)
    (the error happends when the app is shown in the user session too)
    ** ERROR opening doc org.jinterop.dcom.impls.automation.JIAutomationException: Exception occurred. [0x80020009]
    org.jinterop.dcom.impls.automation.JIAutomationException: Exception occurred. [0x80020009]
    at org.jinterop.dcom.impls.automation.JIDispatchImpl.invoke(JIDispatchImpl.java:331)
    at org.jinterop.dcom.impls.automation.JIDispatchImpl.callMethodA(JIDispatchImpl.java:518)
    at org.jinterop.dcom.impls.automation.JIDispatchImpl.callMethodA(JIDispatchImpl.java:524)
    at org.jinterop.dcom.impls.automation.JIDispatchImpl.callMethodA(JIDispatchImpl.java:475)
    at word.Word$Documents.open(Word.java:237)
    at word.Word.openDoc(Word.java:90)
    at word.Word.test(Word.java:37)
    at word.Word.main(Word.java:31)
    Caused by: org.jinterop.dcom.common.JIRuntimeException: Exception occurred. [0x80020009]
    at org.jinterop.dcom.core.JICallBuilder.readResult(JICallBuilder.java:1095)
    at org.jinterop.dcom.core.JICallBuilder.read(JICallBuilder.java:969)
    at ndr.NdrObject.decode(NdrObject.java:36)
    at rpc.ConnectionOrientedEndpoint.call(ConnectionOrientedEndpoint.java:137)
    at rpc.Stub.call(Stub.java:113)
    at org.jinterop.dcom.core.JIComServer.call(JIComServer.java:990)
    at org.jinterop.dcom.core.JIComServer.call(JIComServer.java:945)
    at org.jinterop.dcom.core.JIComObjectImpl.call(JIComObjectImpl.java:271)
    at org.jinterop.dcom.core.JIComObjectImpl.call(JIComObjectImpl.java:156)
    at org.jinterop.dcom.impls.automation.JIDispatchImpl.invoke(JIDispatchImpl.java:313)
    ... 7 more

    (anyway I need to use Initial User for a Terminal Server environment)

    When it crashes winword.exe process takes 100% cpu and needs to be killed with task manager.

    Am I doing something wrong?

    I need to configure something more in Windows Server 2008 r2 sp1 with terminal server services?

    Thank you

    Javi.

    **** SAMPLE JAVA CLASS:
    package word;

    import java.security.Security;

    import org.bouncycastle.jce.provider.BouncyCastleProvider;
    import org.jinterop.dcom.common.JIException;
    import org.jinterop.dcom.core.IJIComObject;
    import org.jinterop.dcom.core.JIClsid;
    import org.jinterop.dcom.core.JIComServer;
    import org.jinterop.dcom.core.JISession;
    import org.jinterop.dcom.core.JIString;
    import org.jinterop.dcom.core.JIVariant;
    import org.jinterop.dcom.impls.JIObjectFactory;
    import org.jinterop.dcom.impls.automation.IJIDispatch;

    public class Word {

    static final int WdSaveOptions_wdDoNotSaveChanges=0; 
    static final int WdSaveOptions_wdSaveChanges=0xffffffff; 
    static final int WdSaveOptions_wdPromptToSaveChanges=0xfffffffe;
    
    static final int WdOriginalFormat_wdWordDocument=0;
    static final int WdOriginalFormat_wdOriginalDocumentFormat=1;
    static final int WdOriginalFormat_wdPromptUser=2;
    
    private Application wordApplication=null;
    Document document=null;
    
    public static void main(String [] s) throws Exception {
        new Word().test();
    }
    
    public void test() throws Exception {
        openWordApp();
        wordShowsMsgBox("going to open c:/temp/test.doc ...");
        openDoc();
        if (document!=null) {
            wordShowsMsgBox("saving and closing doc ...");  
            saveAndCloseDoc();
        }
        wordShowsMsgBox("closing word...");
        closeWordApp();
    }
    
    public void saveAndCloseDoc() throws Exception {
        if (document!=null) {
            document.save();
            document.close(new Integer(WdSaveOptions_wdPromptToSaveChanges),new Integer(WdOriginalFormat_wdPromptUser),null);
        }
    }
    
    public void closeWordApp() throws Exception {
        wordApplication.quit(new Integer(WdSaveOptions_wdPromptToSaveChanges),new Integer(WdOriginalFormat_wdPromptUser),null);
    }
    
    public void openWordApp() throws Exception {
        Security.addProvider(new BouncyCastleProvider());
        JISession session = null;
        JIComServer comStub = null;
        IJIComObject wordDcomObject = null;
        System.loadLibrary("ntlmauth");
        jcifs.Config.setProperty("jcifs.encoding","UTF8");
        session = JISession.createSession();
        session.useNTLMv2(true);
        // 
        JIClsid jiClsid=JIClsid.valueOf("000209ff-0000-0000-c000-000000000046"); 
        comStub = new JIComServer(jiClsid, "localhost", session); 
        wordDcomObject = comStub.createInstance();
        IJIDispatch applicationWordDispatch = ((IJIDispatch)JIObjectFactory.narrowObject(wordDcomObject.queryInterface("00020400-0000-0000-c000-000000000046")));
        wordApplication=new Application(applicationWordDispatch);
        //faltaria registrar eventos de aplicacion aqui...
        wordApplication.setVisible(true);
    }
    
    public void openDoc() throws Exception {        
        Documents docs = wordApplication.getDocuments();
        try {
            document = docs.open("c:/temp/test.doc", // file to open
                    null, // confirm conversions
                    new Boolean(false), // read only
                    new Boolean(false), // Add to recent files
                    null, // password document
                    null, // password template
                    new Boolean(false), // revert
                    null, // writePasswordDocument
                    null, // writePasswordTemplate
                    new Integer(0), // Open Format  wdOpenFormatAuto=0
                    null, // encoding
                    new Boolean(true)); 
        } catch (Exception r) {
            wordShowsMsgBox("ERROR opening c:/temp/test.doc, does it exist? :: "+r);
            System.out.println("****** ERROR opening doc "+r);
            r.printStackTrace(System.out);
        }
    }
    
    public void wordShowsMsgBox(String msg) throws Exception {
        WordBasicProxy wb = wordApplication.getWordBasic(); // Get a reference to the WordProxy native object
        Object[] params = new Object[] { new String(msg), "mensaje", new Integer(0) };
        wb.invokeMethodByName("msgBox", params);
    }
    
    public static  IJIDispatch getIJIDispatch(JIVariant variant) throws JIException {
        IJIDispatch dispatch = (IJIDispatch)JIObjectFactory.narrowObject(variant.getObjectAsComObject());
        return dispatch;
    }
    
    public static JIVariant getOpcionalParamJIVariant(Boolean param) {
        if (param==null) {
            return JIVariant.OPTIONAL_PARAM();
        }
        JIVariant parametro=new JIVariant(param.booleanValue());
        return parametro;
    }
    
    public static JIVariant getOpcionalParamJIVariant(String param) {
        if (param==null) {
            return JIVariant.OPTIONAL_PARAM();
        }
        JIVariant parametro=new JIVariant(param);
        return parametro;
    }
    
    public static JIVariant getOpcionalParamJIVariant(Integer param) {
        if (param==null) {
            return JIVariant.OPTIONAL_PARAM();
        }
        JIVariant parametro=new JIVariant(param.intValue());
        return parametro;
    }
    
    public static JIVariant [] getOpcionalParamObjectsJIVariants(Object[] params) throws Exception {
        JIVariant[] salida=new JIVariant[params.length];
        for (int i=0;i<params.length;i++) {
            salida[i]=getOpcionalParamObjectJIVariant(params[i]);
        }
        return salida;
    }
    
    public static JIVariant getOpcionalParamObjectJIVariant(Object param) throws Exception {
        if (param==null) {
            return JIVariant.OPTIONAL_PARAM();
        }
        if (param instanceof Boolean) {
            return getOpcionalParamJIVariant((Boolean)param);
        }
        if (param instanceof Integer) {
            return getOpcionalParamJIVariant((Integer)param);
        }
        if (param instanceof String) {
            return getOpcionalParamJIVariant((String)param);
        }
        //if (param instanceof Character) 
        //if (param instanceof Date) 
        //if (param instanceof Double) 
        //if (param instanceof Float) 
        //if (param instanceof Long) 
        //if (param instanceof Short) 
        throw new Exception("ERROR en getOpcionalParamObjectJIVariant, tipo no reconocido "+param.getClass());
    }
    
    class Application {
    
        IJIDispatch applicationWordDispatch = null;
    
        public Application(IJIDispatch applicationWordDispatch_) throws Exception {
            applicationWordDispatch=applicationWordDispatch_;
        }
        public Application(JIVariant variant) throws Exception {
            try {
                applicationWordDispatch=getIJIDispatch(variant);
            } catch (Exception r) {
                throw new Exception("error creando Application desde JIVariant",r);
            }
        }
        public void setVisible(boolean visible)  throws Exception {
            //int dispId = this.dispatch.getIDsOfNames("Visible");
            JIVariant variant = new JIVariant(visible);
            this.applicationWordDispatch.put("Visible", variant);
        }
        public Documents getDocuments()  throws Exception {
            JIVariant variant = this.applicationWordDispatch.get("Documents");
            Documents documents=new Documents(variant);
            return documents;
        }
        public WordBasicProxy getWordBasic()  throws Exception {
            JIVariant variant = this.applicationWordDispatch.get("WordBasic");
            WordBasicProxy wordBasic=new WordBasicProxy(variant);
            return wordBasic;
        }       
        public void quit(Integer SaveChanges, Integer OriginalFormat, Boolean RouteDocument)  throws Exception {
            JIVariant jiSaveChanges=getOpcionalParamJIVariant(SaveChanges);
            JIVariant jiOriginalFormat=getOpcionalParamJIVariant(OriginalFormat);
            JIVariant jiRouteDocument=getOpcionalParamJIVariant(RouteDocument);
            applicationWordDispatch.callMethodA("Quit", new Object[] { jiSaveChanges, jiOriginalFormat,jiRouteDocument} );
            JISession.destroySession(this.applicationWordDispatch.getAssociatedSession());      
        }       
    }
    
    class Documents {
        IJIDispatch documentsDispatch=null;
        public Documents(JIVariant variant) throws Exception {
            //try {
            documentsDispatch=getIJIDispatch(variant);
            //} catch (Exception r) {
            //throw new Exception("error creando Documents desde JIVariant",r);
            //}
        }
        public Document open(String FileName, Boolean ConfirmConversions, Boolean ReadOnly, 
                Boolean AddToRecentFiles, String PasswordDocument, String PasswordTemplate, 
                Boolean Revert, String WritePasswordDocument, String WritePasswordTemplate, 
                Integer Format, Integer Encoding, Boolean Visible)  throws Exception {
            JIString FileNameJI = new JIString(FileName);
            JIVariant ConfirmConversionsJI=getOpcionalParamJIVariant(ConfirmConversions);
            JIVariant ReadOnlyJI=getOpcionalParamJIVariant(ReadOnly);
            JIVariant AddToRecentFilesJI=getOpcionalParamJIVariant(AddToRecentFiles);
            JIVariant PasswordDocumentJI=getOpcionalParamJIVariant(PasswordDocument);
            JIVariant PasswordTemplateJI=getOpcionalParamJIVariant(PasswordTemplate);
            JIVariant RevertJI=getOpcionalParamJIVariant(Revert);
            JIVariant WritePasswordDocumentJI=getOpcionalParamJIVariant(WritePasswordDocument);
            JIVariant WritePasswordTemplateJI=getOpcionalParamJIVariant(WritePasswordTemplate);
            JIVariant FormatJI=getOpcionalParamJIVariant(Format);
            JIVariant EncodingJI=getOpcionalParamJIVariant(Encoding);
            JIVariant VisibleJI=getOpcionalParamJIVariant(Visible);
            JIVariant[] variant2 = documentsDispatch.callMethodA("Open", new Object[] { FileNameJI, ConfirmConversionsJI, 
                    ReadOnlyJI, AddToRecentFilesJI, PasswordDocumentJI, 
                    PasswordTemplateJI, RevertJI, WritePasswordDocumentJI, 
                    WritePasswordTemplateJI, FormatJI, EncodingJI, 
                    VisibleJI});
            Document document=new Document(variant2[0]);
            return document;
        }
    }
    
    class Document {
        IJIDispatch documentDispatch=null;
        public Document(JIVariant variant) throws Exception {
            //try {
            documentDispatch=getIJIDispatch(variant);
            //} catch (Exception r) {
            //  throw new Exception("error creando Document desde JIVariant",r);
            //}
        }
        public void save()  throws Exception {
            documentDispatch.callMethod("save");
        }
        public void close(Integer SaveChanges, Integer OriginalFormat, Boolean RouteDocument)  throws Exception {
            JIVariant jiSaveChanges=getOpcionalParamJIVariant(SaveChanges);
            JIVariant jiOriginalFormat=getOpcionalParamJIVariant(OriginalFormat);
            JIVariant jiRouteDocument=getOpcionalParamJIVariant(RouteDocument);
            documentDispatch.callMethod("Close", new Object[] { jiSaveChanges, jiOriginalFormat,jiRouteDocument} );
        }       
    }
    
    class WordBasicProxy {
        IJIDispatch wordBasicDispatch=null;
        public WordBasicProxy(JIVariant variant) throws Exception {
            //try {
            wordBasicDispatch=getIJIDispatch(variant);
            //} catch (Exception r) {
            //throw new Exception("error creando WordBasicProxy desde JIVariant",r);
            //}
        }
        public JIVariant[] invokeMethodByName(String nombreMetodoWordBasic, Object[] params) throws NoSuchMethodException, Exception {
            JIVariant[] respuesta=null;
            if (params==null || params.length==0) {
                JIVariant respuesta0 = wordBasicDispatch.callMethodA(nombreMetodoWordBasic);
                respuesta=new JIVariant[0];
                respuesta[0]=respuesta0;
            } else {
                JIVariant[] variants=getOpcionalParamObjectsJIVariants(params);
                respuesta = wordBasicDispatch.callMethodA(nombreMetodoWordBasic, variants);
            }
            return respuesta;
        }
    
    }
    

    }
    **** END OF SAMPLE JAVA CLASS.

     
  • javcrespo

    javcrespo - 2014-10-31

    Hi,
    I have just done the thing they say in http://stackoverflow.com/questions/1006923/automating-office-via-windows-service-on-server-2008:
    I have created:
    C:\Windows\System32\config\systemprofile\Desktop
    C:\Windows\SysWOW64\config\systemprofile\Desktop

    now If in the dcom config console (mmc.exe comexp.msc /32) the "word document" object has the run as property as Initial User (OK),
    (it shows the word app in the current user (OK))
    then word app shows the document but it throws an exception:

    ** ERROR opening doc org.jinterop.dcom.impls.automation.JIAutomationException: Exception occurred. [0x80020009]
    org.jinterop.dcom.impls.automation.JIAutomationException: Exception occurred. [0x80020009]
    at org.jinterop.dcom.impls.automation.JIDispatchImpl.invoke(JIDispatchImpl.java:331)
    at org.jinterop.dcom.impls.automation.JIDispatchImpl.callMethodA(JIDispatchImpl.java:518)
    at org.jinterop.dcom.impls.automation.JIDispatchImpl.callMethodA(JIDispatchImpl.java:524)
    at org.jinterop.dcom.impls.automation.JIDispatchImpl.callMethodA(JIDispatchImpl.java:475)
    at word.Word$Documents.open(Word.java:237)
    at word.Word.openDoc(Word.java:90)
    at word.Word.test(Word.java:37)
    at word.Word.main(Word.java:31)
    Caused by: org.jinterop.dcom.common.JIRuntimeException: Exception occurred. [0x80020009]
    at org.jinterop.dcom.core.JICallBuilder.readResult(JICallBuilder.java:1095)
    at org.jinterop.dcom.core.JICallBuilder.read(JICallBuilder.java:969)
    at ndr.NdrObject.decode(NdrObject.java:36)
    at rpc.ConnectionOrientedEndpoint.call(ConnectionOrientedEndpoint.java:137)
    at rpc.Stub.call(Stub.java:113)
    at org.jinterop.dcom.core.JIComServer.call(JIComServer.java:990)
    at org.jinterop.dcom.core.JIComServer.call(JIComServer.java:945)
    at org.jinterop.dcom.core.JIComObjectImpl.call(JIComObjectImpl.java:271)
    at org.jinterop.dcom.core.JIComObjectImpl.call(JIComObjectImpl.java:156)
    at org.jinterop.dcom.impls.automation.JIDispatchImpl.invoke(JIDispatchImpl.java:313)
    ... 7 more

     
    • Vikram Roopchand

      Hi,

      There is a getExcepInfo() within the AutomationException , can you please
      check that structure for all cases and see what each one reports ?

      best regards,
      Vikram

      On Fri, Oct 31, 2014 at 6:00 PM, javcrespo javcrespo@users.sf.net wrote:

      Hi,
      I have just done the thing they say in
      http://stackoverflow.com/questions/1006923/automating-office-via-windows-service-on-server-2008:
      I have created:
      C:\Windows\System32\config\systemprofile\Desktop
      C:\Windows\SysWOW64\config\systemprofile\Desktop

      now If in the dcom config console (mmc.exe comexp.msc /32) the "word
      document" object has the run as property as Initial User (OK),
      (it shows the word app in the current user (OK))
      then word app shows the document but it throws an exception:

      **** ERROR opening doc
      org.jinterop.dcom.impls.automation.JIAutomationException: Exception
      occurred. [0x80020009]
      org.jinterop.dcom.impls.automation.JIAutomationException: Exception
      occurred. [0x80020009]
      at
      org.jinterop.dcom.impls.automation.JIDispatchImpl.invoke(JIDispatchImpl.java:331)
      at
      org.jinterop.dcom.impls.automation.JIDispatchImpl.callMethodA(JIDispatchImpl.java:518)
      at
      org.jinterop.dcom.impls.automation.JIDispatchImpl.callMethodA(JIDispatchImpl.java:524)
      at
      org.jinterop.dcom.impls.automation.JIDispatchImpl.callMethodA(JIDispatchImpl.java:475)
      at word.Word$Documents.open(Word.java:237)
      at word.Word.openDoc(Word.java:90)
      at word.Word.test(Word.java:37)
      at word.Word.main(Word.java:31)
      Caused by: org.jinterop.dcom.common.JIRuntimeException: Exception
      occurred. [0x80020009]
      at org.jinterop.dcom.core.JICallBuilder.readResult(JICallBuilder.java:1095)
      at org.jinterop.dcom.core.JICallBuilder.read(JICallBuilder.java:969)
      at ndr.NdrObject.decode(NdrObject.java:36)
      at rpc.ConnectionOrientedEndpoint.call(ConnectionOrientedEndpoint.java:137)
      at rpc.Stub.call(Stub.java:113)
      at org.jinterop.dcom.core.JIComServer.call(JIComServer.java:990)
      at org.jinterop.dcom.core.JIComServer.call(JIComServer.java:945)
      at org.jinterop.dcom.core.JIComObjectImpl.call(JIComObjectImpl.java:271)
      at org.jinterop.dcom.core.JIComObjectImpl.call(JIComObjectImpl.java:156)
      at
      org.jinterop.dcom.impls.automation.JIDispatchImpl.invoke(JIDispatchImpl.java:313)
      ... 7 more
      ------------------------------

      Error opening msWord documents in Windows Server 2008 R2 Sp1 witf jInterop
      3.0 and 2.08.
      https://sourceforge.net/p/j-interop/discussion/840678/thread/95315fa9/?limit=25#ff57


      Sent from sourceforge.net because you indicated interest in
      https://sourceforge.net/p/j-interop/discussion/840678/

      To unsubscribe from further messages, please visit
      https://sourceforge.net/auth/subscriptions/

      --
      The Mind is a place of its own. It can make a heaven out of hell or a hell
      out of heaven. Attitude is everything. No matter how adverse conditions
      maybe, one has the capacity to turn things around by one's Determination,
      Perseverance and Hardwork.

      John Milton
      (Paradise Lost)

       
      • javcrespo

        javcrespo - 2014-11-03

        Thanks for the quick answer.

        I did what you ask.

        JIExcepInfo {
        code:-2146822307,
        desc:No se puede abrir el almacenamiento de macros. [ ,
        source:Microsoft Word ,
        helpFilePath:C:\Program Files (x86)\Microsoft Office\Office12\3082\WDMAIN11.CHM ]
        }

        I will investigate from this point, but if you know something about it, you are very wellcome if you want to share it with me...

        Thank you very much.

        Javi.

         
  • javcrespo

    javcrespo - 2014-11-05

    Hi Vikram,

    Finally I solved this (on windows 2008 server r2 sp1, with remote desktop, running word 2007 as initial user)

    java.lang.IllegalStateException: org.jinterop.dcom.core.VariantBody$EMPTY cannot be cast to org.jinterop.dcom.core.IJIComObject

    [\3082\WDMAIN11.CHM]

    0x80020009 -2146822307 Could Not Open Macro Storage
    No se puede abrir el almacenamiento de macros

    0x80020009 -2146823191 Word has encountered a problem
    Word ha detectado un problema

    Solutions:

    This folders must exist, and users need to have rigths over them:
    C:\Windows\System32\config\systemprofile
    C:\Windows\System32\config\systemprofile\Desktop
    C:\Windows\SysWOW64\config\systemprofile
    C:\Windows\SysWOW64\config\systemprofile\Desktop

    Delete all Normal.dot and Normal.dotm

    Users need to have rights over:
    C:\Windows\Temp

    Thanks

    Javi.

     

Log in to post a comment.