Use Jacob to operate Word documents

Source: Internet
Author: User

Remember to use activexcomponent to call the quit method of Word

1. Create a blank word document, add the content, and save it.
Public class newaword
{
Public newaword (){
// Initialize the com thread, which is very important !! Call the release method after use.
Comthread. initsta ();
// Initialize the word application, create a blank document, and obtain the document content object
Activexcomponent objword = new activexcomponent ("word. application ");
Dispatch wordobject = (dispatch) objword. GetObject ();
Dispatch. Put (dispatch) wordobject, "visible", new variant (true); // new variant (true) indicates that the word application is visible
// Tip: when setting the attributes of an object, assign values to the attributes using the PUT Method of dispatch. The preceding statement is equivalent to the wordobject. Visible = true statement of VB.
Dispatch documents ents = objword. getproperty ("documents"). todispatch (); // documents indicates all document windows of Word (word is a multi-document application)
Dispatch document = Dispatch. Call (documents, "add"). todispatch (); // use the Add command to create a new document. Use the open command to open an existing document.
// Tip: when calling an object method, use the call method of dispatch. The preceding statement is equivalent to the document = documents. Add () Statement of VB.
Dispatch wordcontent = Dispatch. Get (document, "content"). todispatch (); // get the content of the Word file
Dispatch. Call (wordcontent, "insertafter", "content of a paragraph"); // insert a paragraph
// 4. Set the text format of the inserted paragraph
Dispatch paragraphs = Dispatch. Get (wordcontent, "paragraphs"). todispatch (); // all paragraphs
Int paragraphcount = Dispatch. Get (paragraphs, "Count"). toint (); // The total number of paragraphs
// Locate the entered paragraph and set the format
Dispatch lastparagraph = Dispatch. Call (paragraphs, "item ",
New variant (paragraphcount). todispatch (); // The last section
Dispatch lastparagraphrange = Dispatch. Get (lastparagraph, "range"). todispatch ();
Dispatch font = Dispatch. Get (lastparagraphrange, "font"). todispatch ();
Dispatch. Put (font, "bold", new variant (true); // you can specify this parameter as a blacklist.
Dispatch. Put (font, "italic", new variant (true); // set it to italic.
Dispatch. Put (font, "name", new variant (" ant "));//
Dispatch. Put (font, "size", new variant (12); // small four
Dispatch. Call (document, "saveas", new variant ("F:/abc.doc"); // save a new document
// Use activexcomponent to call the quit method of Word
Objword. Invoke ("quit", new variant [] {});
Comthread. Release (); // release the com thread. According to Jacob's help document, com thread collection cannot be processed by Java's Garbage Collector
}
 public static void main(String[] args) {
    new NewAWord(); 
 }
}
Activexcomponent is a subclass of dispatch. Dispatch'sObject represents MS level dispatch object. Article http://danadler.com/jacob/JacobThreading.html on Jacob threads Single threaded apartment (STA) Main staCom requires that if there is any apartment threaded component in your application, then the first sta created is tagged as Main sta. Com uses the main sta to create all the apartment threaded components that are created from an MTA thread. the problem is that if you have already created an STA, then com will pick that as the main STA, and if you ever exit that thread-the whole application will exit. com requires that if there are any apartment threads in your application, the first created STA will be marked Main Sta. Com. Use main sta to create allApartment Thread. The problem is that if you have created a STA, com will choose it as mainsta, And if you exit this thread, the entire application will exit.   Save different formats
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { mhtPath,            new Variant(9) }, new int[1]);
By default, docx7 is saved as txt8 and html9 is saved as MHT dispatchput. Set the invoke call method. Return information: Call call command in the call parameters. Return information: activexcomponetsetproperty. Set the properties of the control, the key to using Jacob is to understand the use manual of the ActiveX control word application, which can be found in the developer manual of the word help.

Variant. Default is used for optional parameters

 

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.