import com.jacob.com.*;
import com.jacob.activeX.*;public class Word_show {
public static void main(String[] args) {
String inFile = "d:\\test.doc";//要进行操作的word文档
boolean flag = false;
ActiveXComponent app = new ActiveXComponent("Word.Application");//启动word
try {
app.setProperty("Visible", new Variant(true));//设置word不可见
Object docs = app.getProperty("documents").toDispatch();//获取Documents对象集合
Object doc = Dispatch.invoke(docs,"Open", Dispatch.Method, new Object[]{inFile,new Variant(false), new Variant(false)}, new int[1]).toDispatch();
Object content = Dispatch.get(doc, "Content").toDispatch();//提取word文档内容对象
Object charac=Dispatch.get(content,"Characters").toDispatch();
int charactercount=Dispatch.get(charac,"Count").toInt();//得到该文档的字符总数
System.out.println("本文档共有字符数为:"+charactercount);
Object finder = Dispatch.get(content, "Find").toDispatch();//提取find对象,也就查找替换的那个对象
Variant f = new Variant(false);
Variant t = new Variant(true);
boolean sb=false;
do{
sb = Dispatch.invoke(finder,"Execute", Dispatch.Method, new Object[]{"%单位名称%",f,f,f,f,f,f,f,f,"单位",new Variant(true)}, new int[2]).toBoolean();
}while(sb);
System.out.println(sb);
do{
sb = Dispatch.invoke(finder,"Execute", Dispatch.Method, new Object[]{"%工程名称%",f,f,f,f,f,f,f,f,"adf",new Variant(true)}, new int[2]).toBoolean();
}while(sb);
Dispatch.call(doc,"SaveAs","D:\\1.doc");//保存对文档的修改
flag = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});//退出word应用程序
}
}
}
import com.jacob.activeX.*;public class Word_show {
public static void main(String[] args) {
String inFile = "d:\\test.doc";//要进行操作的word文档
boolean flag = false;
ActiveXComponent app = new ActiveXComponent("Word.Application");//启动word
try {
app.setProperty("Visible", new Variant(true));//设置word不可见
Object docs = app.getProperty("documents").toDispatch();//获取Documents对象集合
Object doc = Dispatch.invoke(docs,"Open", Dispatch.Method, new Object[]{inFile,new Variant(false), new Variant(false)}, new int[1]).toDispatch();
Object content = Dispatch.get(doc, "Content").toDispatch();//提取word文档内容对象
Object charac=Dispatch.get(content,"Characters").toDispatch();
int charactercount=Dispatch.get(charac,"Count").toInt();//得到该文档的字符总数
System.out.println("本文档共有字符数为:"+charactercount);
Object finder = Dispatch.get(content, "Find").toDispatch();//提取find对象,也就查找替换的那个对象
Variant f = new Variant(false);
Variant t = new Variant(true);
boolean sb=false;
do{
sb = Dispatch.invoke(finder,"Execute", Dispatch.Method, new Object[]{"%单位名称%",f,f,f,f,f,f,f,f,"单位",new Variant(true)}, new int[2]).toBoolean();
}while(sb);
System.out.println(sb);
do{
sb = Dispatch.invoke(finder,"Execute", Dispatch.Method, new Object[]{"%工程名称%",f,f,f,f,f,f,f,f,"adf",new Variant(true)}, new int[2]).toBoolean();
}while(sb);
Dispatch.call(doc,"SaveAs","D:\\1.doc");//保存对文档的修改
flag = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});//退出word应用程序
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货