Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » MS Word with OleControlSite
MS Word with OleControlSite [message #442371] Thu, 02 September 2004 08:09 Go to next message
Eclipse UserFriend
Originally posted by: l.heisterkamp.web.de

Hello,

I'm trying to control MS Word with the swt.ole methods.

I have two problems:
1. How can I tell the OleControlSite not to open an empty document at
first? I always want to open an existing one. I tried to use
OleClientSite, but then the documentBeforeClosed listener is not
working...
2. The documentBeforeClosedListener works fine, but how do I know,
which document fires the event?

Thank you for help,

LH

My Code example:
//--------------------------------
package Test;

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

import org.eclipse.swt.widgets.*;

public class OLETest {

/** OLE instance of MS Word */
private OleControlSite controlSite;

/** OLE automatiion of MS Word */
private OleAutomation automation;

/** OLE automations of the Word application */
private OleAutomation wordapp;
private OleAutomation documents;
private OleAutomation doc1, doc2;


/**
* Creates a new OLE instance of MS Word with a new document
* ??? How to open a ControlSite with an existing document ???
* ClientSite can do it, but then I cannot add the Listeners...
*/
private void WordOLE(Shell shell){
OleFrame frame = new OleFrame(shell, SWT.NONE);
this.controlSite = new OleControlSite(frame, SWT.NONE,
"Word.Document");
this.controlSite.doVerb(OLE.OLEIVERB_OPEN);

// get Word.Document automation
this.automation = new OleAutomation(this.controlSite);

// get Application automation
int [] dispInfo = this.automation.getIDsOfNames(
new String[]{"Application"});
Variant variant = this.automation.getProperty(dispInfo[0]);
this.wordapp = variant.getAutomation();

// get Application.Documents automation
dispInfo = this.wordapp.getIDsOfNames(
new String[] {"Documents"});
variant = this.wordapp.getProperty(dispInfo[0]);
this.documents = variant.getAutomation();
}


/**
* Opens a Word document
* @param filename
* @return OleAutomation of the document
*/
private OleAutomation openDocument(String filename){
int[] id = documents.getIDsOfNames(new String[] {"Open"});

Variant var[]= new Variant[1];
var[0] = new Variant(filename);

OleAutomation doc = documents.invoke(id[0],var).getAutomation();
if (doc!=null){
int activateId = doc.getIDsOfNames(
new String[] {"Activate"})[0];
doc.invoke(activateId);
}
return doc;
}


/**
* set listener (only works with OleControlSite,
* OleClientSite does not listen properly
* ??? How to resolve, which document has been closed or saved ???
*/
private void setListener(){
OleListener listener = new OleListener() {
public void handleEvent(OleEvent event) {
switch (event.type){
case 0x6:
System.out.println("DocumentBeforeClosedEvent");
break;
}
}
};
controlSite.addEventListener(wordapp, 0x6, listener);
}


/**
* Creates Word-OLE instance,
* add listeners
* opens two documents
*/
public static void main(String[] args) {

OLETest test = new OLETest();

Display display = new Display();
Shell shell = new Shell(display);
shell.open();

test.WordOLE(shell);
test.setListener();

// Open documents
test.doc1 = test.openDocument("c:\\test1.doc");
test.doc2 = test.openDocument("c:\\test2.doc");

while(!shell.isDisposed()){
if (!display.readAndDispatch()) {
display.sleep();
}
}
test.controlSite.dispose();
}

}
Re: MS Word with OleControlSite [message #442373 is a reply to message #442371] Thu, 02 September 2004 11:27 Go to previous messageGo to next message
Tiberiu Caprita is currently offline Tiberiu CapritaFriend
Messages: 68
Registered: July 2009
Member
> I have two problems:
> 1. How can I tell the OleControlSite not to open an empty document at
> first? I always want to open an existing one. I tried to use
> OleClientSite, but then the documentBeforeClosed listener is not
> working...
Use OleControlSite constructor with File parameter.
(OleClientSite: Read Swt Ole specs, is clear there written OleControlSite
purpose to listen Events.)

> 2. The documentBeforeClosedListener works fine, but how do I know,
> which document fires the event?
You have to check Event Params (one of them is Document), looking for Doc
Name/Path

Tiberiu
Re: MS Word with OleControlSite [message #442473 is a reply to message #442373] Fri, 03 September 2004 06:03 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: l.heisterkamp.web.de

Hello,

thank you for answering.

> You have to check Event Params (one of them is Document), looking for Doc
> Name/Path
I solved the problem, how to distinguish between the events of the two
documents: I can add the OleListener to the document automation, then I
receive events only for this document.

> Use OleControlSite constructor with File parameter.

OleControlSite does not have a contructor with a File parameter...
Only constructor:
public OleControlSite(Composite parent, int style, String progId)
How can I transfer a File parameter?

bye,
LH
Re: MS Word with OleControlSite [message #442474 is a reply to message #442473] Fri, 03 September 2004 10:02 Go to previous messageGo to next message
Tiberiu Caprita is currently offline Tiberiu CapritaFriend
Messages: 68
Registered: July 2009
Member
> How can I transfer a File parameter?
Yes, it seems a little bit wrong my previous answer.

Inded it is a Close Event on Document level so that if you can listen that
one.

About OleControlSite, my advice is (if you don't need a empty Doc) to get
a OleControlSite with "Word.Application", then using OleAutomation for
Application go in Documents and open the file you want. It seems you need
to make WordApplication visible in order to see something.

Regards,
Tiberiu
Re: MS Word with OleControlSite [message #442476 is a reply to message #442474] Fri, 03 September 2004 11:17 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: l.heisterkamp.web.de

Hello,

thanks for your answer!

> About OleControlSite, my advice is (if you don't need a empty Doc) to get
> a OleControlSite with "Word.Application", then using OleAutomation for
> Application go in Documents and open the file you want. It seems you need
> to make WordApplication visible in order to see something.

I tried to open a ControlSite with "Word.Application" , but my program
hangs completely after executing
this.controlSite =
new OleControlSite(frame, SWT.NONE, "Word.Application");

Any ideas how to solve this?

Best regards,
LH
Re: MS Word with OleControlSite [message #442477 is a reply to message #442476] Fri, 03 September 2004 11:40 Go to previous messageGo to next message
Tiberiu Caprita is currently offline Tiberiu CapritaFriend
Messages: 68
Registered: July 2009
Member
Na, Ja,
I already told to you, Word should be done visible.

I don't know exactly what do you mean with Hang completly.

I'm posting to you some code (you should see the spirit, it doesn't run
with copy&paste); if you follow this idea, you have a OleAutomation of a
File, Word being opened with OleControlSite.

Grüße,
Tiberiu

OleFrame frame = new OleFrame(wShell,SWT.NONE);
wShell.setLayout(new GridLayout());
frame.setLayoutData(new GridData(GridData.FILL_BOTH));

OleControlSite site = new
OleControlSite(frame,SWT.NONE,"Word.Application");

site.doVerb(OLE.OLEIVERB_OPEN);
OleAutomation pApp = new OleAutomation(site);
int [] dispInfo;
Variant [] rgvarg;
int [] nargs;
Variant vRes;

//open File
File pFile = ...;
dispInfo = pApp.getIDsOfNames(new String[] {"Documents"});
vRes = pApp.getProperty(dispInfo[0]);
OleAutomation pDocs = vRes.getAutomation();
vRes.dispose();

dispInfo = pDocs.getIDsOfNames(new String[] {"Open","FileName");
rgvarg = new Variant[1];
nargs = new int[1];
rgvarg[0] = new Variant(pFile.getPath());
nargs[0] = dispIndo[1];
vRes = pDocs.invoke(dispInfo[0], rgvarg, nargs);
OleAutomation pDoc = vRes.getVariant();
vRes.dispose();
pDocs.dispose();


//Word should be done Visible!
dispInfo = pApp.getIDsOfNames(new String[] {"Visible"});
rgvarg = new Variant[1];
rgvarg[0] = new Variant(bIsVisible);
boolean bRes = pApp.setProperty(dispInfo[0],rgvarg);

while (!wShell.isDisposed()) {
if (!wShell.getDisplay().readAndDispatch())
wShell.getDisplay().sleep();
}
Re: MS Word with OleControlSite [message #442478 is a reply to message #442477] Fri, 03 September 2004 12:59 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: l.heisterkamp.web.de

Hello,

> I already told to you, Word should be done visible.
I know, I have tried it...

> I don't know exactly what do you mean with Hang completly.
the program does not respond to anything and the following lines are not
executed. (see my code)

> I'm posting to you some code (you should see the spirit, it doesn't run
> with copy&paste); if you follow this idea, you have a OleAutomation of a
> File, Word being opened with OleControlSite.

Thank you very much, it looks very similar to my code. I' posting my
code, so you can see, which statements are executed and where the
execution stops.

Best wishes,
LH

package de;

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

import org.eclipse.swt.widgets.*;

public class OLETest {

/** OLE instance of MS Word */
private OleControlSite controlSite;

/** OLE automatiion of MS Word */
private OleAutomation automation;

/** OLE automations of the Word application */
private OleAutomation wordapp;
private OleAutomation documents;
private OleAutomation doc1, doc2;


/**
* Creates a new OLE instance of MS Word with a new document
*/
private void WordOLE(Shell shell){
OleFrame frame = new OleFrame(shell, SWT.NONE);
frame.setLayoutData(new GridData(GridData.FILL_BOTH));

System.out.println("This message is shown");

this.controlSite = new OleControlSite(frame, SWT.NONE,
"Word.Application");

System.out.println("this message never comes up, the program
does not respond to anything");

this.controlSite.doVerb(OLE.OLEIVERB_OPEN);

this.wordapp = new OleAutomation(this.controlSite);
System.out.println("4");

int id[] = wordapp.getIDsOfNames(new String[] {"Visible"});
Variant var[]= new Variant[1];
var[0] = new Variant(true);
wordapp.invoke(id[0], var);
System.out.println("5");

// get Application.Documents automation
int[] dispInfo =
this.wordapp.getIDsOfNames(new String[] {"Documents"});
Variant variant = this.wordapp.getProperty(dispInfo[0]);
this.documents = variant.getAutomation();
}


/**
* Opens a Word document
* @param filename
* @return OleAutomation of the document
*/
private OleAutomation openDocument(String filename){
int[] id = documents.getIDsOfNames(new String[] {"Open"});

Variant var[]= new Variant[1];
var[0] = new Variant(filename);

OleAutomation doc = documents.invoke(id[0],var).getAutomation();
if (doc!=null){
int activateId =
doc.getIDsOfNames(new String[] {"Activate"})[0];
doc.invoke(activateId);
}
return doc;
}

/**
* set listener (only works with OleControlSite,
* OleClientSite does not listen properly
* ??? How to resolve, which document has been closed or saved ???
*/
private void setListener(OleAutomation auto){
OleListener listener = new OleListener() {
public void handleEvent(OleEvent event) {
System.out.println(event.type);
switch (event.type){
case 0x6:
//int i = event.arguments[1].getDispatch
().getAddress();
System.out.println("DocumentBeforeClosedEvent");
break;
}
}
};
controlSite.addEventListener(auto, 0x6, listener);
}


/**
* Creates Word-OLE instance,
* add listeners
* opens two documents
*/
public static void main(String[] args) {
System.out.println("-1");

OLETest test = new OLETest();

Display display = new Display();
Shell shell = new Shell(display);
shell.open();
System.out.println("0");

test.WordOLE(shell);
System.out.println("5");

// Open documents
test.doc1 = test.openDocument("c:\\test1.doc");
test.setListener(test.doc1);
test.doc2 = test.openDocument("c:\\test2.doc");

while(!shell.isDisposed()){
if (!display.readAndDispatch()) {
display.sleep();
}
}
test.controlSite.dispose();
}

}
Re: MS Word with OleControlSite [message #442481 is a reply to message #442478] Fri, 03 September 2004 15:16 Go to previous messageGo to next message
Tiberiu Caprita is currently offline Tiberiu CapritaFriend
Messages: 68
Registered: July 2009
Member
Hi,
I tried your code and I go further than "This message is shown
this message never comes up, the program does not respond to anything".

On my computer it quite work after some alterations (Visible is a
property, you should call setProperty).

I guess you have a German Word, I too.
Anyway, it seems a Word Problem; you have firstly to check if in Registry
is defined Word.Application (but I can't believed you could have
Word.Document without Application).
It is really interesting to see what would happen in VBA (in Excel Macro
by example) if you write a
Dim bla as Object
set bla = CreateObject("Word.Application")

Maybe you have to do some Windows/Word updates on your machine.

And in fact, if is working Word.Document, you can use it like that, get
the application from there (Word.Document gives you back the OleAutomation
for a Document), and just to work as previously, eventually closing the
empty Doc.

Regards,
Tiberiu
Re: MS Word with OleControlSite [message #442545 is a reply to message #442481] Tue, 07 September 2004 19:20 Go to previous messageGo to next message
Veronika Irvine is currently offline Veronika IrvineFriend
Messages: 1272
Registered: July 2009
Senior Member
I get the same result as LH. When creating the OleControlSite, the call in
OlePropertyChangeSink.connect to
IConnectionPointContainer.FindConnectionPoint never returns and thus the
application hangs.

LH, please enter a bug report against Platform SWT.

I commented out the IConnectionPointContainer.FindConnectionPoint call and
was able to proceed further. However, this code will open Word in a
separate window (not embedded in the application). LH, is that what you are
trying to achieve?

"Tiberiu Caprita" <capritat@hotmail.com> wrote in message
news:cha1sc$bip$1@eclipse.org...
> Hi,
> I tried your code and I go further than "This message is shown
> this message never comes up, the program does not respond to anything".
>
> On my computer it quite work after some alterations (Visible is a
> property, you should call setProperty).
>
> I guess you have a German Word, I too.
> Anyway, it seems a Word Problem; you have firstly to check if in Registry
> is defined Word.Application (but I can't believed you could have
> Word.Document without Application).
> It is really interesting to see what would happen in VBA (in Excel Macro
> by example) if you write a
> Dim bla as Object
> set bla = CreateObject("Word.Application")
>
> Maybe you have to do some Windows/Word updates on your machine.
>
> And in fact, if is working Word.Document, you can use it like that, get
> the application from there (Word.Document gives you back the OleAutomation
> for a Document), and just to work as previously, eventually closing the
> empty Doc.
>
> Regards,
> Tiberiu
>
>
Re: MS Word with OleControlSite/Excel [message #442637 is a reply to message #442545] Thu, 09 September 2004 15:29 Go to previous message
Tiberiu Caprita is currently offline Tiberiu CapritaFriend
Messages: 68
Registered: July 2009
Member
Hi Veronika,

I had previously discussions about another problem where a Ole code
working well to me (Windows2000), doesn't work to other person, as
reported.

Can you test also the problem regarding Excel, maybe is the case to open a
bug too?

Please see:

http://dev.eclipse.org/newslists/news.eclipse.platform.swt/m sg13031.html

and the others messages.

Regards,
Tiberiu
Previous Topic:InternalFrame
Next Topic:J9 SWT Shell.open() causes pocketIE to close
Goto Forum:
  


Current Time: Thu Sep 19 07:40:45 GMT 2024

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

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

Back to the top