OLE ignores calls to Microsoft Excel. OLE or Windows problem? [message #698316] |
Tue, 19 July 2011 05:00  |
Eclipse User |
|
|
|
Hi all,
I have a problem regarding OLE.
System (business laptop):
- Win 7 64bit (local Admin rights)
- Java 1.6.0.26 32bit
- Eclipse Indigo (SWT win32 x86: Version 3.7.0.v3735b)
- Office 2010 32bit
This is an example code. I just try to open Microsoft Excel, set visible to true and insert a new workbook. This fails and I cannot identify the reason for that. Maybe it is Java, SWT or a special configuration in Windows (in fact I have no idea because I potentially could exclude everything). The method-invoke "Add" returns a null, NO new workbook is displayed in Excel and in my code the following statements will fail because I need the workbook-object which is null.
The problem background:
- The same environment (JRE and Eclipse, SWT) on a clean Win 7 64bit and Office 32bit works (sounds like a Windows configuration failure). During debugging, I see that Excel opens and with invoking "add" a new workbook occurs.
- Other programming languages (Python with Win32 extensions, Powershell and Visual Basic) can perform the same code. In these languages invoking "add" produces a new visible workbook (sounds like a SWT-OLE or Java problem).
Anybody an idea?
Maybe it is just a security property on my business laptop (it works in other languages?) or maybe its a SWT problem?
Thanks for any ideas I can try.
Markus
Here is my example code (after null-pointer native Excel process will not killed):
private void workbookAdd() {
Display display = new Display();
Shell shell = new Shell(display);
OleFrame frame = new OleFrame(shell, SWT.NONE);
OleControlSite controlSite = new OleControlSite(frame, SWT.NONE, "Excel.Application");
OleAutomation application = new OleAutomation(controlSite);
controlSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
// Set visible
Variant[] arguments = new Variant[1];
arguments[0] = new Variant("true");
int[] ids = application.getIDsOfNames(new String[] { "Visible" });
application.setProperty(ids[0], arguments);
// Get Workbooks-Object
int[] excelIds = application.getIDsOfNames(new String[] { "Workbooks" });
Variant workbooksVariant = application.getProperty(excelIds[0]);
OleAutomation workbooks = workbooksVariant.getAutomation();
// Add new workbook
int idAdd = workbooks.getIDsOfNames(new String[] { "Add" })[0];
Variant add = workbooks.invoke(idAdd);
OleAutomation workbook = add.getAutomation();
//some things I'd like to do with "workbook"-object.
//...
//...
// Close Excel
int invokeID = application.getIDsOfNames(new String[] { "Quit" })[0];
application.invokeNoReply(invokeID);
}
|
|
|
|
Re: OLE ignores calls to Microsoft Excel. OLE or Windows problem? [message #698767 is a reply to message #698765] |
Wed, 20 July 2011 03:50  |
Eclipse User |
|
|
|
For those who have the same problem:
This piece of code deactivates the COM-Addins:
int idAddIns = application.getIDsOfNames(new String[] {"COMAddIns"})[0];
Variant addInsVar = application.getProperty(idAddIns);
OleAutomation addIns = addInsVar.getAutomation();
int countID = addIns.getIDsOfNames(new String[] {"Count"})[0];
Variant countVariant = addIns.getProperty(countID);
int addInsItemID = addIns.getIDsOfNames(new String[] {"Item"})[0];
for (int i = 1; i <= countVariant.getInt(); i++) {
Variant addInsItemVariant = addIns.invoke(addInsItemID, new Variant[] {new Variant(i)});
OleAutomation addInsItem = addInsItemVariant.getAutomation();
int addInsItemConnectID = addInsItem.getIDsOfNames(new String[] { "Connect" })[0];
if (addInsItem.getProperty(addInsItemConnectID).getBoolean()) {
int addInsItemNameID = addInsItem.getIDsOfNames(new String[] { "ProgId" })[0];
Variant addInsItemNameVariant = addInsItem.getProperty(addInsItemNameID);
String name = addInsItemNameVariant.getString();
addInsItem.setProperty(addInsItemConnectID, new Variant(false));
}
}
|
|
|
Powered by
FUDForum. Page generated in 0.07763 seconds