1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| public class Dde2Word {
public static void main(String args[]) {
try {
int timeout=5000;
int j;
int format=com.neva.DdeUtil.CF_TEXT;
byte [] repl;
String doc=null;
String [] Topics;
com.neva.DdeClient cli=new com.neva.DdeClient();
//connect to MsWord
cli.connect("WINWORD","System");
//request the list of topics
repl=cli.request("Topics",format,timeout);
Topics=tabparser(new String(repl));
System.out.println("List of topics available:");
for(j=0;j<Topics.length;j++) {
System.out.println(Topics[j]);
if(Topics[j].endsWith(".doc") || Topics[j].endsWith(".DOC"))
doc=Topics[j];
}
//disconnect from MsWord
cli.disconnect();
if(doc==null) {
System.out.println("Document is not available...");
System.exit(0);
}
//connect to the document
cli=new com.neva.DdeClient();
cli.connect("WINWORD",doc);
//retrieve the contents of the entire document
repl=cli.request("\\Doc",format,timeout);
String text=new String(repl);
System.out.println(text);
//Disconnect from the document
cli.disconnect();
} catch(Exception e) {
e.printStackTrace();
System.exit(0);
}
}
static String [] tabparser(String in) {
//parses the tab-delimited list of information
int pos=0;
int cnt=1;
while(true) {
pos=in.indexOf('\t',pos+1);
if(pos==-1)
break;
cnt++;
}
String [] out=new String[cnt];
cnt=0;
pos=0;
while(true) {
int pos2=in.indexOf('\t',pos);
if(pos2==-1) {
out[cnt++]=in.substring(pos);
break;
} else
out[cnt++]=in.substring(pos,pos2);
pos=pos2+1;
}
return out;
}
} |
Partager