Java calls the word method through jacob
Public static void main (String [] args ){
// Start word to generate an ActivexComponent object
ActiveXComponent app = new ActiveXComponent ("Word. Application ");
// The word file to be converted
String inFile = "D: // project // java_word // wordtemplet.doc ";
// Target file to be reported for storage
String tpFile = "D: // project // java_word // wordtemplet3.doc ";
Boolean flag = false;
Dispatch xlo = app. getObject ();
String oldText = "1234 ";
String newText = "test ";
Boolean visible = false;
String bookMarkKey = "LB_KJGG ";
Try {
// Set word invisible
App. setProperty ("Visible", new Variant (visible); string 7
// Log.info ("the word is not visible! ");
System. out. println ("the word is not visible successfully! ");
Dispatch docs = app. getProperty ("events"). toDispatch ();
Dispatch doc = Dispatch. invoke (docs, "Open", Dispatch. Method
, New Object []
{InFile, new Variant (false), new Variant (true )}
, New int [1]). todispatch (); // open the Word file
String 8
// In the word2003 VBA document, the application has the username attribute.
String username = app. getpropertyasstring ("username ");
System. Out. println ("username:" + username );
Dispatch selection = app. getproperty ("selection"). todispatch ();
// Obtain a component
System. Out. println ("selection ");
Dispatch find = app. Call (selection, "find"). todispatch ();
String 7
// Find the text
Dispatch. put (find, "Text", oldText );
// Replace text
Dispatch. call (find, "ClearFormatting ");
Dispatch. put (find, "Text", oldText );
Dispatch. call (find, "Execute ");
Dispatch. put (selection, "Text", newText );
// Dispatch. call (app, "SaveAs", inFile );
System. out. println ("replace ");
// Set the specified value to the specified tag
Dispatch activeDocument = app. getProperty ("ActiveDocument"). toDispatch ();
System. Out. println ("activedocument"); string 1
Dispatch bookmarks = app. Call (activedocument, "bookmarks"). todispatch ();
System. Out. println ("bookmarks ");
Boolean bookmarkexist1 = Dispatch. Call (bookmarks, "exists", bookmarkkey). toboolean ();
If (bookmarkexist1 = true ){
System. Out. println ("exists bookmark! ");
Dispatch rangeitem = Dispatch. Call (bookmarks, "item", bookmarkkey ).
Todispatch ();
System. out. println ("range item! ");
Dispatch range = Dispatch. call (rangeItem, "Range"). toDispatch (); string 4
System. out. println ("range! ");
// Obtain the tag Value
String bookMarkValue = Dispatch. get (range, "Text"). toString ();
BookMarkValue = "test ";
If (bookmarkvalue! = NULL ){
Dispatch. Put (range, "text ",
New variant (bookmarkvalue); string 4
}
}
Else {
System. Out. println ("not exists bookmark! ");
}
// Save the file
Dispatch. Invoke (Doc, "saveas", dispatch. method, new object [] {tpfile, new variant (0)}, new int [1]);
// Save it as a word to the target file
Variant F = new variant (false );
Dispatch. Call (Doc, "close", F );
Flag = true;
} String 4
Catch (exception e ){
E. printstacktrace ();
}
Finally {
App. Invoke ("quit", new variant [] {});
}
String 9