package test;
import com.jacob.activeX.ActiveXComponent;
 import com.jacob.com.ComThread;
 import com.jacob.com.Dispatch;
 import com.jacob.com.Variant;
/**
  * jacob操作MSword类
  * 
  * @author
  */
 public class WordBeanSimple {
  
  // word文档
  private Dispatch doc;
  // word运行程序对象
  private ActiveXComponent word;
  // 所有word文档集合
  private Dispatch documents;
  // 选定的范围或插入点
  private Dispatch selection;
  private boolean saveOnExit = true;
 public WordBeanSimple() throws Exception {
  
   if (word == null) {
  
    word = new ActiveXComponent("Word.Application");
    word.setProperty("Visible", new Variant(false)); // 不可见打开word
    word.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏
   }
   if (documents == null)
    documents = word.getProperty("Documents").toDispatch();
  }
 /**
   * 设置退出时参数
   * 
   * @param saveOnExit
   *            boolean true-退出时保存文件,false-退出时不保存文件
   */
  public void setSaveOnExit(boolean saveOnExit) {
  
   this.saveOnExit = saveOnExit;
  }
 /**
   * 创建一个新的word文档
   * 
   */
  public void createNewDocument() {
  
   doc = Dispatch.call(documents, "Add").toDispatch();
   selection = Dispatch.get(word, "Selection").toDispatch();
  }
 /**
   * 打开一个已存在的文档
   * 
   * @param docPath
   */
  public void openDocument(String docPath) {
  
   doc = Dispatch.call(documents, "Open", docPath).toDispatch();
   selection = Dispatch.get(word, "Selection").toDispatch();
  }
 /**
   * 把插入点移动到文件首位置
   * 
   */
  public void moveStart() {
  
   if (selection == null)
    selection = Dispatch.get(word, "Selection").toDispatch();
   Dispatch.call(selection, "HomeKey", new Variant(6));
  }
 /**
   * 从选定内容或插入点开始查找文本
   * 
   * @param toFindText
   *            要查找的文本
   * @return boolean true-查找到并选中该文本,false-未查找到文本
   */
  @SuppressWarnings("static-access")
  public boolean find(String toFindText) {
  
   if (toFindText == null || toFindText.equals(""))
    return false;
   // 从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");
   // 查找并选中
   return Dispatch.call(find, "Execute").getBoolean();
  }
 /**
   * 把选定选定内容设定为替换文本
   * 
   * @param toFindText
   *            查找字符串
   * @param newText
   *            要替换的内容
   * @return
   */
  public boolean replaceText(String toFindText, String newText) {
  
   this.moveStart();
   if (!find(toFindText))
    return false;
   Dispatch.put(selection, "Text", newText);
   return true;
  }
 /**
   * 文件保存或另存为
   * 
   * @param savePath
   *            保存或另存为路径
   */
  public void save(String savePath) {
  
   Dispatch.call(Dispatch.call(word, "WordBasic").getDispatch(),
     "FileSaveAs", savePath);
  }
 /**
   * 关闭全部应用
   * 
   */
  public void close() {
  
   // closeDocument();
   if (word != null) {
  
    Dispatch.call(word, "Quit");
    word = null;
   }
   selection = null;
   documents = null;
  }
public void openWord(boolean makeVisible) {
  // 启动com的线程
   ComThread.InitSTA();
   // 打開word(如果word未開啟時)
   if (word == null) {
  
    word = new ActiveXComponent("Word.Application");
   }
  // 設置word是可見或不可見(true:顯示;false:不顯示)
   Dispatch.put(word, "Visible", new Variant(makeVisible));
  }
 public static void main(String[] args) throws Exception {
  
   WordBeanSimple word2 = new WordBeanSimple();
   String templetPath = "d:\\ssss.doc"; // 模版文件
   String otPath = "d:\\11122.doc"; // 保存文件
   try {
  
    // 是否显示打开word
    word2.openWord(false);
    // 打开模版文件
    word2.openDocument(templetPath);
    word2.replaceText("$AGE134567$", "18");
    word2.replaceText("$NAME$", "YOUANDME");
    
    
    
    
   // 替换书签内容
    // word2.replaceBookMark("Prj_Name", "项目");
    // 保存到path
    word2.save(otPath);
  } catch (Exception ex) {
  
    ex.printStackTrace();
   } finally {
  
    // 关闭Word
    word2.close();
   }
 }
 }