ActiveXComponent app = new ActiveXComponent("Word.Application"); //启动word
String inFile = "C:\\TEMP\\test.doc"; 

app.setProperty("Visible", new Variant(false)); //设置word不可见
Dispatch docs = app.getProperty("Documents").toDispatch();
Dispatch doc = Dispatch.invoke(docs,"Open",Dispatch.Method,
new Object[] { inFile, new Variant(false),new Variant(false) }, new int[1]).toDispatch();

/*方法1: 通过查找文本取页码*/
Dispatch selection = app.getProperty("Selection").toDispatch();//获得对Selection组件
Dispatch.call(selection, "HomeKey", new Variant(6));//移到开头
Dispatch find = Dispatch.call(selection, "Find").toDispatch();//获得Find组件
Dispatch.put(find, "Text", "二、成果简介"); //查找字符串"二、成果简介"
Dispatch.call(find, "Execute"); //执行查询

 String pages = Dispatch.call(selection, "Information",new Variant(3)).toString();

System.out.println("文本所在页码:"+startpages);

 

/*方法2: 通过查找书签取页码*/
Dispatch bookMarks=Dispatch.call(doc,"Bookmarks").toDispatch();
boolean bookMarkExist=Dispatch.call(bookMarks,"Exists","mark3").toBoolean();
if(bookMarkExist){
Dispatch rangeItem=Dispatch.call(bookMarks,"Item","mark3").toDispatch();
Dispatch range=Dispatch.call(rangeItem,"Range").toDispatch();
String markpage = Dispatch.call(range, "Information",new Variant(3)).toString();
System.out.println("bookmark page:"+endpages);
}