Hallo, ich möchte aus einer JAVA-Anwendung Word per OLE steuern. Zwei Probleme kann ich leider nicht lösen:
1. Der Office-Button in Word erscheint nicht. Ich arbeite mit Word 2007. Gibt es einen OLE-Befehl, mit dem ich den Office-Button sichtbar machen / aktivieren kann?
2. Ich möchte aus meinem Programm heraus ein Word-Dokument aus einer Vorlage erstellen und in das Dokument an bestimmte Stellen Text aus meinem Programm übernehmen, z.B. den Titel des Dokumentes. Ich habe dafür in die Vorlage eine Textmarke eingefügt. Das Programm findet die Textmarke. Wenn ich über die OLE-Anweisung "Select" die Textmarke auswähle, kommt eine Fehlermeldung:
Exception in thread "main" org.eclipse.swt.SWTException: Failed to change Variant type result = -1
at org.eclipse.swt.ole.win32.OLE.error(OLE.java:332)
at org.eclipse.swt.ole.win32.Variant.getAutomation(Variant.java:274)
at q5.wordSupport.WordSupport.createNewDocument(WordSupport.java:100)
Nachfolgend mein Programmcode:
VT_BOOL{true} Ausgabe von System.out.println (invokeBookmarkExists);
VT_EMPTY Ausgabe von System.out.println (invokeSelect);
1. Der Office-Button in Word erscheint nicht. Ich arbeite mit Word 2007. Gibt es einen OLE-Befehl, mit dem ich den Office-Button sichtbar machen / aktivieren kann?
2. Ich möchte aus meinem Programm heraus ein Word-Dokument aus einer Vorlage erstellen und in das Dokument an bestimmte Stellen Text aus meinem Programm übernehmen, z.B. den Titel des Dokumentes. Ich habe dafür in die Vorlage eine Textmarke eingefügt. Das Programm findet die Textmarke. Wenn ich über die OLE-Anweisung "Select" die Textmarke auswähle, kommt eine Fehlermeldung:
Exception in thread "main" org.eclipse.swt.SWTException: Failed to change Variant type result = -1
at org.eclipse.swt.ole.win32.OLE.error(OLE.java:332)
at org.eclipse.swt.ole.win32.Variant.getAutomation(Variant.java:274)
at q5.wordSupport.WordSupport.createNewDocument(WordSupport.java:100)
Nachfolgend mein Programmcode:
Java:
public class WordSupport
{
public static void createNewDocument(String path, String vorlage)
{
Shell wordShell = new Shell();
wordShell.setMaximized(true);
FillLayout layout = new FillLayout();
wordShell.setLayout(layout);
OleFrame oleFrame = new OleFrame(wordShell, SWT.NULL);
OleClientSite clientSite = new OleClientSite(oleFrame, SWT.NONE, new File("meineVorlage mit Pfad"));
clientSite.doVerb(OLE.OLEIVERB_OPEN);
oleFrame.setVisible(true);
wordShell.setVisible(true);
OleAutomation docInterface = new OleAutomation(clientSite);
int[] applId = docInterface.getIDsOfNames(new String[]{"Application"});
if (applId == null) OLE.error(OLE.ERROR_APPLICATION_NOT_FOUND);
Variant pVarResult0 = docInterface.getProperty(applId[0]);
if (pVarResult0 == null) OLE.error(OLE.ERROR_APPLICATION_NOT_FOUND);
OleAutomation application = pVarResult0.getAutomation();
int[] documentId = application.getIDsOfNames(new String[]{"ActiveDocument"});
if (documentId == null) OLE.error(OLE.ERROR_APPLICATION_NOT_FOUND);
Variant pVarResult1 = application.getProperty(documentId[0]);
if (pVarResult1 == null) OLE.error(OLE.ERROR_APPLICATION_NOT_FOUND);
OleAutomation document = pVarResult1.getAutomation();
int[] saveAsId = document.getIDsOfNames(new String[]{"SaveAs","FileName","FileFormat"});
if (saveAsId == null) OLE.error(OLE.ERROR_APPLICATION_NOT_FOUND);
int[] saveAsIdNamedArgs = new int[] {saveAsId[1],saveAsId[2]};
Variant[] rgvar = new Variant[2];
rgvar[0] = new Variant(path);
rgvar[1] = new Variant (0); // wdFormatDocument = 0
Variant pVarResult2 = document.invoke(saveAsId[0],rgvar,saveAsIdNamedArgs);
int[] bookmarksId = document.getIDsOfNames(new String[]{"Bookmarks"});
if (bookmarksId == null) OLE.error(OLE.ERROR_APPLICATION_NOT_FOUND);
Variant pVarResult3 = document.getProperty(bookmarksId[0]);
if (pVarResult3 == null) OLE.error(OLE.ERROR_APPLICATION_NOT_FOUND);
OleAutomation bookmarks = pVarResult3.getAutomation();
int[] itemId = bookmarks.getIDsOfNames(new String[]{"Item","Index"});
if (itemId == null) OLE.error(OLE.ERROR_APPLICATION_NOT_FOUND);
int[] itemNamedArgs = new int[] {itemId[1]};
Variant[] varItem = new Variant[1];
varItem[0] = new Variant("Titel");
Variant invokeBookmarkItem = bookmarks.invoke(itemId[0],varItem,itemNamedArgs);
OleAutomation bookmarkTitle = invokeBookmarkItem.getAutomation();
int[] bookmarkExistsId = bookmarks.getIDsOfNames(new String[]{"Exists","Name"});
if (bookmarkExistsId == null) OLE.error(OLE.ERROR_APPLICATION_NOT_FOUND);
int[] bookmarkExistsNamedArgs = new int[] {bookmarkExistsId[1]};
Variant[] varbookmarkExists = new Variant[1];
varbookmarkExists[0] = new Variant("Titel");
Variant invokeBookmarkExists = bookmarks.invoke(bookmarkExistsId[0],varbookmarkExists,bookmarkExistsNamedArgs);
System.out.println (invokeBookmarkExists);
int[] selectId = bookmarkTitle.getIDsOfNames(new String[]{"Select"});
if (selectId == null) OLE.error(OLE.ERROR_APPLICATION_NOT_FOUND);
Variant invokeSelect = bookmarkTitle.invoke(selectId[0]);
System.out.println (invokeSelect);
OleAutomation selectTitle = invokeSelect.getAutomation();
int[] typeTextId = selectTitle.getIDsOfNames(new String[]{"TypeText","Text"});
if (typeTextId == null) OLE.error(OLE.ERROR_APPLICATION_NOT_FOUND);
int[] typeTextNamedArgs = new int[] {typeTextId[1]};
Variant[] typeTextVariant = new Variant[1];
typeTextVariant[0] = new Variant("Mustertitel");
Variant invokeTypeText = selectTitle.invoke(typeTextId[0],typeTextVariant,typeTextNamedArgs);
}
VT_BOOL{true} Ausgabe von System.out.println (invokeBookmarkExists);
VT_EMPTY Ausgabe von System.out.println (invokeSelect);
Zuletzt bearbeitet: