Java calls the word method through jacob

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.