Home » Eclipse Projects » Standard Widget Toolkit (SWT) » SWT - Active X
SWT - Active X [message #462135] |
Fri, 07 October 2005 01:45 |
Eclipse User |
|
|
|
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 |
Veronika Irvine 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 #462401 is a reply to message #462380] |
Tue, 11 October 2005 23:16 |
Eclipse User |
|
|
|
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,
|
|
| | | | | | | |
Goto Forum:
Current Time: Wed Sep 25 17:18:16 GMT 2024
Powered by FUDForum. Page generated in 0.04957 seconds
|