MS-Word COM Automation not working

471 views
Skip to first unread message

Andreas Nagel

unread,
Oct 23, 2013, 7:00:43 AM10/23/13
to jna-...@googlegroups.com
Hi,

I tried to use the example on GitHub for MS-Word COM-automation:
https://github.com/twall/jna/blob/master/contrib/msoffice/src/com/sun/jna/platform/win32/COM/office/MSOfficeDemo.java

The first thing I noticed that these examples use classes which are not in the current version 4.0.0 (jar).
The example uses the 'com.sun.jna.platform.win32.COM.COMLateBindingObject'. Therefore I had to download the
sources and create my own jna-platform.jar.

After that, the example seems to work. Word started and could be set to visible. Then it was
possible to open a document, add text, close the document, hide the window and quit the application.
But one thing couldn't be done: Save the document to another file name or to another file format.

In both cases, there occur an exception: com.sun.jna.platform.win32.COM.COMException: Falscher Variablentyp.
(engl.: Wrong Variable Type).

I didn't alter the example-file. Seems that either the example is wrong implemented or JNA-implementation of COM has
a bug. I marked the line form the Example, where the exception occurs.

. . . . .
try {
            msWord = new MSWord();
            System.out.println("MSWord version: " + msWord.getVersion());

            msWord.setVisible(true);
            // msWord.newDocument();
            msWord.openDocument(currentWorkingDir + "jnatest.doc", true);
            msWord.insertText("Hello from JNA!");
            // wait 10sec. before closing
            Thread.currentThread().sleep(10000);
            // save in different formats
            // pdf format is only supported in MSWord 2007 and above
            // msWord.SaveAs("C:\\TEMP\\jnatestSaveAs.pdf", wdFormatPDF);
            msWord.SaveAs("C:\\TEMP\\jnatestSaveAs.rtf", wdFormatRTF);       // EXCEPTION!
            msWord.SaveAs("C:\\TEMP\\jnatestSaveAs.html", wdFormatHTML); // EXCEPTION!
            // close and save the document
}
. . . . .

Does anybody had the same problem and found a solution for that. I read in the frequently asked questions
that the COM-implementation in JNA is not well tests and they recomment to use com4j or JACOB. Don't
know if you can trust the implementation in JNA.

Thanks for help.

Best regards
Andi



Timothy Wall

unread,
Oct 23, 2013, 7:41:23 AM10/23/13
to jna-...@googlegroups.com
The FAQ documentation is a little out of date; the contributed COM support in JNA is new and perhaps a bit rough in places.
> --
> You received this message because you are subscribed to the Google Groups "Java Native Access" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jna-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Andreas Nagel

unread,
Oct 23, 2013, 8:04:37 AM10/23/13
to jna-...@googlegroups.com
I saw a topic in the group which has to do with the same problem I have.
--> https://groups.google.com/forum/#!topic/jna-users/2DUrOV8Z8ZI

But this topic ends where you listed a lot of problems with the COM stuff.
Can you tell me if and when the COM-support is merged to a release
version of JNA?

Timothy Wall

unread,
Oct 23, 2013, 1:30:59 PM10/23/13
to jna-...@googlegroups.com
I'd definitely recommend using the latest from github, which I believe is a little newer than that contained in the 4.0 release. @wolftobias is the primary author; I'll leave it to him to speak to the state of the COM support as a whole.

wolf....@gmx.net

unread,
Oct 24, 2013, 12:32:23 AM10/24/13
to jna-...@googlegroups.com
Hi Andreas, 

if you get the latest version of the source from github the MSOfficeDemo sample should work. If not please send more informations about your environment, OS, 32 or 64bit, etc.

wolf....@gmx.net

unread,
Oct 24, 2013, 12:40:15 AM10/24/13
to jna-...@googlegroups.com
Hi Andreas,

I just tried the example as you wrote it below and on my pc it works. I`m running here Windows 8 with Office 2003. 


On Wednesday, 23 October 2013 13:00:43 UTC+2, Andreas Nagel wrote:

wolf....@gmx.net

unread,
Oct 24, 2013, 12:43:14 AM10/24/13
to jna-...@googlegroups.com
I just changed the path to a path which in fact does not exists and got an Exception, maybe you don`t have the given C:\TEMP path on your computer. Please check again and give more details about your system.

msWord.SaveAs("C:\\TEMP2\\jnatestSaveAs.rtf", wdFormatRTF);


On Wednesday, 23 October 2013 13:00:43 UTC+2, Andreas Nagel wrote:

Andreas Nagel

unread,
Oct 24, 2013, 3:20:07 AM10/24/13
to jna-...@googlegroups.com
Hi,

my system settings are:
- Windows 8.1, 64 bit
- JVM 64 bit
- Word 2013

I will test it today and give you a feedback.
Thanks for your reply.


Andreas Nagel

unread,
Oct 24, 2013, 4:56:58 AM10/24/13
to jna-...@googlegroups.com
Hi,
so I downloaded the current state of the source code by cloning the git-repository:
git clone https://github.com/twall/jna


The I created the jna.jar and jna-platform.jar and copied the samples from
jna\contrib\msoffice\src\com\sun\jna\platform\win32\COM\office to my
IntelliJ-Project. When I run the test, unfortunately there are still problems
when executeing the 'SaveAs' command.

The problem is in MsWord.java when you call invokeNoReply(...):

 
   public void SaveAs(String FileName, LONG FileFormat) throws COMException {
        VARIANT vtFileName = new VARIANT(FileName);
        VARIANT vtFileFormat = new VARIANT(FileFormat);

        this.invokeNoReply("SaveAs", this.getActiveDocument().getIDispatch(),
                vtFileName, vtFileFormat);
    }


The exception is:

com.sun.jna.platform.win32.COM.COMException: Typenkonflikt.             (engl.: Type Conflict)
    at com.sun.jna.platform.win32.COM.COMUtils.checkRC(COMUtils.java:115)
    at com.sun.jna.platform.win32.COM.COMBindingBaseObject.oleMethod(COMBindingBaseObject.java:267)
    at com.sun.jna.platform.win32.COM.COMBindingBaseObject.oleMethod(COMBindingBaseObject.java:217)
    at com.sun.jna.platform.win32.COM.COMLateBindingObject.invokeNoReply(COMLateBindingObject.java:404)
    at MSWord.SaveAs(MSWord.java:56)
    at MSOfficeDemo.testMSWord(MSOfficeDemo.java:44)
    at MSOfficeDemo.<init>(MSOfficeDemo.java:21)
    at MSOfficeDemo.main(MSOfficeDemo.java:14)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)


wolf....@gmx.net

unread,
Oct 25, 2013, 4:20:03 AM10/25/13
to jna-...@googlegroups.com
For your information Daniel has already posted a issue with the Unit test with a 64bit JVM. I`ve actually no test environment for 64bit JVM.
But it could also be a issue with the Office 2013. Could you please check the microsoft Word documentation if there is a change in the function interface.

Andreas Nagel

unread,
Oct 25, 2013, 8:28:51 AM10/25/13
to jna-...@googlegroups.com

Yes, I run the Ant-task and the unit tests for COM also failed on 64-Bit JVM.

I installed a 32-Bit JVM and now it works. So it looks like it's not a MS-Word issue but more the
implementation of the jna-platform library (dll/jar). Seems that there are problems in converting the data types.

By the way. Just tested your generator for MS-Word. I found a lot of problems:

I use the 32-Bit JVM and the following command option: 
-tlb.file C:\Program Files (x86)\Microsoft Office\Office15\MSWORD.OLB

- it is not possible to define the package name. Always is 'mypackage.xxx'
- it is not clear which value to use for the bind.mode-property. The usage info shows an example how to
  use it - there are two values possible 'vTable' and 'dispId'. But this should be an integer because  a
  NumberFormatException is thrown. What value can I choose here?
- I created classes for MS-Word, but there are 100 compilation errors. Most errors are in Application.java
       * Some classes are not found (--> not generated??)
       * Illegal type casts. e.g. Object to float
        
          public float PointsToLines(float points) {
             return (float)this._invokeNativeObject(170, new Object[] { this.getPointer(), points}, float.class);
           }
       * Muliple implementations of Interface methods
- cannot specify output directory of the generated files, tt's always somewhere in the users-directory

In general this generator is useless for generating wrapper classes.
Have you tried it yourself with MS-Word type library?







Daniel Doubrovkine

unread,
Oct 25, 2013, 8:59:08 AM10/25/13
to jna-...@googlegroups.com
That issue is https://github.com/twall/jna/issues/273. I wasn't able to fix this, Tobias, would be great if you could look into it - most people have 64-bit windows now.


--
You received this message because you are subscribed to the Google Groups "Java Native Access" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jna-users+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

wolf....@gmx.net

unread,
Oct 25, 2013, 9:01:57 AM10/25/13
to jna-...@googlegroups.com
- it is not possible to define the package name. Always is 'mypackage.xxx' --> Thats true, you can change the package name easily with the refactoring feature of your ide

- it is not clear which value to use for the bind.mode-property. The usage info shows an example how to 
  use it - there are two values possible 'vTable' and 'dispId'. But this should be an integer because  a 
  NumberFormatException is thrown. What value can I choose here? Currently only the generation of vtable is supported, the disp id generation currently under development!
- I created classes for MS-Word, but there are 100 compilation errors. Most errors are in Application.java --> Ypu get a lot of compilation errors, becaue Word has a lot of dependent tlb libraries, that is currently not supported by the generator. A way is to use the gernerated java code as a base and to remove the methods you need manually.
- cannot specify output directory of the generated files, tt's always somewhere in the users-directory --> Yes it is a fix path, but it is printed to system out so you can easlily find it!


In general this generator is useless for generating wrapper classes. 
Have you tried it yourself with MS-Word type library? --> Yes I tried it, but for Word support you should not use it, background is Microsoft recommend to use for word a disp id binding, which is still under development.

Thanks for your inpout. I`ll take it to improve the COM support, special the TLB generation.

wolf.tobias

unread,
Oct 25, 2013, 12:07:03 PM10/25/13
to jna-...@googlegroups.com
Will take all the comments to create a new version weich also Supports 64bit but it takes some time 


Von Samsung Mobile gesendet



-------- Ursprüngliche Nachricht --------
Von: Daniel Doubrovkine <dbl...@dblock.org>
Datum:
An: jna-...@googlegroups.com
Betreff: Re: MS-Word COM Automation not working


That issue is https://github.com/twall/jna/issues/273. I wasn't able to fix this, Tobias, would be great if you could look into it - most people have 64-bit windows now.
On Fri, Oct 25, 2013 at 8:28 AM, Andreas Nagel <anag...@gmail.com> wrote:

Yes, I run the Ant-task and the unit tests for COM also failed on 64-Bit JVM.

I installed a 32-Bit JVM and now it works. So it looks like it's not a MS-Word issue but more the
implementation of the jna-platform library (dll/jar). Seems that there are problems in converting the data types.

By the way. Just tested your generator for MS-Word. I found a lot of problems:

I use the 32-Bit JVM and the following command option: 
-tlb.file C:\Program Files (x86)\Microsoft Office\Office15\MSWORD.OLB

- it is not possible to define the package name. Always is 'mypackage.xxx'
- it is not clear which value to use for the bind.mode-property. The usage info shows an example how to
  use it - there are two values possible 'vTable' and 'dispId'. But this should be an integer because  a
  NumberFormatException is thrown. What value can I choose here?
- I created classes for MS-Word, but there are 100 compilation errors. Most errors are in Application.java
       * Some classes are not found (--> not generated??)
       * Illegal type casts. e.g. Object to float
        
          public float PointsToLines(float points) {
             return (float)this._invokeNativeObject(170, new Object[] { this.getPointer(), points}, float.class);
           }
       * Muliple implementations of Interface methods
- cannot specify output directory of the generated files, tt's always somewhere in the users-directory

In general this generator is useless for generating wrapper classes.
Have you tried it yourself with MS-Word type library?







--
You received this message because you are subscribed to the Google Groups "Java Native Access" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jna-users+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Tobias Wolf

unread,
Jan 3, 2014, 9:45:58 AM1/3/14
to jna-...@googlegroups.com
I tested the COM stuff here on my Win7/64, with a 64bit JVM and Office 2007 sucessfully.
Reply all
Reply to author
Forward
0 new messages