Using the C Builder 6 OLE Automation Function Removal Word to report production This article is a supplement to the original space in CB6, and the development of CB5 can refer to the original article.
1. Overview When we use C Builder to report design and print processing, we are often designed and output by the components in the QReport component page provided by C Builder. However, the table output is in advance, and it is not easy to modify, and it is very troublesome when the system is transplantable and needs to be modified. So, what is a good solution to realize it you can automatically generate a report, and have you flexiblely modify the generated report? Almost every person who has used the Word word processing software in Miscrosoft Office 2000 knows: Word is a powerful text, form, typographic processing software. Different fonts, font numbers can be set in Word, and draw a table of different shapes ... Word powerful features have left a deep impression in using it. If we can handle the data of C Builder to generate data into the report to Word; in Word, you can modify the generated report, which is better! The answer is affirmative, we can call Word in C Builder to generate a variety of different reports.
Second, the implementation will use a specific example to explain how to use C Builder to call Word's design and production. We can design the application after installing C Builder and Word. In the process of installing C Builder, you will select the version of the Office in the system you are using, C Builder can choose two Version: Office2000 and Office97. In the instance we have, it is done in the Office2000 version.
(Example 1) An example of the following will explain how to call Word in C Builder and print a string. Step 1 Open the C Builder development platform, create a new project step 2 Find the Office2K component page in the component panel of C Builder, and we can see the following components on this component page: Here we have these components Call the Word section for detailed instructions: Wordglobal Components: Call the most advanced properties and methods for Word. WordApplication Components: Word Application Components, complete calls to Word applications. It is a core component that implements Word calls. WordDocument Components: Word document processing components for handling specific contents of the document. WordFont Components: Used to set text settings information such as fonts, font numbers in Word. WordParaGraphFormat Components: Used to process paragraph formats in Word. WordletterContent Components: Components that call the elements of a letter created by the letter wizard.
Step 3 Add a WordApplication component to the Form form of the new project, change the Name property to: WordApp1. Step 4 Add a button component to the Form form set to: "Word Call" Step 5 Double-click the button component to switch to the code editing window, add the following code:
Try {WordApp-> Connect (); // Connect WordApp-> Documents-> add (); // Create a new document WordApp-> Documents-> Item (Variant (1)) -> Range () - > INSERTAFTER ("This is a test text")); // Add a new text in the document WordApp-> Application-> Visible = true; // Display Word} catch (...) {showMessage ("you Word! ") May not be installed; WordApp-> disconnect (); step 6 compiles running procedures. At this time, we can see the "Word Call" button: The Word program is automatically run, and a string is displayed in Word, and the call of the entire program is completed. Analysis: This is the simplest Word call instance. Below, let's analyze the code we entered: WordApp-> Connect (); this statement is to connect our applications and Word if the connection fails may be because there is no Word or Word error. Therefore, we use the Try ... catch (...) statement to process possible errors. WordApp-> disconnect (); This statement is the opposite operation, if you have already connected with Word, then this statement is to disconnect with Word. It should be noted that if you do not close the Word program, run this statement in C Builder, it may result in a connection error. WordApp-> Documents-> add (); This statement is to create a new document in Word, because Word is a multi-document handler, so we can build multiple documents to operate. Documentation is also distinguished by Item functions under document. For example, in the program we use: WordApp-> Documents-> Item (Variant (1)) to return a pointer to a specific document. The Range () function is used to set the scope of the text operating in the document, and if there is no parameters in the Range () function, it is shown in all ranges. We use the TEXT attribute written in the pointer returned to the Range to write a string, and you can see this string in the document. Finally, what we do is displaying the results of the Word program run. We use the WordApp-> Application-> Visible property.
(Example 2) In this example, we will explore the method of C Builder to invoke the Word program to implement the report. It will include the setting of the font, the format of the paragraph, and the like. We will output three words in Word, where the fonts of the first paragraph are set to the black body and hit, the font size is the word, the second paragraph set the font to the Song No. 5 word, the third paragraph will live and Song Xiaowan word. We build two objects called Word, one for the WordApplication object, another is the WordDocument object. The name of the object is: WordApp and WordDoc. Below is the code of the entire program:
Try {WordApp-> Connect (); // Connect Word WordApp-> Documents-> add (); // Create a new document WordDoc-> Connectto (WordApp-> Documents-> Item (Variant (1))); / / Connect the WordDocument object with the WordApplication object WordDoc-> paragraphs-> add (); // Add a paragraph WordDoc-> paragraphs-> item (variant (1)) -> Range-> font-> set_name (WideString " Black body ")); // Set this paragrahead word worddoc-> paragraphs-> item (variant (1)) -> Range-> font-> set_size (20); // set the font size wordDoc-> paragraphs-> item ( Variant (1)) -> Range-> set_text ("This is the first text!")); // Current paragraph text WordDoc-> paragraphs-> item (variant (1)) -> alignment = wdalignparaGraphCenter; // Set this paragraph home worddoc-> paragraphs-> add (); // Add a paragraph Worddoc-> paragraph S-> item (variant (2)) -> Range-> font-> set_name ("Song")); // Setting this section text Font WordDoc-> paragraphs-> item (variant (2)) -> Range-> font-> set_size (12); // Set the font size worddoc-> paragraphs-> item (variant (2)) -> Range-> set_text (WideString ("This is the second text!")); / / Current Paragraph WordDoc-> paragraphs-> item (variant (2)) -> alignment = wdalignparagraphLeft; // Set this paragraph WordDoc-> Paragraphs-> add ();
// Add a paragraph worddoc-> paragraphs-> item (variant (3)) -> Range-> font-> set_name (WideString ("Song")); // Setting this section text Font WordDoc-> paragraphs-> Item (Variant (3)) -> Range-> font-> set_size (10); // Setting the font size worddoc-> paragraphs-> item (variant (3)) -> Range-> set_text (WideString ("This is the third Segment text! "); // Current paragraph text WordDoc-> paragraphs-> item (variant (3)) -> alignment = wdalignparagraphright; // Set this parapp-> Application-> Visible = true; Catch (...) {ShowMessage ("You may not have Word!"); WordApp-> disconnect ();} Analysis: In this program we use two components about Word call, in the WordDocument component The use of the ConnectTo function is connected to WordApplication. You can easily use the functions provided by the WordDocument component to make specific operations in one of the documents provided by WordDocument components. Make us more convenient to control and modify the reports we produce. In the use of the WordDocument component, we use Paragraphs to operate the paragraphs in Word, including the settings of fonts in the paragraphs and types of typographic patterns. This shows that C Builder provides us with a very convenient Word call component to let us implement Word applications. Expand: What should we do if we need to set the font in the paragraph? We see in the programs that: set the statements for the fonts of the paragraphs is this: worddoc-> paragraphs-> item (variant (2)) -> Range-> font-> set_size (12); if we want Set the 5 characters in the paragraph to 9 characters, then we need to limit the range of font settings, as follows:
Worddoc-> paragraphs-> item (variant (2)) -> Range-> select (); // Put the second paragraph text Selection-> START = WordApp-> Selection-> start 5; / / 5 characters in the selected range as a selected start character wordapp-> selection-> end = wordapp-> selection-> start 1; // Set the selected end character set to start character plus 1 character WordApp -> selection-> font-> set_size (9); // Set the font of the last selected character to 9 words (instance 3) This instance will explain the method of calling the Word drawing form in C Builder, including The formation of the table, the control and operation of the cell. The following code will implement a 4-multiplying table in Word.
WordDoc-> Tables-> Add (WordDoc-> Range (EmptyParam, EmptyParam), 4, 8); // Add a form in Word
Here we call Tables in the WordDocument object to implement the drawing of the table. The Add function is to add a form. The parameters are: The table is added to the location, table, and the number of tables. EmptyParam is the defined constant in the parameter. If we need to add a table to the end of the entire document, we can change this code to:
Worddoc-> Tables-> Add (WordDoc-> Range (variant (WordDoc-> Range (EmptyParam, EmptyParam) -> End-1), EMPTYPARAM, 4, 8); // Add a form at the end of the Word document
In the table, if we need to operate cells, for example, in line 3, the cells in column 5 we need to insert a piece of text, you can do this:
Worddoc-> Tables-> Item (Variant (1)) -> Cell (Variant (3), Variant (5)) -> Range-> Set_text (WideString (Added Text));
Among them, the value in the ITEM is a table that specifies the need to operate. The value in Cell is the cell location in the specified table. In the form, if we need to merge two cells, you can do the following:
Worddoc-> Tables-> Item (Variant (1)) -> Cell (Varient (3), Variant (5)) -> Marge (WordDoc-> Tables-> Item (Variant (1)) -> Cell (Variant 3), variant (6))); // Merge in the table (3, 5) and (3, 6) cells
In the form, if we need to split cells, do the following:
Worddoc-> Tables-> Item (Variant (1)) -> Cell (Variant (3), Variant (5)) -> Split (Variant (2), Variant (3)); // Put the table (3, 5) Unit Split into 2 lines 3 columns Through this example we have mastered the basic method of calling Word in C Builder to formulate form drawing, while the basic operation of cells in the table is also known. The following example is how to add an image in Word.
The basic method of adding images in WORD is to use a clipboard to add images. Let's copy the image to the clipboard and then paste it in Word. For example, we are transferred from the image stored in the database to Word, which is as follows:
DbiMage-> CopyToClipboard (); // Image Copy to Clipboard WordDoc-> Sentences-> Last-> Paste (); // Paste to Word
Summary: There are still many ways to perform data, reports, etc. in C Builder. Here I only introduce several call methods commonred in Word calls, and the range involved is still small. I just hope that you can develop more and better applications with C Builder to develop more interested in programs.