OO-Snippets: Inserting a Document in an other

Commons

Keywordsinsert, document
LanguageJava
ApplicationWriter
AuthorsTobias Krais (initial)
Supported Versions2.0.x  
Supported OSAll  
QuestionHow to insert a document in an other one?
Answer

Code-Snippet-Listing (snippet-source)

import com.sun.star.document.XDocumentInsertable;
import com.sun.star.lang.XComponent;
import com.sun.star.text.XText;
import com.sun.star.text.XTextCursor;
import com.sun.star.text.XTextDocument;
import com.sun.star.uno.UnoRuntime;

/**
 * Inserts a document a the cursors point.
 * @param unoDocumentURL File name that is UNO URL conform.
 * How to create this, see ../Office/Office.CreateUNOCompatibleURL.snip
 */
public void insertDocument(String unoDocumentURL) {
    // How to get the XComponent, see ../Office/Office.OpenDocumentFromURL.snip
    XTextDocument xTextDocument = (XTextDocument)
	UnoRuntime.queryInterface(XTextDocument.class, xComponent);
    XText xText = xTextDocument.getText();
        
    // create a text cursor from the cells XText interface
    XTextCursor xTextCursor = xText.createTextCursor();
    XDocumentInsertable xDocInsert = (XDocumentInsertable)
        UnoRuntime.queryInterface(XDocumentInsertable.class, xTextCursor);
    try {
        xDocInsert.insertDocumentFromURL(unoDocumentURL, null);
    }
    catch (Exception e) {
        if (debug > 1)
            e.printStackTrace();
    }
}

Changelog

DateUserModification
2006-11-30tobiaskraisInitial version

and