Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » SWT - Active X
SWT - Active X [message #462135] Fri, 07 October 2005 01:45 Go to next message
Eclipse UserFriend
Originally posted by: tom.yahoo.com

Hi,

I am writing a simple SWT program which opens a existing WORD document and
allows user to edit the WORD document and save it.

I have two questions

1) How to open the WORD document inside the SWT Frame or canvas, without
openining seperately

2) Currently cant save the WORD doc, it get stuck, Just want user to
save the data via WORD application. Sometimes it doent get saved to the
opened document.

thanks
Re: SWT - Active X [message #462173 is a reply to message #462135] Fri, 07 October 2005 14:05 Go to previous messageGo to next message
Veronika Irvine is currently offline Veronika IrvineFriend
Messages: 1272
Registered: July 2009
Senior Member
Here is an example. It contains two classes WordExample and OleWordBasic (a
wrapper for the WordBasic API for Word). It saves to a file and has some
menu commands that invoke WordBasic functions.

import java.io.File;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.ole.win32.*;

public class WordExample {

private Shell shell;
private OleFrame frame;
private OleClientSite clientSite;
private OleAutomation dispInterface;
private OleAutomation application;
private OleWordBasic wordBasic;
private File file;
private MenuItem fileItems;
private MenuItem containerItems;

private void changeFont() {
// Change the size of the font to 24, make it bold and italic
wordBasic.FormatFont(24, true, true);
}

private void close() {
// cleanup automation objects
if (wordBasic != null)
wordBasic.dispose();
wordBasic = null;
if (application != null)
application.dispose();
application = null;
if (dispInterface != null)
dispInterface.dispose();
dispInterface = null;
if ((shell != null) && (!shell.isDisposed()))
shell.dispose();
shell = null;
}

private void copyToClipboard() {
wordBasic.EditSelectAll();
wordBasic.EditCopy();
wordBasic.EndOfDocument(0);
}

private Menu createMenuBar() {
Menu bar = new Menu(shell, SWT.BAR);
shell.setMenuBar(bar);
// File menu items
fileItems = new MenuItem(bar, SWT.CASCADE);
fileItems.setText("&File");
Menu menu = new Menu(bar);
MenuItem item = new MenuItem(menu, SWT.PUSH);
item.setText("&Save");
item.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
menuFileSave();
}
});
item = new MenuItem(menu, SWT.PUSH);
item.setText("&Save As...");
item.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
menuFileSaveAs();
}
});
item = new MenuItem(menu, SWT.PUSH);
item.setText("&Exit");
item.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
menuFileExit();
}
});
fileItems.setMenu(menu);
// Container menu items
containerItems = new MenuItem(bar, SWT.CASCADE);
containerItems.setText("&Java");
menu = new Menu(bar);
item = new MenuItem(menu, SWT.PUSH);
item.setText("&Copy to Clipboard");
item.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
copyToClipboard();
}
});
item = new MenuItem(menu, SWT.PUSH);
item.setText("&Check Spelling");
item.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
spellCheck();
}
});
item = new MenuItem(menu, SWT.PUSH);
item.setText("C&hange font");
item.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
changeFont();
}
});
item = new MenuItem(menu, SWT.PUSH);
item.setText("&Insert Text");
item.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
insertText();
}
});
containerItems.setMenu(menu);
return bar;
}
private boolean getFile(int style) {
FileDialog dialog = new FileDialog(shell, style);
dialog.setFilterExtensions(new String[]{"*.doc"});
dialog.setFilterNames(new String[] {"Word Documents (*.doc)"});
dialog.setFilterPath("C:");
String fileName = dialog.open();
if (fileName == null) {
return false;
}
file = new File(fileName);
shell.setText(file.getName());
return true;
}
private void initWordBasicIF() {
// Get generic IDispatch interface
dispInterface = new OleAutomation(clientSite);
// Get Application
int[] appId = dispInterface.getIDsOfNames(new String[]{"Application"});
if (appId == null) OLE.error(OLE.ERROR_APPLICATION_NOT_FOUND);
Variant pVarResult = dispInterface.getProperty(appId[0]);
if (pVarResult == null) OLE.error(OLE.ERROR_APPLICATION_NOT_FOUND);
application = pVarResult.getAutomation();
// Get Word Basic
int[] wbId = application.getIDsOfNames(new String[]{"WordBasic"});
if (wbId == null) OLE.error(OLE.ERROR_APPLICATION_NOT_FOUND);
Variant pVarResult2 = application.getProperty(wbId[0]);
if (pVarResult2 == null) OLE.error(OLE.ERROR_APPLICATION_NOT_FOUND);
wordBasic = new OleWordBasic( pVarResult2.getAutomation());
}
private void insertText() {
wordBasic.SelType();
wordBasic.EndOfDocument(0);
wordBasic.InsertPara();
wordBasic.Insert("Adding text to the end of the document.");
}
public static void main(String[] args) {
//SWTOLE.DebugInfo = true;
WordExample workspace = new WordExample();
workspace.open();
workspace.run();
workspace.close();
}
private void menuFileExit() {
shell.close();
}
private void menuFileSave() {
if (file != null && file.exists()) {
File tempFile = new File(file.getAbsolutePath() + ".tmp");
file.renameTo(tempFile);
//if (clientSite.save(file, true)) {
if (clientSite.save(file, true)){
// save was successful so discard the backup
tempFile.delete();
} else {
// save failed so restore the backup
tempFile.renameTo(file);
}
} else {
menuFileSaveAs();
}
}
private void menuFileSaveAs() {
if (!getFile(SWT.SAVE)) return;
//clientSite.save(file, true);
clientSite.save(file, true);
}
private void open() {
// Init shell
shell = new Shell();
shell.setLayout(new FillLayout());
// Add a menubar
createMenuBar();
if (!getFile(SWT.OPEN)) return;
// Create the Document:
// Every Document must have an associated OleFrame
frame = new OleFrame(shell, SWT.NONE);
// add your own File, Window and Container menus - optional
frame.setFileMenus(new MenuItem[] {fileItems});
frame.setContainerMenus(new MenuItem[] {containerItems});
// Finally, create the Document Container and init the Document
try {
clientSite = new OleClientSite(frame, SWT.NONE, file);
} catch (SWTException e) {
MessageBox message = new MessageBox(shell);
message.setMessage("Unable to open the selected document. "+e.getMessage());
message.open();
shell.dispose();
return;
}
// Create The Word Basic Automation object
initWordBasicIF();
// Open the Document in the InPlaceActive state
clientSite.doVerb(OLE.OLEIVERB_SHOW);
shell.open();
}
private void run() {
Display display = shell.getDisplay();
while (!shell.isDisposed() && shell.isVisible() ) {
if (!display.readAndDispatch())
display.sleep();
}
}
private void spellCheck() {
wordBasic.ToolsSpelling();
}
}

////!!!!! OleWordBasic class

import org.eclipse.swt.*;
import org.eclipse.swt.ole.win32.*;

public class OleWordBasic {
// Generated from typelib filename: wb70en32.tlb
private OleAutomation oleObject;
public static final int wdOpenFormatAuto = 0;
public static final int wdOpenFormatDocument = 1;
public static final int wdOpenFormatTemplate = 2;
public static final int wdOpenFormatRTF = 3;
public static final int wdOpenFormatText = 4;
public static final int wdOpenFormatUnicodeText = 5;
public OleWordBasic(OleAutomation automationObject) {
oleObject = automationObject;
}
public void AppShow(){
// dispid = 33057 name = AppShow
// You can hard code the DISPID if you know it before hand - this is of
course the fastest way
int dispIdMember = 33057;
// Alternatively, you can look up the DISPID dynamically
//int[] rgdispid = getIDsOfNames(new String[]{"AppShow"});
//int dispIdMember = rgdispid[0];
oleObject.invokeNoReply(dispIdMember);
}
public void dispose() {
if (oleObject != null)
oleObject.dispose();
oleObject = null;
}
//Removes the selection without putting it on the Clipboard
public void EditClear(){
// dispid = 32897 name = EditClear
// You can hard code the DISPID if you know it before hand - this is of
course the fastest way
int dispIdMember = 32897;
// Alternatively, you can look up the DISPID dynamically
//int[] rgdispid = getIDsOfNames(new String[]{"EditClear"});
//int dispIdMember = rgdispid[0];
oleObject.invokeNoReply(dispIdMember);
}
// Copies the selection and puts it on the Clipboard
public void EditCopy(){
// dispid = 109 name = EditCopy
// You can hard code the DISPID if you know it before hand - this is of
course the fastest way
int dispIdMember = 109;
// Alternatively, you can look up the DISPID dynamically
//int[] rgdispid = getIDsOfNames(new String[]{"EditCopy"});
//int dispIdMember = rgdispid[0];
oleObject.invokeNoReply(dispIdMember);
}
//Selects the entire document"
public void EditSelectAll(){
// dispid = 237
// You can hard code the DISPID if you know it before hand - this is of
course the fastest way
int dispIdMember = 237;
// Alternatively, you can look up the DISPID dynamically
//int[] rgdispid = getIDsOfNames(new String[]{"EditSelectAll"});
//int dispIdMember = rgdispid[0];
oleObject.invokeNoReply(dispIdMember);
}
// Moves the Insertion point or, if Select is non-zero, the active end
// of the selection to the end of the document
// This method return 0 if the insertion point or the active end of the
// selection was not moved (ie it was already at the end of the document).
// This method returns -1 if the insertion point or the active end of the
selection has moved.
public short EndOfDocument(int select){
// dispid=49169, type=METHOD, name="EndOfDocument"
// You can hard code the DISPID if you know it before hand - this is of
course the fastest way
int dispIdMember = 49169;
// Alternatively, you can look up the DISPID dynamically
//int[] rgdispid = oleObject.getIDsOfNames(new String[]{"EndOfDocument"});
//int dispIdMember = rgdispid[0];
Variant[] rgvarg = null;
if (select >= 0 ){
rgvarg = new Variant[1];
rgvarg[0] = new Variant(select);
}
Variant pVarResult = oleObject.invoke(dispIdMember, rgvarg);
if (pVarResult == null) return -1;
return pVarResult.getShort();
}
// Opens an existing document or template
public void FileOpen(String name)
{
// dispid = 80 name = FileOpen
// You can hard code the DISPID if you know it before hand - this is of
course the fastest way
//int dispIdMember = 80;
// Alternatively, you can look up the DISPID dynamically
int[] rgdispid = oleObject.getIDsOfNames(new String[]{"FileOpen"});
int dispIdMember = rgdispid[0];
Variant[] rgvarg = new Variant[1];
rgvarg[0] = new Variant(name);
oleObject.invokeNoReply(dispIdMember, rgvarg);
}
// Saves the active document or template
public void FileSave(){
// dispid = 83 name = FileSave
// You can hard code the DISPID if you know it before hand - this is of
course the fastest way
int dispIdMember = 83;
// Alternatively, you can look up the DISPID dynamically
//int[] rgdispid = getIDsOfNames(new String[]{"FileSave"});
//int dispIdMember = rgdispid[0];
oleObject.invokeNoReply(dispIdMember);
}
public void FormatFont(int points, boolean bold, boolean italic){
// dispid=174, type=METHOD, name=FormatFont
// You can hard code the DISPID if you know it before hand - this is of
course the fastest way
//int dispIdMember = 174;
// Alternatively, you can look up the DISPID dynamically
int[] rgdispid = oleObject.getIDsOfNames(new String[]{"FormatFont",
"Points", "Color", "Font", "Bold", "Italic"});
int dispIdMember = rgdispid[0];
int index = 0;
Variant[] rgvarg = new Variant[3];
int[] rgdispidNamedArgs = new int[3];
rgvarg[0] = new Variant(points);
rgdispidNamedArgs[0] = rgdispid[1];
rgvarg[1] = new Variant(bold?1:0);
rgdispidNamedArgs[1] = rgdispid[4];
rgvarg[2] = new Variant(italic?1:0);
rgdispidNamedArgs[2] = rgdispid[5];
oleObject.invokeNoReply(dispIdMember, rgvarg, rgdispidNamedArgs);
}
public void Insert(String text){
//dispid=32786, type=METHOD, name="Insert"
// You can hard code the DISPID if you know it before hand - this is of
course the fastest way
int dispIdMember = 32786;
// Alternatively, you can look up the DISPID dynamically
//int[] rgdispid = oleObject.getIDsOfNames(new String[]{"Insert"});
//int dispIdMember = rgdispid[0];
Variant[] rgvarg = new Variant[1];
rgvarg[0] = new Variant(text);
oleObject.invokeNoReply(dispIdMember, rgvarg);
}
public void InsertFile(String name){
//dispid=164, type=METHOD, name="InsertFile"
// You can hard code the DISPID if you know it before hand - this is of
course the fastest way
//int dispIdMember = 164;
// Alternatively, you can look up the DISPID dynamically
int[] rgdispid = oleObject.getIDsOfNames(new String[]{"InsertFile",
"Name"});
int dispIdMember = rgdispid[0];
Variant[] rgvarg = new Variant[1];
rgvarg[0] = new Variant(name);
oleObject.invokeNoReply(dispIdMember, rgvarg);
}
public void InsertFile(
String name,
//Variant Range,
boolean confirmConversions
//boolean Link
)
{
//dispid=164, type=METHOD, name="InsertFile"
// You can hard code the DISPID if you know it before hand - this is of
course the fastest way
//int dispIdMember = 164;
// Alternatively, you can look up the DISPID dynamically
int[] rgdispid = oleObject.getIDsOfNames(new String[]{"InsertFile", "Name",
"ConfirmConversions"});
int dispIdMember = rgdispid[0];
Variant[] rgvarg = new Variant[2];
int[] rgdispidNamedArgs = new int[2];
rgvarg[0] = new Variant(name);
rgdispidNamedArgs[0] = rgdispid[1];
rgvarg[1] = new Variant(confirmConversions?1:0);
rgdispidNamedArgs[1] = rgdispid[2];
oleObject.invokeNoReply(dispIdMember, rgvarg, rgdispidNamedArgs);
}
public void InsertPara(){
//dispid=32787, type=METHOD, name="InsertPara"
// You can hard code the DISPID if you know it before hand - this is of
course the fastest way
int dispIdMember = 32787;
// Alternatively, you can look up the DISPID dynamically
//int[] rgdispid = oleObject.getIDsOfNames(new String[]{"InsertPara"});
//int dispIdMember = rgdispid[0];
oleObject.invokeNoReply(dispIdMember);
}
public short SelType(){
//dispid=32922, type=METHOD, name="SelType"
// You can hard code the DISPID if you know it before hand - this is of
course the fastest way
int dispIdMember = 32922;
// Alternatively, you can look up the DISPID dynamically
//int[] rgdispid = oleObject.getIDsOfNames(new String[]{"SelType"});
//int dispIdMember = rgdispid[0];
Variant pVarResult = oleObject.invoke(dispIdMember);
if (pVarResult == null) return -1;
return pVarResult.getShort();
}
// Moves the insertion point to the beginning of the first line of the
document
public short StartOfDocument(int select){
// dispid= 49168, type=METHOD, name="StartOfDocument"
// You can hard code the DISPID if you know it before hand - this is of
course the fastest way
int dispIdMember = 49168;
// Alternatively, you can look up the DISPID dynamically
//int[] rgdispid = oleObject.getIDsOfNames(new String[]{"StartOfDocument"});
//int dispIdMember = rgdispid[0];
Variant[] rgvarg = null;
if (select >= 0 ){
rgvarg = new Variant[1];
rgvarg[0] = new Variant(select);
}
Variant pVarResult = oleObject.invoke(dispIdMember, rgvarg);
if (pVarResult == null) return -1;
return pVarResult.getShort();
}
public void ToolsSpelling(){
//dispid=191, type=METHOD, name="ToolsSpelling"
// You can hard code the DISPID if you know it before hand - this is of
course the fastest way
int dispIdMember = 191;
// Alternatively, you can look up the DISPID dynamically
//int[] rgdispid = oleObject.getIDsOfNames(new String[]{"ToolsSpelling"});
//int dispIdMember = rgdispid[0];
oleObject.invokeNoReply(dispIdMember);
}
}
Re: SWT - Active X [message #462342 is a reply to message #462173] Tue, 11 October 2005 02:00 Go to previous messageGo to next message
Peter is currently offline PeterFriend
Messages: 51
Registered: July 2009
Member
Hi,

Can you give us a hint or a reference to do the same using Open Office.

That would be really great.

Thanks,
Re: SWT - Active X [message #462380 is a reply to message #462342] Tue, 11 October 2005 20:26 Go to previous messageGo to next message
Haris Peco is currently offline Haris PecoFriend
Messages: 1072
Registered: July 2009
Senior Member
Peter wrote:

> Hi,
>
> Can you give us a hint or a reference to do the same using Open Office.
>
> That would be really great.
>
> Thanks,
Peter,

It work on windows without change if you have OO and haven't MS Office
(it is transparent, because OO on windows is Ole capable).
With OO you have set swt/rcp view on Linuxu (and other platform probably)
see http://www.snpe.co.yu/index1.php?sadrzaj=html/ooswt.html

Peco
Re: SWT - Active X [message #462401 is a reply to message #462380] Tue, 11 October 2005 23:16 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: TOM.hotmail.com

Thanks for the reply, am running on windows.

Why cant we switch to MS-Word vs Open Office by the Prog IDS without
uninstalling the software so that both MS Office and Open Office can be in
one machine.

i.e same code base but with following change

OleClientSite site = new OleClientSite(frame,
SWT.NONE,"sOffice.StarWriterGlobalDocument");

or

OleClientSite clientSite = new OleClientSite(frame, SWT.NONE,
"Word.Document");

Unfortunate the two behaviours are different and sometimes does not work.

for e.g MS-WORD opens in a SWT container and same code base if we chage to
OO
it opens as a pop up window without residing in SWT container.

Still I cant run the code sample in the link on Windows.

Waiting for a reply

Thanks,
Re: SWT - Active X [message #462405 is a reply to message #462401] Wed, 12 October 2005 01:29 Go to previous messageGo to next message
Haris Peco is currently offline Haris PecoFriend
Messages: 1072
Registered: July 2009
Senior Member
I haven't MS Office and I can't try, but I can do it with OO fine
- you can make oo swt view with OO java API - without Ole
and it is cross-platform
TOM wrote:

> Thanks for the reply, am running on windows.
>
> Why cant we switch to MS-Word vs Open Office by the Prog IDS without
> uninstalling the software so that both MS Office and Open Office can be in
> one machine.
>
> i.e same code base but with following change
>
> OleClientSite site = new OleClientSite(frame,
> SWT.NONE,"sOffice.StarWriterGlobalDocument");
>
> or
>
> OleClientSite clientSite = new OleClientSite(frame, SWT.NONE,
> "Word.Document");
>
> Unfortunate the two behaviours are different and sometimes does not work.
>
> for e.g MS-WORD opens in a SWT container and same code base if we chage to
> OO
> it opens as a pop up window without residing in SWT container.
>
> Still I cant run the code sample in the link on Windows.
>
> Waiting for a reply
>
> Thanks,
Re: SWT - Active X [message #462406 is a reply to message #462401] Wed, 12 October 2005 00:15 Go to previous messageGo to next message
Peter is currently offline PeterFriend
Messages: 51
Registered: July 2009
Member
am running eclipse 3.1, Open Office 1.1.5 on windows XP with JDK 1.5
(update 4)

seems like com.sun.star.comp.beans.OOoBean is not in officebean.jar on
windows

Due to above reason cant run the program on windows

Thanks
Re: SWT - Active X [message #462410 is a reply to message #462406] Wed, 12 October 2005 03:31 Go to previous messageGo to next message
Haris Peco is currently offline Haris PecoFriend
Messages: 1072
Registered: July 2009
Senior Member
Peter wrote:

> am running eclipse 3.1, Open Office 1.1.5 on windows XP with JDK 1.5
> (update 4)
>
> seems like com.sun.star.comp.beans.OOoBean is not in officebean.jar on
> windows
>
> Due to above reason cant run the program on windows
>
> Thanks

No, OOoBean is sross-platform - it doesn't work good on linux gnome
(work in linux kde great, but it is bug), but in windows it work fine

- you have to leave OO libraries in OO (in program directories) and it
doesn't work if you replace libraries (btw in eclipse project)
Re: SWT - Active X [message #462412 is a reply to message #462410] Wed, 12 October 2005 02:09 Go to previous messageGo to next message
Peter is currently offline PeterFriend
Messages: 51
Registered: July 2009
Member
Thanks works fine (gives cannot find msvcr70 error though).

Wondering why the org.eclipse.swt.awt.SWT_AWT is used in the code.

Cant this be done only using SWT. Is there something which cannot be done
only using SWT.

How can we get the control to ADD remove menus in the container in shis
sample.

Thanks,
Peter
Re: SWT - Active X [message #462419 is a reply to message #462412] Wed, 12 October 2005 05:47 Go to previous messageGo to next message
Haris Peco is currently offline Haris PecoFriend
Messages: 1072
Registered: July 2009
Senior Member
Peter wrote:

> Thanks works fine (gives cannot find msvcr70 error though).
>
> Wondering why the org.eclipse.swt.awt.SWT_AWT is used in the code.
>
Original example have swing (frame) control - I deon't know how I can
integrate this without SWT_AWT - OO haven't integration with SWT

> Cant this be done only using SWT. Is there something which cannot be done
> only using SWT.
>
It is possible, probably, but I don't why it is important
> How can we get the control to ADD remove menus in the container in shis
> sample.
You can complete manipulate with OO document - see DeveloperGuide on OO site
Re: SWT - Active X [message #462426 is a reply to message #462419] Wed, 12 October 2005 06:24 Go to previous messageGo to next message
Peter is currently offline PeterFriend
Messages: 51
Registered: July 2009
Member
Doest this mean we are bypassing the SWT Ole Frame and accesing the OO docs
using UNO.

Still searching for the OO developer guide 2.0

I do have the Developer guide 1.5 but cant find a section which discuss
about the ole features with samples

Thanks,
Re: SWT - Active X [message #462456 is a reply to message #462426] Wed, 12 October 2005 15:08 Go to previous message
Haris Peco is currently offline Haris PecoFriend
Messages: 1072
Registered: July 2009
Senior Member
Peter wrote:

> Doest this mean we are bypassing the SWT Ole Frame and accesing the OO
> docs using UNO.
>
> Still searching for the OO developer guide 2.0
>
> I do have the Developer guide 1.5 but cant find a section which discuss
> about the ole features with samples
>
> Thanks,

Peter,
I don't sure that you have manipulate with OO doc with Ole (probably not)
You have to use UNO - Developer Guide is in source (maybe sdk) download
Previous Topic:Custome view and ... ??
Next Topic:Problems Opening GEF Editor: Migration from 3.1 M6->3.1.1
Goto Forum:
  


Current Time: Tue Apr 16 04:05:07 GMT 2024

Powered by FUDForum. Page generated in 0.84055 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top