Problem mit Select-Anweisung bei Word OLE

joergTh

Grünschnabel
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:

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:
Moin,

BITTE CODE-TAGS BENUTZEN !!

In welcher Zeile des Code tritt der Fehler denn konkret auf ?

Was mir spontan auffiel :
Java:
if (pVarResult0 == null) OLE.error(OLE.ERROR_APPLICATION_NOT_FOUND);
OleAutomation application = pVarResult0.getAutomation();
Wenn nun pVarResult0 wirklich NULL ist, dann sollte dan nachfolgende pVarResult0.getAutomation() doch wohl auch fehlschlagen, oder ?

Gruß
Klaus
 
Hallo Klaus,

danke für Deine schnelle Reaktion. Wie kann ich Code als solchen kennzeichnen? Sorry, bin neu im Forum.

Der Fehler tritt in folgender Zeile auf:

OleAutomation selectTitle = invokeSelect.getAutomation();

Die Select-Anweisung funktioniert anscheinend nicht.

Gruß Jörg
 
Moin,

JAVA-Code: im erweiterten Editor auf den Button mit der '#' klicken und dann in den Tags ggf. die Begriffe CODE durch JAVA (oder auch CPP etc. je nachdem) ersetzen!

Ich bin jetzt kein wirklicher Experte für OLE-Automation (in Java), aber zu einen sieht es gefährlich aus, wenn Du nach deinen Prüfungen auf NULL trotzdem einfach mit dem Objekt weitermachst !

Wenn Du mal nach dem auftretenden Fehler "Failed to change Variant" googlest, findest Du jede Menge Seiten, die sich damit beschäftigen! Das scheint aber wohl ein Problem mit inkompatioblen (Daten-)Typen zu sein ...

Gruß
Klaus
 
Dank Dir, hab die Code Tags direkt mal angewandt. Werde mal "Failed to change Variant" googeln. Danke für den Tip.

Gruß Jörg
 
Hi Joerg,

Das Problem ist das Ergebnis vom Type VT_EMPTY ist und deswegen kann man keine Automation auf ihn aufrufen.
ich benutze viel OleAutomation um reports zu erstellen. Mein problem ist das manchmal das Ergbenis null kommt, und manch mal funktionert es prima.
Irgendwelche Ideen was man machen muss um das Prozess zuverläsign zu kriegen?
 
Zurück