Use the OLE automation function in C Builder to call Word for report production
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 () - > Text = WideString ("This is the first word call program"); // Add a new text in the document WordApp-> Visible = true; // Display Word} catch (...) {showMessage ("You may No Word! "); WordApp-> disconnect (); step 6 Compile the running program. 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-> Visible properties to achieve. (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 to WordDoc-> paragraphs-> add (); // Add a paragraph WordDoc-> paragraphs-> item (variant (1)) -> Range-> font-> name = WideString (" "); // set this paragrahead word worddoc-> paragraphs-> item (variant (1)) -> Range-> font-> size = 20; // Set the font size worddoc-> paragraphs-> item (variant) 1) -> Range-> text = WideString ("This is the first text!"); // Current paragraph word worddoc-> paragraphs-> item (variant (1)) -> alignment = 1; // setting This paragraph is hitting WordDoc-> paragraphs-> add (); // Add a paragraph WordDoc-> paragraphs-> item (variant (2)) -> Range-> font-> name = WideString ("Song"); / / Set this section text fonts WordDoc-> paragrap HS-> item (variant (2)) -> Range-> font-> size = 12; // Set the font size worddoc-> paragraphs-> item (variant (2)) -> Range-> Text = WideString ("this It is the second paragraph! "); // Current paragraph text worddoc-> paragraphs-> item (variant (2)) -> alignment = 0; // Set this paragrahead left WordDoc-> paragraphs-> add (); // Add a paragraph worddoc-> paragraphs-> item (variant (3)) -> Range-> font-> name = WideString ("Song Body"); // Set this section text Font WordDoc-> paragraphs-> Item VARIANT (3)) ->
Range-> font-> size = 10; // Set the font size word worddoc-> paragraphs-> item (variant (3)) -> Range-> text = WideString ("This is the third paragraph!"); // Current Segment text worddoc-> paragraphs-> item (variant (3)) -> alignment = 2; // Set this paragraph home wordapp-> visible = true;} catch (...) {showMessage ("You may not Word! "); WordApp-> disconnect ();} Analysis: In this program we use two components about Word calls, in the WordDocument component, we use the connectTo function to connect with WordApplication, through this connection You can easily operate one of the documents of Word using the functions provided by the WordDocument component. 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 just: Setting the statement for the fonts of the paragraph is this: WordDoc-> paragraphs-> item (variant (2)) -> Range-> font-> size = 12; if we want to set The fifth character in the paragraph is 9 characters, then we need to limit the range of font settings, the procedure is 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-> size = 9; // Set the font of the last selected character to 9 characters
(Example 3) This example will illustrate the method of calling the Word drawing table in C Builder, including the generation 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-> 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) cells from 2 lines 3 columns in units
Through this example we have mastered the basic ways to call Word in C Builder, and the basic operation of the cell 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.