Office Development in Delphi

Source: Internet
Author: User
Tags define function

1. Introduction

 

When developing applications using Delphi, we usually need to design many documents and reports in various formats, and often create, edit, and modify files, although Delphi provides a lot of controls or methods for designing reports, there are obvious deficiencies, such as Chinese Input and Data Format control, which are troublesome. By using office tools that everyone is familiar with, you can effectively solve this problem. The following uses Delphi as an example to describe how to use office to create various documents and reports.

 

2. Implementation Method

 

There are three main methods for developing with Delphi and office:

 

(1) Use the tolecontainer control of Delphi to embed the Office. This is the simplest Ole embedding. You can directly call the office document by using tolecontainer. Run to directly start the Office document. The Office document and Delphi program started in this way are a whole (from the interface), but it has insurmountable shortcomings, that is, the Office document cannot be controlled through Delphi, in this case, the Office cannot be flexibly manipulated.

 

(2) Use the servers control provided by Delphi to call office, use office attributes, and use the servers control of Delphi to manipulate office. During programming, Delphi can implement code prompts, in general, Delphi can better control office, but some office functions cannot be called in Delphi (such as self-compiled VBA macro code ). When implementing the function, you can select a parameter in the VBA code and must add it when calling delphi. Otherwise, compilation fails. The Office and Delphi programs started in this way belong to two forms.

 

(3) Use createoleobject to start office and then Control Office in OLE mode. This method is to use createoleobjects to call the office, but it is still Ole. However, this method can truly fully control the Office file and use all the attributes of the Office, including the VBA macro code compiled by yourself. Compared with the servers control and COM technology, this method can truly use the various attributes of the Office. It is basically the same as writing your own code in VBA, and can be used by default without any code.

 

3. Software Implementation

 

3.1 Method 1: Use the tolecontainer control of Delphi to embed the Office as follows:

 

(1) Call the system page control tolecontainer in Delphi for implementation;

 

(2) double-click olecontainer and the following interface is displayed:

(3) Now you can select & #8220; create a file & #8221; and select the corresponding document, and simply implement the function of transferring office documents; for example:

 

 

3.2 Method 2: Use the servers control provided by Delphi to call office; it makes it easy for us to put Office applications (Word, Excel, PowerPoint, outlook and access) take word as an example to describe how to implement the control as a com Application Server:

 

(1) Call tworddocument (Create a word file object), twordapplication (start WORD and establish a connection with word), twordfont (set the font of the Word file), twordparagraphformat (set the paragraph format in the Word file) control;

 

(2) start WORD and implement it through twordapplication;

 

Try

 

Wordapplication. Connect;

 

Except

 

Messagedlg (word may not be installed, mterror, [mbok], 0 );

 

Abort;

 

End;

 

(3) create a new file, which is implemented through twordapplication;

 

Template: = emptyparam; // indicates that the template is not used.

 

Newtemplate: = false; // indicates that the type of the newly created document is document,

 

// Add a new document

 

Wordapplication.doc uments. Add (template, newtemplate); worddocument.connectto(wordapplication.doc uments. Item (itemindex ));

 

// Disable pinyin search and syntax search to improve program running efficiency

 

Wordapplication. Options. checkspellingasyoutype: = false;

 

Wordapplication. Options. checkgrammarasyoutype: = false;

 

(3) Insert data through tworddocument;

 

// Text

 

Worddocument. range. insertafter (Oracle Database CHR (#13 ));

 

// Table

 

Worddocument1.tables. Add (worddocument1.range, rownum, colnum, template, newtemplate); // Where rownum is the number of rows in the table and colnum is the number of columns in the table

 

// Insert data into the table

 

Worddocument1.tables. Item (1). Cell (1, 1). range. Text: = certificate number;

 

Worddocument1.tables. Item (1). Cell (1, 2). range. Text: = ID card number

 

(4) format settings

 

Wordfont. connectto (worddocument. Sentences. getlast. font); // set some text

 

Wordfont. connectto (worddocument. Paragraphs. getlast. font); // you can specify a text segment.

 

Wordfont. Bold: = 1;

 

Wordfont. italic: = 1 ;........................

 

Worddocument1.pagesetup. headerdistance: = 1.5; // you can specify the margins.

 

Worddocument1.pagesetup. footerdistance: = 1.75;

 

// Set the header and footer

 

Worddocument1.activewindow. activepane. View. seekview: = wdseekcurrentpagefooter; worddocument1.activewindow. activepane. selection. insertafter (inttostr (wdfieldnumpages) page );

 

// Align the footer to the right

 

Worddocument1.activewindow. activepane. selection. paragraphformat. Alignment: = wdalignparagraphright;

 

..............................

 

3.3 method 3: Use createoleobject to start office and then use OLE to control office. Take an Excel table as an example (the format of the table can be set in advance or customized by yourself. Here, the format of the table is set in advance as an example:

 

(1) Add the comobj class to uses;

 

(2) apply for a global variable: fvexcel. The type is variant, indicating that it is an Excel object.

 

(3) define a function to start excel, for example, define function openexcel (strfilename: string): Boolean;

 

Function tform1. openexcel (strfilename: string): Boolean;

 

Begin

 

Result: = true;

 

Try

 

Fvexcel: = createoleobject (Excel. application );

 

Except

 

Application. MessageBox (failed to open excel, pchar (application. Title), mb_iconerror );

 

Result: = false;

 

End;

 

Fvexcel. Visible: = true;

 

Fvexcel. Caption: =;

 

Fvexcel. workbooks. Open (strfilename); // open a workbook

 

Fvexcel. worksheets [1]. Activate; // you can specify 1st worksheets as active worksheets.

 

End;

 

(4) Insert data to fvexcel

 

Fvexcel. cells [row, Col]. Value: = & #8217; China & #8217; // row indicates row and Col indicates column;

 

(5) set the format

 

Fvexcel. Rows [row]. Font. Color: = clred; // you can specify the color of a Data row.

 

Fvexcel. activesheet. pagesetup. leftmargin: = 1; // set the left margin

 

Fvexcel. activesheet. pagesetup. rightmargin: = 1; // set the right margin

 

Fvexcel. activesheet. pagesetup. Zoom: = 100; // you can specify the display ratio.

 

Fvexcel. activesheet. pagesetup. papersize: = xlpapera4; // you can specify the size of the printed paper.

 

4. Some experiences

 

In the process of program design, we often need to set the format of office documents and tables. In this case, we need to use many attributes, methods, and functions, these attributes, methods, and functions are rarely used at ordinary times. If there is no VBA manual in hand, it is difficult to find these things. Therefore, a better way is to first open the office, complete the format you want to design in advance, use the macro record in office, open the Visual Basic Editor in office, and view the code to see the attributes, methods, or functions used by your operations. Usually many attributes, functions, or methods can be directly moved to the Delphi Program. Some of them can be used in Delphi as long as they are slightly modified.

 

5. Conclusion

 

With Delphi, you can work perfectly with the Office to design tables, texts, slides, and other styles. At the same time, you can use Delphi to operate the database, insert the required data into the appropriate location of the office, and preview, print, and save the required data through the functions of the Office itself, reducing the time required for program debugging and locating and debugging, the difficulty of simplified programming reduces the workload of programmers. For users, the use of familiar office increases the system operability. This idea has been widely used in the title Review Management Information System & #8220.

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.