Eclipse Microsoft Integration
This article will demonstrate how eclipse SWT can be used to integrate/use Microsoft applications. eclipse is using the swt gui framework. SWT does allow to integrated Microsoft application via OLE (Object Linking and Embedding ). microsoft Outlook, Microsoft Excel and the file Explorer are used as examples. the examples used are based on Eclipse RCP as this makes it easy to demonstrate the usage but work also with standalone Java application (which are using SWT ).
This article assumes that you are already familiar with using the Eclipse IDE and with developing simple eclipse RCP applications.
Table of contents
- 1. Microsoft Object Linking and Embedding
- 2. Microsoft Outlook with eclipse
-
- 2.1. Create Project
- 2.2. Create a command
- 3. Microsoft Excel with eclipse
-
- 3.1. Create Project
- 3.2. Change view code
- 4. Microsoft File Explorer with eclipse
-
5. Thank you
- 6. Questions and discussion
-
7. Links and Literature
-
-
7.1. Source Code
- 7.2. vogella Resources
1. Microsoft Object Linking and Embedding
Windows applications use OLE (Object Linking and Embedding) to allow applications to control other application objects. The following will show how to do this using a few examples.
Obviusly the following examples assume that your are running on Microsoft Windows and have the application which is discussed installed.
2. Microsoft Outlook with eclipse
This example assumes you running an MS operating system and that you have a version of outlook installed.
2.1. Create Project
Create a new project "De. vogella. Microsoft. Outlook". See eclipse RCP for details. Use the "Hello RCP" as a template.
2.2. Create a command
Using extensions create the command "de. vogella. microsoft. outlook. sendemail "and add it to the menu. see eclipse command for details. program the default handler "de. vogella. microsoft. outlook. handler. sendemail"
The following coding will create and open the email for the user. It also assumes that you have a file c: \ temp \ test.txt which will be attached to the email.
Package de. vogella. microsoft. outlook. handler; import Java. io. file; import Org. eclipse. core. commands. export acthandler; import Org. eclipse. core. commands. executionevent; import Org. eclipse. core. commands. executionexception; import Org. eclipse. jface. dialogs. messagedialog; import Org. eclipse. SWT. SWT; import Org. eclipse. SWT. ole. win32.ole; import Org. eclipse. SWT. ole. win32.oleautomation; import Org. eclipse. SWT. ole. win32.oleclientsite; import Org. eclipse. SWT. ole. win32.oleframe; import Org. eclipse. SWT. ole. win32.variant; import Org. eclipse. SWT. widgets. display; import Org. eclipse. SWT. widgets. shell; public class sendemail extends acthandler { @ Override Public object execute (executionevent event) throws executionexception {display = display. getcurrent (); shell = new shell (Display); oleframe frame = new oleframe (shell, SWT. none ); // This shoshould start outlook if it is not running yet Oleclientsite site = new oleclientsite (frame, SWT. None, "Ovctl. ovctl" ); Site. doverb (OLE. oleiverb_inplaceactivate ); // Now get the outlook Application Oleclientsite Site2 = new oleclientsite (frame, SWT. None, "Outlook. Application" ); Oleautomation outlook = new oleautomation (Site2 ); // Oleautomation mail = invoke (outlook, "Createitem" , 0/* Mail item */ ). Getautomation (); setproperty (mail, "" , Test@gmail.com" ); /** Empty but cocould also be * predefined */ Setproperty (mail, "BCC" , Test@gmail.com" ); /** Empty but cocould also be * predefined */ Setproperty (mail, "Bodyformat" , 2 /* HTML */ ); Setproperty (mail, "Subject" , "Top news for you" ); Setproperty (mail,"Htmlbody" , "<HTML> Hello <p>, please find some Infos here. ); File = new file ( "C:/temp/test.txt" ); If (file. exists () {oleautomation attachments = getproperty (mail, "Attachments" ); Invoke (attachments, "Add" , "C:/temp/test.txt" );} Else {messagedialog. openinformation (shell, "Info" , "Attachment file C:/temp/test.txt not found; will send email with attachment" );} Invoke (mail, "Display" /* or "send" */); Return NULL;} Private Static oleautomation getproperty (oleautomation auto, string name) {variant varresult = auto. getproperty (Property (Auto, name); If (varresult! = NULL & varresult. GetType ()! = Ole. vt_empty) {oleautomation result = varresult. getautomation (); varresult. dispose (); return result;} return NULL;} Private Static variant invoke (oleautomation auto, string command, string value) {return auto. invoke (Property (Auto, command), new variant [] {new variant (value)});} Private Static variant invoke (oleautomation auto, string command) {return auto. invoke (Property (Auto, command);} Private Static variant invoke (oleautomation auto, string command, int value) {return auto. invoke (Property (Auto, command), new variant [] {new variant (value)});} Private Static Boolean setproperty (oleautomation auto, string name, string value) {return auto. setproperty (Property (Auto, name), new variant (value);} Private Static Boolean setproperty (oleautomation auto, string name, int value) {return auto. setproperty (Property (Auto, name), new variant (value);} Private Static int property (oleautomation auto, string name) {return auto. getidsofnames (New String [] {name}) [0] ;}}
If you now start the application and press the button an email shocould be prepared and shown to the user.
3. Microsoft Excel with eclipse 3.1. Create Project
Create a new project "exceltest". See eclipse RCP for details. Use the "RCP with a view" as a template. Run it and see that is working.
3.2. Change view code
Select view. Java and replace the coding with the following.
Package exceltest; import Org. eclipse. SWT. SWT; import Org. eclipse. SWT. swterror; import Org. eclipse. SWT. ole. win32.oleclientsite; import Org. eclipse. SWT. ole. win32.oleframe; import Org. eclipse. SWT. widgets. composite; import Org. eclipse. UI. part. viewpart; Public Class View extends viewpart {public static final string id = "Exceltest. View" ; Private oleclientsite site; public view (){}@ Override Public void createpartcontrol (composite parent) {try {oleframe frame = new oleframe (parent, SWT. None); Site = new oleclientsite (frame, SWT. None, "Excel. Sheet" );} Catch (swterror e) {system. Out. println ( "Unable to open ActiveX control" ); Return ;}} @ Override Public void setfocus (){ // Have to set the focus see https://bugs.eclipse.org/bugs/show_bug.cgi? Id = 207688 Site. setfocus ();}}
Start your application and Excel shocould be displayed.
4. Microsoft File Explorer with eclipse
Create a new project "De. vogella. Microsoft. fileexplorer". See eclipse RCP for details.
Add a view to the application and the perspective. to display the file Explorer you can use the following code for your view.
Package de. vogella. microsoft. fileexplorer; import Org. eclipse. SWT. SWT; import Org. eclipse. SWT. swterror; import Org. eclipse. SWT. ole. win32.ole; import Org. eclipse. SWT. ole. win32.oleautomation; import Org. eclipse. SWT. ole. win32.oleclientsite; import Org. eclipse. SWT. ole. win32.oleframe; import Org. eclipse. SWT. ole. win32.variant; import Org. eclipse. SWT. widgets. composite; import Org. eclipse. UI. part. viewpart; public class fileexplorerview extends viewpart {private oleclientsite site; static final int navigate = 0x68; @ Override Public void createpartcontrol (composite parent) {try {oleframe frame = new oleframe (parent, SWT. None); Site = new oleclientsite (frame, SWT. None, "Shell. assumer.1" ); Site. doverb (OLE. oleiverb_inplaceactivate); oleautomation auto = new oleautomation (SITE); Auto. Invoke (navigate, new variant [] {new variant ( "C: \ Temp" )});} Catch (swterror e) {system. Out. println ( "Unable to open ActiveX control" ); Return ;}} @ Override Public void setfocus () {site. setfocus ();}}
5. Thank you
Please help me to support this article:
6. Questions and discussion
Before posting questions, please see the vogella FAQ. if you have questions or find an error in this article please use the www.vogella.de Google group. I have created a short list how to create good questions which might also help you.
7. Links and literature 7.1. Source Code
Source code of examples
7.2. vogella Resources
Eclipse RCP training (German) eclipse RCP training with Lars Vogel
Android tutorial introduction to Android programming
GWT tutorial program in Java and compile to JavaScript and HTML
Eclipse RCP tutorial create native applications in Java
JUnit tutorial test your application
Git tutorial put everything you have under distributed version control system
Reprinted from:
Http://www.vogella.de/articles/EclipseMicrosoftIntegration/article.html#microsoftole
Open Excel document
Reprinted from:
Http://hi.baidu.com/btb368/blog/item/886906e892c5ee35b90e2d2d.html