Deep control of Word using JAVA

Source: Internet
Author: User
Use the JAVA language to deeply control Word-general Linux technology-Linux programming and kernel information. The following is a detailed description. It is very troublesome to control Office controls in Java.
With JACOB, things have become much easier.
However, it is very troublesome to implement flexible control of Word in Java.

Below we will introduce several common WORD objects and some typical processing procedures, and hope to help you.
(Note: JDK1.3.2 runs Jacob normally, and JDK1.4 has a problem)
/** WORD object */
Private ActiveXComponent word = null;
/** Document Object */
Private Dispatch documents = null;
/** The selection object is an important object */
Private Dispatch vSelection = null;
/** One Word document */
Private Dispatch wordfile = null;
1. Initialization
Word = new ActiveXComponent ("Word. Application ");
Documents = word. getProperty ("Documents"). toDispatch ();
(It is easy to put JACOB under WINNT \ system32)
2. open the file
Wordfile = Dispatch. invoke (
Documents,
"Open ",
Dispatch. Method,
New Object [] {
StrFileName,
New Variant (true), // whether to convert ConfirmConversions
New Variant (false) // read-only
}, New int [1]). toDispatch ();
VSelection = word. getProperty ("Selection"). toDispatch ();
In WORD, when the selected content is converted, it does not need to be retrieved back and forth like a Java object. This object is always valid.
3. Display WORD
Word. setProperty ("Visible", new Variant (visible ));
4. Set the WORD location
Dispatch activeWindow = Dispatch. get (word, "Application"). toDispatch ();
Dispatch. put (activeWindow, "WindowState", new Variant (0 ));
Dispatch. put (activeWindow, "Top", new Variant (0 ));
Dispatch. put (activeWindow, "Left", new Variant (0 ));
Dispatch. put (activeWindow, "Height", new Variant (600 ));
Dispatch. put (activeWindow, "width", new Variant (800 ));

To exchange data in JAVA with the WORD, it is often used to make some special marks on the WORD, using the FIND and Replace methods, this method is not very good.
I personally think it is more convenient to use the hyperlink mode.
There are several advantages:
1. Hyperlink has three zones for developers to use.
ActiveDocument. Hyperlinks. Add
Anchor: = Selection. Range,
Address: = "location", // Address (available) has a disadvantage
SubAddress: = "", // sub-location (available)
ScreenTip: = "", // on-screen prompt
TextToDisplay: = "show content" // The best thing to use

We recommend that you use TextToDisplay.
Address is replaced with the absolute path when it is saved.
For example
"AA. BB. CC"
It may be replaced
C: \ Documents ents and Settings \ Administrator \ My Documents ents \ AA. BB. CC
2. You can perform automatic locating.
Hyperlinks can be used to obtain all Hyperlinks in the article.
You can also obtain the hyperlink of the specified range.
3. Free Layout
4. copy and paste

Add hyperlink:
Dispatch Hyperlinks = Dispatch. get (wordfile, "Hyperlinks"). toDispatch ();
Dispatch range = Dispatch. get (vSelection, "Range"). toDispatch ();
Dispatch h = Dispatch. invoke (Hyperlinks,
"Add", Dispatch. Method, new Object []
{Range,
New Variant ("Address "),
New Variant ("SubAddress "),
New Variant ("{table. fieldName}"), // recommended data link
New Variant ("name")}, // content displayed in WORD
New int [4]). toDispatch ();
Dispatch hRange = Dispatch. get (h, "Range"). toDispatch ();
Dispatch. call (hRange, "select ");
// Set the font and color
Dispatch font = Dispatch. get (vSelection, "Font"). toDispatch ();
Dispatch. put (font, "Underline", new Variant (0 ));
Dispatch. put (font, "Color", new Variant (0 ));
// Cancel the selection
Dispatch. call (vSelection, "MoveRight", new Variant (1), new Variant (1 ));

Hyperlink replacement content:
1. Obtain all hyperlinks.
// Select an object
Dispatch. call (dObject, "select ");
// Obtain the hyperlink set
Dispatch Hyperlinks = Dispatch. get (vSelection, "Hyperlinks"). toDispatch ();
// Obtain the number of hyperlinks
Int nHyperlink = Dispatch. get (Hyperlinks, "count"). toInt ();
// Obtain a hyperlink
Dispatch hyperlink = Dispatch. invoke (Hyperlinks, "item ",
Dispatch. Method, new Object [] {new Integer (I + 1 )},
New int [1]). toDispatch ()));
2. Replace content
Dispatch. put (hyperlink, "TextToDisplay", information );
3. Cancel the hyperlink and convert it into plain text.
Dispatch. call (hyperlink, "delete ");

We recommend that you use tables for automatic expansion of batch data, which is convenient and simple.
Combine the above hyperlink technology. It will be very simple:

For example, the following data is available:

DataA
DataB

1. List all tables
It is basically the same as listing all hyperlinks:
Private void getTables01 (Dispatch objcet, Vector vTableStore ){
Dispatch tables = Dispatch. get (objcet, "tables"). toDispatch ();
Int nTableAmount = Dispatch. get (tables, "count"). toInt ();
For (int I = 0; I <nTableAmount; I ++ ){
Dispatch table =
Dispatch
. Invoke (
Tables,
"Item ",
Dispatch. Method,
New Object [] {new Integer (I + 1 )},
New int [1])
. ToDispatch ();
VTableStore. add (new DTable (table ));
GetTables01 (table, vTableStore); // process the table applied to the table
}
}
2. Controllable objects of tables
Dispatch dRows = Dispatch. get (dTable, "rows"). toDispatch (); // all rows
Int nRows = Dispatch. get (dRows, "count"). toInt ();
3. Get the content of a row
Dispatch dRow =
Dispatch
. Invoke (
Rows,
"Item ",
Dispatch. Method,
New Object [] {new Integer (row + 1 )},
New int [1])
. ToDispatch ();
Return dRow;
} Catch (ComFailException cfe)
{
/** Cases with merged rows */
Return null;
}
4. Obtain the hyperlink of a row.
DHyperlink dhInRow [] = listHyperlinks (dRow );
5. Copy a row multiple times
Dispatch. call (dRow, "select ");
Dispatch. call (vSelection, "Copy ");
Int nCopyNow = nDataBlockRow-1;
For (int nCopys = 0; nCopys <nCopyNow; nCopys ++ ){
Try {
Dispatch. call (vSelection, "Paste ");
} Catch (Exception e) {e. printStackTrace ();
// Sometimes the document is corrupted. You can ignore this issue and have actually pasted it.
}
}
6. Replace the content. You don't need to introduce it here.

Print preview:
Dispatch. call (wordfile, "PrintPreView ");

Other functions
The macro recording of WORD, the VB editor, and auxiliary functions can be used to discover them.
Related Article

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.