Java calls Word in Jacob Word to choose from J2EEJIA's blog

xiaoxiao2021-03-06  20

Public static void main (String [] args) {

// Start Word, generate an ActiveXComponent object

ActiveXComponent App = New ActiveXComponent ("Word.Application");

// Word file to be converted

String infile = "d: //project//java_word//java_word//wordTemplet.doc";

// The target file to be reported

String tpfile = "d: //project//java_word//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));

//log.info ("Setting Word is invisible!");

System.out.println ("Setting Word Not Obtaining Success!");

Dispatch DOCS = App.getProperty ("Documents"). Todispatch ();

Dispatch doc = dispatch.invoke (DOCS, "Open", Dispatch.Method

New Object []

{Infile, New Variant (false), New Variant (TRUE)}

New int [1]). Todispatch (); // Open Word file

// Aplication has the username property in the VBA document of Word2003.

String username = app.getpropertyasstring ("username");

System.out.println ("User Name: Username);

Dispatch selection = app.getProperty ("selection"). Todispatch ();

// Get a component

System.out.println ("Selection));

Dispatch Find = App.call (Selection, "Find"). Todispatch ();

// What 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"); // Specify is set to the value specified tags to Dispatch activeDocument = app.getProperty ( "ActiveDocument") toDispatch ();. System.out.println ( "activedocument"); 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 ") .to Dispatch (); system.out.println ("Range!"); // Take the value of the label String BookmarkValue = dispatch.get (Range, "Text"). ToString (); BookmarkValue = "Test"; if (BookmarkValue! = NULL) {Dispatch.put (Range, "Text", new variant (bookmarkvalue));}} else {system.out.println ("not exists bookmark!");} // save file Dispatch.invoke (DOC, "Saveas"

转载请注明原文地址:https://www.9cbs.com/read-46885.html

New Post(0)