Go: Delphi and Office program development--Good to read

Source: Internet
Author: User

Http://www.delphifans.com/infoview/Article_730.html
Date: February 20, 2006 2003 Views: 5602 view: [Font small font]1. Introduction in the Use of Delphi development applications, usually we want to design a lot of documents in various formats, reports, often to create, edit and modify the file, although Delphi itself provides a lot of design report control or method, but there are obvious shortcomings, such as Chinese input, The control of the data format is more cumbersome to operate. With the use of familiar Office tools and development, can be a good solution to this problem. The following is an example of Delphi in detail how to use Office to complete a variety of documents, report production. 2. Implementation methods using Delphi and Office development are the following three ways: (1) through the control of Delphi Tolecontainer office embedded, Gourmet Pro pi サ 腛 le embedded, can directly call Office documents, Just use Tolecontainer.run to start the Office document directly. and the Office document that started this way is a whole (from the interface) of the Delphi program, but it has the insurmountable disadvantage that the Office document cannot be controlled by Delphi, and that it is not possible to manipulate office flexibly. (2) Use the servers control provided by Delphi to invoke office, use the properties of Office, use the Delphi servers control to manipulate office, and when programming Delphi can implement code hints, Overall, it is better to implement Delphi's control of office, but there are some functions that can not be called in Delphi (such as the VBA macro code you write yourself). And the implementation of the function is originally in the VBA code can choose parameters in the Delphi call must be added, otherwise, even compile can not pass. Office and Delphi programs that start this way are divided into two forms. (3) Use Createoleobject to start Office, and then to control office in OLE mode. This approach is to use createoleobjects to call office, actually OLE, but this way you can really take full control of Office files and be able to use all the properties of office, including the VBA macro code you write yourself. Compared with servers controls and COM technologies, this method can really use the various properties of office, and it is basically the same as writing your own code in VBA, which can be used by default code. 3. Software implementation 3. 1 The first method: Use the Delphi control Tolecontainer to embed office in the following ways: (1)Using the System page control in Delphi Tolecontainer implementation; (2) Double-click the Olecontainer, the following interface appears: (3) Then you can make a selection, select "Create From File", select the appropriate document, and a simple implementation of the ability to transfer Office documents into. 3. 2 Second method: Use the servers control provided by Delphi to invoke office; It makes it easy for us to put applications in office (Word, Excel, PowerPoint, Outlook and access, etc.) As a COM application server for control, in Word as an example, describes its implementation methods: (1) Call tworddocument (build Word file object), Twordapplication (start Word and establish a connection with word), Twordfont (Sets the font for Word files), Twordparagraphformat (sets paragraph formatting in Word files) control, (2) starts Word, implements it by twordapplication ; trywordapplication.connect; Exceptmessagedlg (' Word may not be installed ', mterror, [Mbok], 0); Abort; End; (3) Create a new file to be implemented by twordapplication; Template: = Emptyparam; Indicates that the template newtemplate is not used: = False; Represents the type of new new document as document,//Add New document WORDAPPLICATION.DOCUMENTS.ADD (Template, newtemplate); Worddocument.connectto (WordApplication.Documents.Item (ItemIndex));//Turn off Pinyin lookup and syntax lookups, In order to improve the efficiency of the program WordApplication.Options.CheckSpellingAsYouType: = False; WordApplication.Options.CheckGrammarAsYouType: = False; (3) inserting data, implemented 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 table rows, Colnum is the number of table columns//INSERT data into the table WordDocument1.Tables.Item (1). Cell (). Range.text:= ' certificate number '; WordDocument1.Tables.Item (1). Cell. range.text:= ' ID number ' (4) Format setting Wordfont.connectto (WordDocument.Sentences.GetLast.Font); Set some text Wordfont.connectto (WordDocument.Paragraphs.GetLast.Font); Set a paragraph of text wordfont.bold:=1; Wordfont.italic:=1, ....... ........... worddocument1.pagesetup.headerdistance:=1.5; Set margin worddocument1.pagesetup.footerdistance:=1.75; Set header footer Worddocument1.activewindow.activepane.view.seekview:=wdseekcurrentpagefooter; WordDocument1.ActiveWindow.ActivePane.Selection.InsertAfter (' +inttostr (wdfieldnumpages) + ' page ');// Let the footer align to the right worddocument1.activewindow.activepane.selection.paragraphformat.alignment:=wdalignparagraphright, ........ ............ 3. 3 The third method: Use Createoleobject to start Office, and then to control office in OLE mode. Take the Excel table as an example (the format of the table can be set in advance, or it can be self-defined, here in advance to set the format of the table as an example), to explain: (1) Add Comobj class in uses; (2) apply for a global variable: fvexcel, type variant; Represents an Excel object (3) that defines a function to start Excel, as defined by FuNction Openexcel (strfilename:string): Boolean; Function Tform1. Openexcel (strfilename:string): Boolean;beginresult: = true;tryfvexcel: = Createoleobject (' Excel.Application '); Exceptapplication.messagebox (' Open Excel failed ', PChar (application.title), mb_iconerror); Result: = False;end; Fvexcel.visible: = True; Fvexcel.caption: = "; FvExcel.WorkBooks.Open (strFileName); Open Workbook fvexcel.worksheets[1]. Activate; Set the 1th sheet as the active sheet end, (4) Insert data into Fvexcel fvexcel.cells[row,col].value:= "China"; Row represents rows, col represents columns, and (5) formats fvexcel.rows[row].font.color:=clred; Sets the color fvexcel for a row of data. activesheet.pagesetup.leftmargin:=1;//set the left margin fvexcel. ActiveSheet.PageSetup. rightmargin:=1;//set the right margin fvexcel. activesheet.pagesetup.zoom:=100;//Set the display scale fvexcel. activesheet.pagesetup.papersize:= xlpapera4;//Set the print paper size to 4. Some experience in the process of programming, we often have to set up Office documents, table format, then to use a lot of properties, methods and functions, and so on, these properties, methods and functions are usually rarely used, if there is no VBA manual on hand, it is difficult to find these things, so, The good way to do this is to open office first, do the formatting you want to design, make the macro records in office, and then open the Visual Basic Editor in office and see the code to see the properties, methods, or functions that your operation uses. Often many properties, functions, or methods can be moved directly into the Delphi program, and part of it is onlyTo be slightly modified, it can also be used in Delphi. 5. Conclusion Delphi can be a perfect combination of office, design a variety of styles of tables, text and slides and so on. At the same time, through the Delphi operation database, the required data into the appropriate location of office, through the functions of office itself for previewing, printing and saving, so as to reduce the time to save program debugging and positioning debugging, simplified programming difficulty, reduce the workload of the programmer. For users, the use of familiar office, increased system operability. This idea has been used extensively in the "Management Information system of professional title review". 2006-2-15 9:19:00 Reviews»»»2006-2-15 9:19:32//Get data from the Word table procedure Getwordcellstr;var wordapp:twordapplication; Worddoc:tworddocument;docinx,ofilename,cfcversions,oreadonly,addtorctfiles,pswdocument,pswtemplate,orevert, Wpswdocument,wpswtemplate,oformat:olevariant;i,irow,icol:integer;mycell:cell;myrow:row;beginmemo1. lines.clear;//===== Create object =====if not Assigned (WordApp) then beginwordapp:= twordapplication.create (nil); Wordapp.visible: = False;end;if not Assigned (worddoc) thenworddoc:= tworddocument.create (nil); trydocinx:=1;ofilename : = ' d:\test.doc '; oreadonly:=true; Cfcversions: = Emptyparam; Addtorctfiles:= Emptyparam; Pswdocument:= Emptyparam; pswtemplate:= emptyparam;orevert:= Emptyparam; Wpswdocument:= Emptyparam; wpswtemplate:= emptyparam;oformat:= Emptyparam; //===== Open File ===== WordApp.Documents.open (ofilename,cfcversions,oreadonly,addtorctfiles,pswdocument,pswtemplate, Orevert,wpswdocument,wpswtemplate,oformat);//===== Associated file =====worddoc.connectto (WordApp.Documents.Item (Docinx)); Method (1) ==> rule table for I: = 1 to WordDoc.Tables.Count do//I table begin//IRow line for IRow: = 1 to WordDoc.Tables.Item (i). Rows.Count do begin//icol column for Icol: = 1 to WordDoc.Tables.Item (i). Columns.count Dobeginmycell:=worddoc.tables.item (i). Cell (Irow,icol); Memo1. Lines.add (MyCell.Range.Text); end;end;end;//method (2) ==> irregular table: For I only when horizontal merge: = 1 to WordDoc.Tables.Count do//I Table Beginfor IRow: = 1 to WordDoc.Tables.Item (i). Rows.Count Dobeginmyrow:=worddoc.tables.item (i). Rows.item (IRow);//IRow line for Icol: = 1 to MyRow.Cells.Count do//icol column beginmycell:= MyRow.Cells.Item (icol); Memo1. Lines.add (MyCell.Range.Text); end;end;end;//method (3) ==> irregular: horizontal, vertical merge; Any table for I: = 1 to WordDoc.Tables.Count do/I table be Gin//J cell for J: = 1 to WordDoc.Tables.Item (i). Range.Cells.CounT Dobeginmycell: = WordDoc.Tables.Item (i). Range.Cells.Item (j); Memo1. Lines.add (MyCell.Range.Text); End;end;finallyif Assigned (WordDoc) then//===== close file =====beginworddoc.close; Worddoc.disconnect; Worddoc.destroy; WordDoc: = Nil;end;if Assigned (WordApp) then//===== off word =====beginwordapp.quit; Wordapp.disconnect; Wordapp.destroy; WordApp: = Nil;end;end;end; 2006-2-15 9:33:25 skillfully use VBA to automatically process Word table http://w ... Source: http://www.52wg.org Update: 12/10/2005 Microsoft Word 97 is a familiar word processing software, powerful features for our work to provide a great help. While the visual Basic for Applications (VBA) app adds a lot of features to Word 97, using VBA properly and appropriately can be a great convenience for users.    Here are some examples of automating Word tables with VBA programming. 1. Create a table, insert text The function of this example is to insert a table with 3 rows and 4 columns at the beginning of the document. Available for Each ... Next structure to iterate through each cell in the table. In For Each ... Next structure, the InsertAfter method is used to add text to the table cell (cell 1th, cell 2nd, and so on), and the Otable.autoformat property is used to specify the table format. Run result 1: @@0869600.jpg; figure [email protected]@ Set odoc = ActiveDocument set otable= oDoc.Tables.Add (Range:=odoc.rang E (start:=0, end:=0), NumRows: =3,numcolumns:=4) ICount = 1 for each ocell in OTable.Range.CelLS oCell.Range.InsertAfter "First" & ICount & "cell" ICount = iCount + 1 Next ocell Otable.autoformat Forma T:=wdtableformatcolorful2,applyborders:=true, App lyfont:=true, Applycolor:=true 2. Insert ordinal 1 in the column or row of the table. If you need to insert an ordinal in the first column of the table, just use the for each ...    The contents of the NEXT structure are changed to the following line, where the InsertAfter method is used to add an ordinal ("line 1th", "line 2nd", and so on) to the table cell. If ICount Mod 4 = 1 then OCell.Range.InsertAfter "First" & (ICOUNT-1)/4 + 1 & "line" End If iCount = Icoun T + 1 2. If you need to insert an ordinal from the second row of the table, change the above code to: if ICount Mod 4 = 1 and iCount > 4 then OCell.Range.InsertAfter "First" & (iCount-1 )/4 & "line" End If iCount = iCount + 1 3. Insert date in table column (1) If you want to insert a date in the first column of the table, you can use the for each ... Next structure to iterate through each cell in the table, inserting a date when judging a cell as the first column. Formart (Date,...)    Used to specify the format of the date, the date in the following example starts with date+1 (that is, the second day of the current date) and the user can customize it as needed.    Set odoc = ActiveDocument Set Otable =odoc.tables.add (Range:=odoc.range (start:=0,end:=0), numrows:= 4,NumColumns:=4) ICount = 1 for each ocell in OTable.Range.Cells If iCount Mod 4 = 1 and ICOunt > 4 then oCell.Range.InsertAfter Format (Date + (iCount-1)/4, "YYYY. Mm. DD ") End if If iCount Mod 4 = 2 and ICount > 4 then OCell.Range.InsertAftercWeekName (WeekDay (date+ (iCount-1 )/4)) End If iCount = iCount + 1 Next ocell Otable.autoformat format:=wdtableformatcolorful1,applyborders:=t Rue, Ap Plyfont:=true, applycolor:=true @@0869601.jpg; fig. [Email protected]@ (2) If you need to insert a week value in the second column of the table, you can do so in the example above ... Next structure insert the following lines: If iCount Mod 4 = 2 and ICount > 4 then OCell.Range.InsertAfter cweekname (WeekDay (Date + (ICount- 1) (4)) End If where WeekDay (Date) returns a value (1~7), respectively, "Sunday" ~ "Saturday", cweekname array needs to be defined in advance: Dim cweekname (7) cweekname (1) = "Sunday" Cweekname (2) = "Monday" ... cweekname (7) = "Saturday" 4. Set different formats according to cell contents For example, if you need to change the row format for all "Saturday" and "Sunday" to a blue background, simply append the following lines after the previous procedure (the table format changes to WDTABLEFORMATCOLORFUL2 and the row number to 12 rows). The program again uses the For each ...    The next structure iterates through each row in the table (rows), and if a row is detected to satisfy the condition ("Saturday" or "Sunday"), select a row (selection.selectrow) and change its properties to the desired format (in this case, a blue background). ICoUNT = 1 For each of the Rows in OTable.Range.Rows If (WeekDay (date + (iCount-1)) = 7 Or WeekDay (date + (iCount-1)) = 1) and ICount > 1 then selection.selectrow with Selection.Cells. Shading. Texture = Wdtexturenone. ForegroundPatternColorIndex = Wdauto. BackgroundPatternColorIndex = Wdblue End With End With end If ICount = iCount + 1 Selection.movedown Uni T:=wdline, count:=1 Next Rows @@0869602.jpg; figure [email protected]@ above briefly describes an example of using VBA to automatically work with a Word table (for Visual Basic events, party See Microsoft Word Visual Basic Help for detailed usage of methods, objects, and properties. Customers can assign Visual Basic code as Word macros to toolbars or shortcuts for later use.

  

Go: Delphi and Office program development--Good to read

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.