package Beans;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class WordOperate {
private ActiveXComponent wordApp = null;
private Dispatch docs = null;
private Dispatch selection = null;
private Dispatch align = null;
private Dispatch font = null;
private Dispatch document = null;
private String tpFile =null;
private String SpecifyContent = null;
private Variant userName = null;
private int testNo;
public WordOperate(){
wordApp = new ActiveXComponent("Word.Application");
Dispatch.put(wordApp, "Visible", new Variant(true));
docs = wordApp.getProperty("Documents").toDispatch();
userName = wordApp.getProperty("Username");
document = Dispatch.call(docs, "Add").toDispatch();
selection = Dispatch.get(wordApp, "Selection").toDispatch();
align = Dispatch.get(selection, "ParagraphFormat").toDispatch();
font = Dispatch.get(selection, "Font").toDispatch();
testNo = 1;
}
public void createDocument(){
document = Dispatch.call(docs, "Add").toDispatch();
}
public void insertTitle(String Title){
Dispatch.put(align, "Alignment", "1"); // 1:置中 2:靠右 3:靠左
Dispatch.put(font, "Bold", "1");
Dispatch.put(font, "Color", "0,0,0,0"); // 黑色
Dispatch.put(font,"Name","宋体");
Dispatch.call(selection, "TypeText", Title);
Dispatch.call(selection, "TypeParagraph"); // 空一行段落
Dispatch.call(selection,"TypeParagraph");
moveDown(1);
}
public void insertSelection(String Content,String keyA,String keyB , String keyC , String keyD){
Dispatch.put(align,"Alignment","3");
Dispatch.put(font,"Bold","0");
SpecifyContent = "" + testNo + ". " + Content;
Dispatch.call(selection, "TypeText",SpecifyContent);
Dispatch.call(selection,"TypeParagraph");
Dispatch.call(selection,"TypeText","A: " + keyA);
Dispatch.call(selection, "TypeParagraph");
Dispatch.call(selection,"TypeText","B: " + keyB);
Dispatch.call(selection, "TypeParagraph");
Dispatch.call(selection,"TypeText","C: " + keyC);
Dispatch.call(selection, "TypeParagraph");
Dispatch.call(selection,"TypeText","D: " + keyD);
Dispatch.call(selection,"TypeParagraph");
Dispatch.call(selection,"TypeParagraph");
moveDown(1);
testNo ++ ;
}
public void insertOthers(String Content){
Dispatch.put(align,"Alignment","3");
Dispatch.put(font,"Bold","0");
SpecifyContent = "" + testNo + ". " + Content;
Dispatch.call(selection, "TypeText",SpecifyContent);
Dispatch.call(selection,"TypeParagraph");
Dispatch.call(selection,"TypeParagraph");
moveDown(1);
testNo ++ ;
}
public void insertPara(String Content){
Dispatch.put(align,"Alignment","3");
Dispatch.put(font,"Bold","1");
Dispatch.call(selection, "TypeText",Content);
Dispatch.call(selection,"TypeParagraph");
Dispatch.call(selection,"TypeParagraph");
moveDown(1);
}
private void moveUp(int pos){
if (selection == null)
selection = Dispatch.get(document, "Selection").toDispatch();
for (int i = 0; i < pos; i++)
Dispatch.call(selection, "MoveUp");
}
private void moveDown(int pos){
if (selection == null)
selection = Dispatch.get(document, "Selection").toDispatch();
for (int i = 0; i < pos; i++)
Dispatch.call(selection, "MoveDown");
}
private void moveLeft(int pos){
if (selection == null)
selection = Dispatch.get(document, "Selection").toDispatch();
for (int i = 0; i < pos; i++){
Dispatch.call(selection, "MoveLeft");
}
}
private void moveRight(int pos) {
if (selection == null)
selection = Dispatch.get(document, "Selection").toDispatch();
for (int i = 0; i < pos; i++)
Dispatch.call(selection, "MoveRight");
}
private void moveStart() {
if (selection == null)
selection = Dispatch.get(document, "Selection").toDispatch();
Dispatch.call(selection, "HomeKey", new Variant(6));
}
private void moveEnd() {
if (selection == null)
selection = Dispatch.get(document, "Selection").toDispatch();
Dispatch.call(selection, "EndKey", new Variant(6));
}
public void openDocument(String docPath){
closeDocument();
Dispatch doc = Dispatch.invoke(docs, "Open",
Dispatch.Method,new Object[] { docPath, new Variant(false), new Variant(false)},
new int[1]).toDispatch();
}
public void saveAs(){
Dispatch.call(document, "SaveAs", tpFile);
}
public void save(){
Dispatch.call(document,"Save");
}
public void setFilePath(String file){
tpFile = file;
}
public void closeDocument() {
if (document != null) {
Dispatch.call(document, "Close", new Variant(0));
document = null;
}
}
public void close() {
closeDocument();
if (wordApp != null) {
Dispatch.call(wordApp, "Quit");
wordApp = null;
}
selection = null;
docs = null;
}
public int getTestNo() {
return testNo;
}
public void setTestNo(int testNo) {
this.testNo = testNo;
}
}