Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Word in SWT : Application.Documents.Open fails.
Word in SWT : Application.Documents.Open fails. [message #461464] Sat, 24 September 2005 12:28
Eclipse UserFriend
Originally posted by: omry_y.inter.net.il

Hello.
I am attempting to use MS-Word OLE Control from within SWT OLE Bridge.
Initialy I would simply like to open a document programatically.
something equevelent to
Application.Documents.Open "Filename" in VBA

Basicaly the following Java code gets a reference to "Application", from
there there a reference to Documents, and than attempt to call
Open with one argument, "FileName" = "c:\1.doc"

This fails with the error:
--- The Open method or property is not available because this document
is being edited in another application.
org.eclipse.swt.SWTException: Action can not be performed. result =
-2147352567

I have discovered than even if I call the method with a file which does
not exist I get the same error.

help would be appriciated.


here is a snippet:


package testing.misc;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
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.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class WordSWT
{
private static OleClientSite _clientSite;

/**
* @param args
*/
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setBounds(100, 100, 500, 500);
shell.setLayout(new FillLayout());
Composite c = new Composite(shell, SWT.NONE);
c.setLayout(new GridLayout());

OleFrame frame = new OleFrame(c, SWT.NONE);
frame.setLayoutData(new GridData(GridData.FILL_BOTH));
_clientSite = new OleClientSite(frame, SWT.NONE, "Word.Document");
_clientSite.doVerb(OLE.OLEIVERB_PRIMARY);
Button b = new Button(c, SWT.NONE);
b.setText("Open");
b.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
b.addSelectionListener(new SelectionAdapter()
{
public void widgetSelected(SelectionEvent e)
{
openFile();
}
});

shell.setVisible(true);

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

public static void openFile()
{
OleAutomation 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);
OleAutomation application = pVarResult.getAutomation();

int[] docsId = application.getIDsOfNames(new String[] {
"Documents" });
if (docsId == null)
OLE.error(OLE.ERROR_APPLICATION_NOT_FOUND);
pVarResult = application.getProperty(docsId[0]);
if (pVarResult == null)
OLE.error(OLE.ERROR_APPLICATION_NOT_FOUND);

OleAutomation documents = pVarResult.getAutomation();

int[] rgdispid = documents.getIDsOfNames(new String[] { "Open",
"FileName" });

Variant[] rgvarg = new Variant[] { new Variant("c:\\1.doc"), };
int nameArgs[] = new int[] { rgdispid[1] };

try
{
documents.invokeNoReply(rgdispid[0], rgvarg, nameArgs);
}
catch (Exception e1)
{
System.out.println("--- " + documents.getLastError());
e1.printStackTrace();
}
}
}
Previous Topic:Combo selection bug?
Next Topic:AWT.requestFocus deadlocks with SWT.EventQueue.invokeAndWait
Goto Forum:
  


Current Time: Sat Oct 21 12:28:17 GMT 2017

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

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