Mit JACOB Textmarken von Word-Vorlage füllen

stochi

Grünschnabel
Hi,
ich habe mir die JACOB Libs runtergeladen und eingebunden. Funktioniert alles super, jedoch bin ich nicht gerade der VB Experte.

Es geht darum Datenbestände aus dem Java (mit DB-Anbindung) in eine Wordvorlage zu speichern, und dazu verwende ich die Textmarken von Word.
Habe früher einfach die WordAPI genutzt, jedoch funktioniert diese nicht mehr bzw. nicht immer. Außerdem gefällt mit die Kommunikation der WordAPI nicht (von Java -> Textdatei -> WordAPI.exe -> Word)

Mein Ziel ist es eine IN-Code WordAPI mit den selben Funktionen wie die WordAPI zu schreiben. Mein Problem ist die Übersetzung von VB (besser: Word Macros) in Java (besser: JACOB).

Code:
package mywordapi;

import com.jacob.com.*;
import com.jacob.activeX.*;

/*
 * Dieses Programm öffnet eine Word-Vorlage und speichert 
 * diese unter einem andern Namen wieder ab.
 * 
 * Ziel ist es, dass enthaltene Textmarken befüllt werden.
 */

public final class WordProcessing
{

    static String temp_dir = "C:\\WordTemplates\\";     //Pfad für Vorlage und Speichern
    static String temp_file = "Template.dot";           
    static String newfile = "MyDocument.doc";          
        
    public static void searchWordFile()
    {
        ActiveXComponent wordApp = new ActiveXComponent("Word.Application");
        wordApp.setProperty("Visible", new Variant(true));
        Object documents = wordApp.getProperty("Documents").toDispatch();
        Object document = Dispatch.call((Dispatch) documents, "Add", temp_dir + temp_file).toDispatch();
        
        /*
         *  Vorlage von VB-Macro:
         *  =====================
         * 
         *  Hier wird eine Textmarke "ArztPLZOrt" gesucht und anschließend an dessen
         *  Stelle ein Text geschrieben.
         *  
         *  -----------
         *  Selection.GoTo What:=wdGoToBookmark, Name:="ArztPLZOrt"
         *  Selection.TypeText Text:="TEST"
         *  -----------
         *
         *  Mein fehlgeschlagener Versuch:
         *  ==============================
         *  
         *  Object Selection = wordApp.getProperty("Selection").toDispatch();
         *  Selection = Dispatch.call((Dispatch) Selection, "Goto",new Variant("What:=wdGoToBookmark"),new Variant("Name:=\"ArztPLZOrt\")"));
         *  Dispatch.call((Dispatch) Selection, "TypeText","Text:=\"TEST\"");
         *
        */
        
        Dispatch.call((Dispatch) document, "SaveAs", new Variant("\"" + temp_dir + newfile + "\""));
    }
}

Habe schon verzweifelt nach anspruchsvollern Tutorials für JACOB gesucht, jedoch ohne Erfolg. Bin für jegliche Art von Hilfe offen.

Danke schon mal im Vorraus.
 
Textmarken sind in Word als Bookmarks erfasst. Befüllen musste ich sie zwar noch nie, jedoch bekommst du über folgenden Source zumindest mal an die Objekte:

Java:
/**
	 * Suche nach Bookmarks
	 * @param application Application-Objekt
	 * @return Vector
	 */
	private Vector<String> getBookmarks(Dispatch application) {
		Vector<String> retval = new Vector<String>();
		try {
			Dispatch document = Dispatch.get(application, "ActiveDocument").toDispatch();
			Dispatch bookmarks = Dispatch.get(document, "Bookmarks").toDispatch();
			int bkmcnt = Dispatch.get(bookmarks, "Count").getInt();
	        for (int i = 0; i < bkmcnt; i++) {
	              Dispatch bkm = Dispatch.call(bookmarks, "Item", new Variant(i + 1)).toDispatch();
	              Variant res = Dispatch.get(bkm, "Name");
	              retval.add(res.getString());
	        }
		} catch (ComFailException e) {
			
		}
		return retval;
	}

Über den entsprechenden Dispatch müsste die Befüllung dann eigendlich klappen.

Gruss, Manuel
 
Danke vielmals für deine Hilfe.

Hier die funktionierende Source.

Code:
public final class ASAWordProcessing
{
    
    static Properties bookmarks = new Properties();
    
    public static void setBookmark(String bookmark, String value)
    {
        bookmarks.setProperty(bookmark,value);
    }
    
    public static void setBookmarksEmpty()
    {
        bookmarks.clear();
    }
    
    
    
    public static void createWordFileFromTemplateShow(String template, String newFile)
    {
        ComThread.InitSTA();
        ActiveXComponent com_wordApp = new ActiveXComponent("Word.Application");
        com_wordApp.setProperty("Visible", new Variant(true));
        Object com_documents = com_wordApp.getProperty("Documents").toDispatch();
        Object com_document = Dispatch.call((Dispatch) com_documents, "Add", template).toDispatch();
        
        try
        {
            Dispatch com_bookmarks = Dispatch.get((Dispatch) com_document, "Bookmarks").toDispatch();
            int bkmcnt = Dispatch.get(com_bookmarks, "Count").getInt();
            for (int i = 0; i < bkmcnt; i++)
            {
                Dispatch com_bkm = Dispatch.call(com_bookmarks, "Item", new Variant(i + 1)).toDispatch();
                Variant com_res = Dispatch.get(com_bkm, "Name");
                String com_bookmark_text = bookmarks.getProperty(com_res.getString());
                
                if(com_bookmark_text != null)
                {
                    Dispatch.put(com_bkm, "Range", new Variant(com_bookmark_text));
                }
                
            }
            Dispatch.call((Dispatch) com_document, "SaveAs", new Variant("\"" + newFile + "\""));
        }
        catch (ComFailException e)
        {
            System.out.println(e);
        }
        finally
        {
        ComThread.Release(); 
        }
    }
    
    public static void createWordFileFromTemplateClose(String template, String newFile)
    {
        ComThread.InitSTA();
        ActiveXComponent com_wordApp = new ActiveXComponent("Word.Application");
        com_wordApp.setProperty("Visible", new Variant(true));
        Object com_documents = com_wordApp.getProperty("Documents").toDispatch();
        Object com_document = Dispatch.call((Dispatch) com_documents, "Add", template).toDispatch();
        
        try
        {
            Dispatch com_bookmarks = Dispatch.get((Dispatch) com_document, "Bookmarks").toDispatch();
            int bkmcnt = Dispatch.get(com_bookmarks, "Count").getInt();
            for (int i = 0; i < bkmcnt; i++)
            {
                Dispatch com_bkm = Dispatch.call(com_bookmarks, "Item", new Variant(i + 1)).toDispatch();
                Variant com_res = Dispatch.get(com_bkm, "Name");
                String com_bookmark_text = bookmarks.getProperty(com_res.getString());
                
                if(com_bookmark_text != null)
                {
                    Dispatch.put(com_bkm, "Range", new Variant(com_bookmark_text));
                }
                
            }
            Dispatch.call((Dispatch) com_document, "SaveAs", new Variant("\"" + newFile + "\""));
            Dispatch.call((Dispatch) com_document, "Close", new Variant(false));
            com_wordApp.invoke("Quit", new Variant[] {});
        }
        catch (ComFailException e)
        {
            System.out.println(e);
        }
        finally
        {
        
        ComThread.Release(); 
        }
    }
    
}
 
Hallo Leute!

Ich bin zufällig auf diesen beitrag gestoßen. Es geht eigentlich um was ganz einfaches, aber ich konnte es bisher nicht lösen.

Und zwar möchte ich ein Word Dokument als Txt datei abspeichern.
In VB was ich aus der Makroaufzeichung aus Word habe, sieht es so aus

Code:
ActiveDocument.SaveAs FileName:="file_in.txt", FileFormat:=wdFormatText, _

In moment sieht es noch bei mir so aus.
Code:
Dispatch.call(oWordBasic, "FileSaveAs" ,output)

Ich komme da einfach nicht weiter habe schon einiges ausprobiert.
Hoffe es kann mir jemand weiter helfen.

Gruß Sieo
 
Hallo Zusammen,

ich müsste für eine Java-Webanwendung ein Word-Dokument mit Daten aus einer Datenbank füllen.
Hierfür wollte ich Bookmarks verwenden. Dazu habe ich Stochi's SourceCode verwendet, aber leider bekomme ich folgende Exception:

Code:
Exception in thread "main" com.jacob.com.ComFailException: Invoke of: Range
Source: Microsoft Word
Description: Der Bereich kann nicht gelöscht werden

Hier ein Auszug aus meinem SourceCode:

Code:
public void erstelleWordbrief(final String vorlage, final String strInputDoc) {

        final ActiveXComponent wordApp = new ActiveXComponent("Word.Application");
        wordApp.setProperty("Visible", new Variant(true));
        final Dispatch documents = wordApp.getProperty("Documents").toDispatch();
        final Dispatch document = Dispatch.call(documents, "Add", vorlage).toDispatch();

        final Dispatch bkm = Dispatch.get(document, "Bookmarks").toDispatch();
        final int bkmCount = Dispatch.get(bkm, "Count").getInt();
        for (int i = 0; i < bkmCount; i++) {
            final Dispatch bkmItem = Dispatch.call(bkm, "Item", new Variant(i + 1)).toDispatch();
            final Variant bkmName = Dispatch.get(bkmItem, "Name");
            final String bkmText = Serienbrief.bookmarks.getProperty(bkmName.getString());

            if (bkmText != null) {
                Dispatch.put(bkmItem, "Range", new Variant(bkmText));
            }
        }
        Dispatch.call(document, "SaveAs", new Variant("\"" + strInputDoc + "\""));
        Dispatch.call(document, "Open", strInputDoc).toDispatch();
    }

Weiß einer von Euch, was dieser Fehler zu bedeuten hat? Ich arbeite zum ersten Mal mit JaCOB.

Danke.
 
Zurück