importcom.jacob.activeX.ActiveXComponent;importcom.jacob.com.ComThread;importcom.jacob.com.Dispatch;importcom.jacob.com.Variant;/***
*
*@authorBruceLeey
**/publicclassMSWordManager {//word文档privateDispatch doc;//word运行程序对象privateActiveXComponent word;//所有word文档集合privateDispatch documents;//选定的范围或插入点privateDispatch selection;privatebooleansaveOnExit=true;publicMSWordManager() {
ComThread.InitSTA();if(word==null) {
word=newActiveXComponent("Word.Application");
word.setProperty("Visible",newVariant(false));
}if(documents==null)
documents=word.getProperty("Documents").toDispatch();
}/*** 设置退出时参数
*
*@paramsaveOnExit
* boolean true-退出时保存文件,false-退出时不保存文件*/publicvoidsetSaveOnExit(booleansaveOnExit) {this.saveOnExit=saveOnExit;
}/*** 创建一个新的word文档
**/publicvoidcreateNewDocument() {
doc=Dispatch.call(documents,"Add").toDispatch();
selection=Dispatch.get(word,"Selection").toDispatch();
}/*** 打开一个已存在的文档
*
*@paramdocPath*/publicvoidopenDocument(String docPath) {
closeDocument();
doc=Dispatch.call(documents,"Open", docPath).toDispatch();
selection=Dispatch.get(word,"Selection").toDispatch();
}/*** 把选定的内容或插入点向上移动
*
*@parampos
* 移动的距离*/publicvoidmoveUp(intpos) {if(selection==null)
selection=Dispatch.get(word,"Selection").toDispatch();for(inti=0; i
Dispatch.call(selection,"MoveUp");
}/*** 把选定的内容或者插入点向下移动
*
*@parampos
* 移动的距离*/publicvoidmoveDown(intpos) {if(selection==null)
selection=Dispatch.get(word,"Selection").toDispatch();for(inti=0; i
Dispatch.call(selection,"MoveDown");
}/*** 把选定的内容或者插入点向左移动
*
*@parampos
* 移动的距离*/publicvoidmoveLeft(intpos) {if(selection==null)
selection=Dispatch.get(word,"Selection").toDispatch();for(inti=0; i
Dispatch.call(selection,"MoveLeft");
}
}/*** 把选定的内容或者插入点向右移动
*
*@parampos
* 移动的距离*/publicvoidmoveRight(intpos) {if(selection==null)
selection=Dispatch.get(word,"Selection").toDispatch();for(inti=0; i
Dispatch.call(selection,"MoveRight");
}/*** 把插入点移动到文件首位置
**/publicvoidmoveStart() {if(selection==null)
selection=Dispatch.get(word,"Selection").toDispatch();
Dispatch.call(selection,"HomeKey",newVariant(6));
}/*** 从选定内容或插入点开始查找文本
*
*@paramtoFindText
* 要查找的文本
*@returnboolean true-查找到并选中该文本,false-未查找到文本*/publicbooleanfind(String toFindText) {if(toFindText==null||toFindText.equals(""))returnfalse;//从selection所在位置开始查询Dispatch find=word.call(selection,"Find").toDispatch();//设置要查找的内容Dispatch.put(find,"Text", toFindText);//向前查找Dispatch.put(find,"Forward","True");//设置格式Dispatch.put(find,"Format","True");//大小写匹配Dispatch.put(find,"MatchCase","True");//全字匹配Dispatch.put(find,"MatchWholeWord","True");//查找并选中returnDispatch.call(find,"Execute").getBoolean();
}/*** 把选定选定内容设定为替换文本
*
*@paramtoFindText
* 查找字符串
*@paramnewText
* 要替换的内容
*@return*/publicbooleanreplaceText(String toFindText, String newText) {if(!find(toFindText))returnfalse;
Dispatch.put(selection,"Text", newText);returntrue;
}/*** 全局替换文本
*
*@paramtoFindText
* 查找字符串
*@paramnewText
* 要替换的内容*/publicvoidreplaceAllText(String toFindText, String newText) {while(find(toFindText)) {
Dispatch.put(selection,"Text", newText);
Dispatch.call(selection,"MoveRight");
}
}/*** 在当前插入点插入字符串
*