Hallo!
Irgendwas mache ich falsch. :-(
Ich habe in meine SWT-Applikation MS-Word engebettet und möchte gleich beim Start der Anwendung eine Datei öffnen. Ich übergebe den Pfad zur Datei, die geöffnet werden soll, es scheint jedoch etwas falsch zu laufen. Der Fehler tritt meines Wissens nach beim Aufruf der invoke-Methode auf. Und zwar in der folgenden Zeile:
Damit ihr es nachvollziehen könnt, hier der komplette Quellcode.
THX im Voraus!
PS: Ich verwende OleControlSite statt OleClientSite. Bei der OleClientSite kann man dem Konstruktor als Parameter zusätzlich eine Datei mitgeben, die sofort geöffnet wird. Der OleClientSite kann man aber keinen EventListener adden. Dies ist der Grund, warum ich OleControlSite verwende, was mir das Leben etwas schwerer macht.
Irgendwas mache ich falsch. :-(
Ich habe in meine SWT-Applikation MS-Word engebettet und möchte gleich beim Start der Anwendung eine Datei öffnen. Ich übergebe den Pfad zur Datei, die geöffnet werden soll, es scheint jedoch etwas falsch zu laufen. Der Fehler tritt meines Wissens nach beim Aufruf der invoke-Methode auf. Und zwar in der folgenden Zeile:
Code:
Variant pVarResult = fDocumentsAutomation.invoke(dispIdMember, rgvarg, rgdispidNamedArgs);
Damit ihr es nachvollziehen könnt, hier der komplette Quellcode.
THX im Voraus!
PS: Ich verwende OleControlSite statt OleClientSite. Bei der OleClientSite kann man dem Konstruktor als Parameter zusätzlich eine Datei mitgeben, die sofort geöffnet wird. Der OleClientSite kann man aber keinen EventListener adden. Dies ist der Grund, warum ich OleControlSite verwende, was mir das Leben etwas schwerer macht.
Code:
import java.io.File;
import org.eclipse.swt.SWT;
import org.eclipse.swt.ole.win32.OLE;
import org.eclipse.swt.ole.win32.OleAutomation;
import org.eclipse.swt.ole.win32.OleControlSite;
import org.eclipse.swt.ole.win32.OleFrame;
import org.eclipse.swt.ole.win32.Variant;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class WordOpenFileTest
{
/**
* File has to be opened.
*/
private File file = new File("c:\\test.rtf");
private OleAutomation fWordAutomation;
private OleAutomation fDocumentsAutomation;
/**
* Constructor.
*/
public WordOpenFileTest()
{
Display display = new Display();
Shell shell = new Shell(display);
shell.open();
OleFrame frame = new OleFrame(shell, SWT.NONE);
OleControlSite site = new OleControlSite(frame, SWT.NONE, "Word.Document");
fWordAutomation = new OleAutomation(site);
site.doVerb(OLE.OLEIVERB_SHOW);
site.setVisible(true);
int openResult = openFile(file.getAbsolutePath());
System.out.println("Open return value: " + openResult);
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
}
private int openFile(String pFile)
{
// Type Lib method: Open
// [id(0x00000013), helpcontext(0x096c0013)]
// Document* Open(
// [in] VARIANT* FileName,
// [in, optional] VARIANT* ConfirmConversions,
// [in, optional] VARIANT* ReadOnly,
// [in, optional] VARIANT* AddToRecentFiles,
// [in, optional] VARIANT* PasswordDocument,
// [in, optional] VARIANT* PasswordTemplate,
// [in, optional] VARIANT* Revert,
// [in, optional] VARIANT* WritePasswordDocument,
// [in, optional] VARIANT* WritePasswordTemplate,
// [in, optional] VARIANT* Format,
// [in, optional] VARIANT* Encoding,
// [in, optional] VARIANT* Visible,
// [in, optional] VARIANT* OpenAndRepair,
// [in, optional] VARIANT* DocumentDirection,
// [in, optional] VARIANT* NoEncodingDialog,
// [in, optional] VARIANT* XMLTransform);
// names of the Method ant the first parameter
String [] names = { "Open", "FileName" };
// get the application id
int appId = fWordAutomation.getIDsOfNames(new String[] { "Application" })[0];
OleAutomation application = fWordAutomation.getProperty(appId).getAutomation();
// get the id to access the docuemts functions
int documentsId = application.getIDsOfNames(new String[] { "Documents" })[0];
Variant docVariant = application.getProperty(documentsId);
fDocumentsAutomation = docVariant.getAutomation();
int [] rgdispid = fDocumentsAutomation.getIDsOfNames(names);
docVariant.dispose();
// function id
int dispIdMember = rgdispid[0];
// Set the parameter
Variant [] rgvarg = new Variant[1];
rgvarg[0] = new Variant(pFile);
// id of the argument name
int [] rgdispidNamedArgs = new int[1];
rgdispidNamedArgs[0] = rgdispid[1];
Variant pVarResult = fDocumentsAutomation.invoke(dispIdMember, rgvarg, rgdispidNamedArgs);
rgdispid.getClass();
if (pVarResult == null)
{
return 0;
}
return pVarResult.getInt();
}
public static void main(String [] args)
{
new WordOpenFileTest();
}
}