import msword.*; import ezjcom.*; public class RetrieveTextSample { public static void main( String[] args ) { _Application app = null; try { // Launch MS Word (invisibly), // Open file C:\EZJWordSample1.doc, // Retrieve all characters as text, output to console. // Exit Word. // // When running invisibly, we Quit the application in // a "finally" clause, so invisible applications are // not left around in case of exceptions etc. Application msword = new Application(); app = msword.get_Application(); // app.setVisible( true ); Document doc = app.getDocuments().Open( new JComVariant( "C:\\EZJWordSample1.doc" )); // Open existing document // Retrieve paragraphs. (Characters can also be retrieved.) Paragraphs ps = doc.get_Document().getParagraphs(); int count = ps.getCount(); for ( int i = 1; i <= count; i++ ) { Paragraph p = ps.Item( i ); System.out.println( p.getRange().getText()); System.out.println(); } // app.Quit(); // Quit is moved to a "finally" clause to handle situations with exceptions etc. } catch (Exception ex) { ex.printStackTrace(); } finally { if ( app != null ) try { app.Quit(); } catch (Exception e) {} } } }