OLE and Microsoft word [message #445368] |
Thu, 04 November 2004 09:07  |
Eclipse User |
|
|
|
Hi, group.
I would like to have the user open a Microsoft word template (which is
pretty easy to do using the FileDialog) and then create a new Word document
merging the template data and some data coming from my Java application. I
heard this is possible using OLE in SWT. Has anyone does this before?
Thanx,
- Kalman
|
|
|
|
|
Re: OLE and Microsoft word [message #445397 is a reply to message #445390] |
Thu, 04 November 2004 21:59   |
Eclipse User |
|
|
|
I have done it , but I cann't give you all code.
In doNew method , I have used Word template to create a new document.
/*
* Created on 2003-12-11
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
import java.io.File;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.ole.win32.OLE;
import org.eclipse.swt.ole.win32.OleClientSite;
import org.eclipse.swt.ole.win32.OleFrame;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.FileDialog;
/**
* @author Kite
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public class MOREditorView implements IContentView{
private Control contentControl;
private IPerspective parent;
private OleClientSite controlSite;
private OleFrame clientFrame;
private String cmd;
public MOREditorView(IPerspective persp){
this.parent = persp;
}
/* (non-Javadoc)
* @see
com.mor.ui.IContentView#createControl(org.eclipse.swt.widget s.Composite)
*/
public Control createControl(Composite parent) {
clientFrame = new OleFrame(parent, SWT.CLIP_CHILDREN);
clientFrame.setBackground(clientFrame.getDisplay().getSystem Color(SWT.COLOR_LIST_BACKGROUND));
contentControl = clientFrame;
return contentControl;
}
public OleClientSite getEditor(){
return controlSite;
}
/* (non-Javadoc)
* @see com.mor.ui.IContentView#getControl()
*/
public Control getControl() {
return contentControl;
}
/* (non-Javadoc)
* @see com.mor.ui.IContentView#getParent()
*/
public IPerspective getParent() {
return parent;
}
public void registerActions(){
MOREditorTaskView view = (MOREditorTaskView)(getParent().getTaskView());
view.registerAction(new NewDocumentAction());
view.registerAction(new OpenDocumentAction());
view.registerAction(new SaveDocumentAction());
view.registerAction(new Separator());
view.registerAction(new PrintDocumentAction());
}
private void doPrint() {
if(controlSite == null)
return;
BusyIndicator.showWhile(controlSite.getDisplay(), new Runnable() {
public void run() {
int result = controlSite.exec(OLE.OLECMDID_PRINT,
OLE.OLECMDEXECOPT_PROMPTUSER, null, null);
if(result != OLE.S_OK)
System.out.println("Cann't print!!!" + result);
// note: to check for success: above == SWTOLE.S_OK
}
});
}
private void doSave(){
if(controlSite == null)
return;
String docPath = ResourceManager.getString("MOREditorView.DocmentPath");
if(docPath == null || docPath.length()==0) {
DirectoryDialog d = new DirectoryDialog(clientFrame.getShell());
d.open();
docPath = d.getFilterPath();
if(docPath.length() > 3)
docPath = docPath + "\\";
}
IGenerator generator = null;
String generatorClassName =
ResourceManager.getString("MOREditorView.GeneratorClass");
if(generatorClassName != null && generatorClassName.length() > 0){
try {
generator =
(IGenerator)Class.forName(generatorClassName).newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}else
generator = new MORGenerator();
if(cmd == null)
controlSite.save(new File(docPath+generator.generate()),true);
else
controlSite.save(new File(cmd.substring(0,cmd.lastIndexOf(".")) +
".doc"),true);
}
private void doOpen(){
FileDialog fileDialog = new FileDialog(clientFrame.getShell());
String docPath = ResourceManager.getString("MOREditorView.DocmentPath");
if(docPath == null || docPath.length()==0)
fileDialog.setFilterPath(docPath);
fileDialog.open();
String path = fileDialog.getFilterPath();
String fileName = "";
if(path.length()>3)
fileName = path+"\\" + fileDialog.getFileName();
else
fileName = path+fileDialog.getFileName();
if(fileName.length() == 0)
return;
if(controlSite != null)
controlSite.dispose();
controlSite = new OleClientSite(clientFrame,SWT.NONE,"Word.Document",new
File(fileName));
controlSite.setBackground(clientFrame.getDisplay().getSystem Color(SWT.COLOR_LIST_BACKGROUND));
controlSite.doVerb(OLE.OLEIVERB_SHOW);
clientFrame.layout();
cmd = fileName;
}
private void doNew(){
if(controlSite != null)
controlSite.dispose();
String fileName = ResourceManager.getString("MOREditorView.Template");
File templateFile = null;
if(fileName != null && (templateFile = new File(fileName)).exists())
controlSite = new
OleClientSite(clientFrame,SWT.NONE,"Word.Document",templateFile);
else
controlSite = new OleClientSite(clientFrame,SWT.NONE,"Word.Document");
controlSite.setBackground(clientFrame.getDisplay().getSystem Color(SWT.COLOR_LIST_BACKGROUND));
controlSite.doVerb(OLE.OLEIVERB_SHOW);
clientFrame.layout();
cmd = null;
}
class Separator implements IAction {
/* (non-Javadoc)
* @see com.mor.ui.IAction#getText()
*/
public String getText() {
return null;
}
/* (non-Javadoc)
* @see com.mor.ui.IAction#getToolTipText()
*/
public String getToolTipText() {
return null;
}
/* (non-Javadoc)
* @see com.mor.ui.IAction#getImage()
*/
public Image getImage() {
return null;
}
/* (non-Javadoc)
* @see com.mor.ui.IAction#getHoverImage()
*/
public Image getHoverImage() {
return null;
}
/* (non-Javadoc)
* @see com.mor.ui.IAction#getDisabledImage()
*/
public Image getDisabledImage() {
return null;
}
/* (non-Javadoc)
* @see com.mor.ui.IAction#getType()
*/
public int getType() {
return 0;
}
/* (non-Javadoc)
* @see com.mor.ui.IAction#getStyle()
*/
public int getStyle() {
return SWT.SEPARATOR;
}
/* (non-Javadoc)
* @see com.mor.ui.IAction#run()
*/
public void run() {
}
}
class OpenDocumentAction implements IAction {
/* (non-Javadoc)
* @see com.mor.ui.IAction#getText()
*/
public String getText() {
return ResourceManager.getString("MOREditorTaskView.OpenActionText ");
}
/* (non-Javadoc)
* @see com.mor.ui.IAction#getToolTipText()
*/
public String getToolTipText() {
return
ResourceManager.getString("MOREditorTaskView.OpenActionToolTipText ");
}
/* (non-Javadoc)
* @see com.mor.ui.IAction#getImage()
*/
public Image getImage() {
return
ResourceManager.getImage(ResourceManager.class,ResourceManag er.getString( "MOREditorTaskView.OpenActionImage"));
}
/* (non-Javadoc)
* @see com.mor.ui.IAction#getHoverImage()
*/
public Image getHoverImage() {
return null;
}
/* (non-Javadoc)
* @see com.mor.ui.IAction#getDisabledImage()
*/
public Image getDisabledImage() {
return null;
}
/* (non-Javadoc)
* @see com.mor.ui.IAction#getType()
*/
public int getType() {
return 0;
}
/* (non-Javadoc)
* @see com.mor.ui.IAction#getStyle()
*/
public int getStyle() {
return SWT.PUSH;
}
/* (non-Javadoc)
* @see com.mor.ui.IAction#run()
*/
public void run() {
doOpen();
}
}
class PrintDocumentAction implements IAction {
/* (non-Javadoc)
* @see com.mor.ui.IAction#getText()
*/
public String getText() {
return ResourceManager.getString("MOREditorTaskView.PrintActionText ");
}
/* (non-Javadoc)
* @see com.mor.ui.IAction#getToolTipText()
*/
public String getToolTipText() {
return
ResourceManager.getString("MOREditorTaskView.PrintActionToolTipText ");
}
/* (non-Javadoc)
* @see com.mor.ui.IAction#getImage()
*/
public Image getImage() {
return
ResourceManager.getImage(ResourceManager.class,ResourceManag er.getString( "MOREditorTaskView.PrintActionImage"));
}
/* (non-Javadoc)
* @see com.mor.ui.IAction#getHoverImage()
*/
public Image getHoverImage() {
return null;
}
/* (non-Javadoc)
* @see com.mor.ui.IAction#getDisabledImage()
*/
public Image getDisabledImage() {
return null;
}
/* (non-Javadoc)
* @see com.mor.ui.IAction#getType()
*/
public int getType() {
return 0;
}
/* (non-Javadoc)
* @see com.mor.ui.IAction#getStyle()
*/
public int getStyle() {
return SWT.PUSH;
}
/* (non-Javadoc)
* @see com.mor.ui.IAction#run()
*/
public void run() {
doPrint();
}
}
class SaveDocumentAction implements IAction {
/* (non-Javadoc)
* @see com.mor.ui.IAction#getText()
*/
public String getText() {
return ResourceManager.getString("MOREditorTaskView.SaveActionText ");
}
/* (non-Javadoc)
* @see com.mor.ui.IAction#getToolTipText()
*/
public String getToolTipText() {
return
ResourceManager.getString("MOREditorTaskView.SaveActionToolTipText ");
}
/* (non-Javadoc)
* @see com.mor.ui.IAction#getImage()
*/
public Image getImage() {
return
ResourceManager.getImage(ResourceManager.class,ResourceManag er.getString( "MOREditorTaskView.SaveActionImage"));
}
/* (non-Javadoc)
* @see com.mor.ui.IAction#getHoverImage()
*/
public Image getHoverImage() {
return null;
}
/* (non-Javadoc)
* @see com.mor.ui.IAction#getDisabledImage()
*/
public Image getDisabledImage() {
return null;
}
/* (non-Javadoc)
* @see com.mor.ui.IAction#getType()
*/
public int getType() {
return 0;
}
/* (non-Javadoc)
* @see com.mor.ui.IAction#getStyle()
*/
public int getStyle() {
return SWT.PUSH;
}
/* (non-Javadoc)
* @see com.mor.ui.IAction#run()
*/
public void run() {
doSave();
}
}
class NewDocumentAction implements IAction {
/* (non-Javadoc)
* @see com.mor.ui.IAction#getText()
*/
public String getText() {
return ResourceManager.getString("MOREditorTaskView.NewActionText");
}
/* (non-Javadoc)
* @see com.mor.ui.IAction#getToolTipText()
*/
public String getToolTipText() {
return
ResourceManager.getString("MOREditorTaskView.NewActionToolTipText ");
}
/* (non-Javadoc)
* @see com.mor.ui.IAction#getImage()
*/
public Image getImage() {
return
ResourceManager.getImage(ResourceManager.class,ResourceManag er.getString( "MOREditorTaskView.NewActionImage"));
}
/* (non-Javadoc)
* @see com.mor.ui.IAction#getHoverImage()
*/
public Image getHoverImage() {
return null;
}
/* (non-Javadoc)
* @see com.mor.ui.IAction#getDisabledImage()
*/
public Image getDisabledImage() {
return null;
}
/* (non-Javadoc)
* @see com.mor.ui.IAction#getType()
*/
public int getType() {
return 0;
}
/* (non-Javadoc)
* @see com.mor.ui.IAction#getStyle()
*/
public int getStyle() {
return SWT.PUSH;
}
/* (non-Javadoc)
* @see com.mor.ui.IAction#run()
*/
public void run() {
doNew();
}
}
}
Kalman wrote:
> I did see this prior to writing my e-mails, but am still not sure how to
> pass my input into the Word Document.
> "Veronika Irvine" <veronika_irvine@oti.com> wrote in message
> news:cmdps3$tua$1@eclipse.org...
>> See:
>>
>
http://www.eclipse.org/articles/Article-ActiveX%20Support%20 in%20SWT/ActiveX%20Support%20in%20SWT.html
>>
>>
>> "Kalman" <kalman@zikal.com> wrote in message
>> news:cmdcuk$tbn$1@eclipse.org...
>> > Hi, group.
>> >
>> > I would like to have the user open a Microsoft word template (which is
>> > pretty easy to do using the FileDialog) and then create a new Word
>> > document
>> > merging the template data and some data coming from my Java application.
> I
>> > heard this is possible using OLE in SWT. Has anyone does this before?
>> >
>> > Thanx,
>> > - Kalman
>> >
>> >
>>
>>
|
|
|
Re: OLE and Microsoft word [message #445409 is a reply to message #445368] |
Fri, 05 November 2004 05:57  |
Eclipse User |
|
|
|
Hi Kalman,
in fact your questions are related with how to use MSWord exposed Objects.
SwtOles gives to you the possibility to use Automation no matter of
application.
For merging, you have to read VBAWD??.CHM file (install it from msoficce
if isn't already on your computer), then you'll understand how you can use
Word Automation(you have to work with MailMerge object).
There are also several (vba) samples you can get about office automation.
Using Com Objects with swtoles is quite clear explained in Eclipse help;
is no so easy as in VBA, but with some effort you get it.
Regards,
Tiberiu
|
|
|
Powered by
FUDForum. Page generated in 0.03703 seconds