Word template creation method and content positioning and Recognition Technology

Source: Internet
Author: User

Http://blog.csdn.net/hcx_2008/archive/2007/09/17/1788589.aspx

Abstract: This paper proposes four production methods for Word templates, discusses in detail the creation and use of each method, and finally compares the advantages and disadvantages of the four methods and their application scenarios, A Protection Method for Word template format is proposed. Keyword word, template, and positioning word not only have unparalleled graphic processing capabilities, but also provide word hierarchical objects, allowing users to automate documents using VBA or third-party software development tools. The development of COM technology makes it easier for the client program to control the server program. The significant increase in hardware speed makes the bottleneck that originally restricted the slow execution speed of COM no longer exist. Therefore, more and more software systems begin to use word as a report output tool, and some even use word as a data entry tool. However, whether word is used as a report output or input tool, it involves the issue of character locating in word. Generally, Word templates are used to locate characters. The template mentioned in this article is an individual word document, which is different from the. Dot suffix template defined in word. There are four methods to create a template: Special Character markup, bookmarkdonefile, table, and domain definition, the following describes the production method and content recognition technology, compares the advantages and disadvantages, and provides solutions to common problems. The following program examples are mainly based on the Delphi language, but also apply to other languages.

1 Special Character MarkupThis method uses uncommon special characters in the template to represent the part to be replaced. For example, the symbol <# Variable>, <#> is a special character, and Variable is the Variable name, used to differentiate different replacement content. The Word template created using this method can only be used for write operations, replacing special characters and variables with specific content. An example of replacement is as follows: FindText: = '<# cgmc>'; // The character MatchCase: = False; MatchWholeWord: = True; MatchWildcards: = False; MatchSoundsLike: = False; MatchAllWordForms: = False; Forward: = True; Wrap: = wdFindContinue; // whether to query the next Format: = False; Replace: = True; MatchKashida: = false; MatchDiacritics: = false; MatchAlefHamza: = false; MatchControl: = false; ReplaceWith: = 'special character markup method'; // Replace the character with WordDocument1.Ran Ge. find dobegin execute (FindText, MatchCase, MatchWholeWord, example, MatchSoundsLike, MatchAllWordForms, Forward, Wrap, Format, Replace, MatchKashida, distance, distance, MatchControl, ReplaceWith); end; all variables defined in the example are OleVariant variables. The above is only an example of processing a special character mark. In this example, you can replace "cgmc" in the query character with a variable to carry out cyclic query replacement. Alternatively, you can replace it with a special character <#> for traversal query, the replacement content can be a mix of characters, images, and even text and text. This can be achieved through the clipboard. 2 BookmarksBookmarks were originally used as a way to quickly return to the original view when Browsing Word documents. This article uses the positioning principle of bookmarks to identify specific areas in the template. The steps for creating a template using this method are as follows: (1) Open the template document and select the region where the bookmarks are to be defined. (2) Select bookmarks from the insert main menu. (3) enter the bookmarks and select Add. A document can define multiple bookmarks. After the bookmarks are defined, they are not displayed in the document body. However, you can use the bookmarks menu of Word to manually find the bookmarks or use code to find the bookmarks. The following is an example of the positioning and read/write operations of Word bookmarks: tmp1: = wdGoToBookmark; tmp2: = 'cgmc '; WordApp. selection. goTo _ (tmp1, EmptyParam, EmptyParam, tmp2); // bookmark to locate Memo1.Lines. add (WordApp. selection. text); // read operation tmp1: = wdGoToBookmark; tmp2: = 'cgmc '; WordApp. selection. goTo _ (tmp1, EmptyParam, EmptyParam, tmp2); // click the marker to locate the WordApp. selection. typeText ('bookmarks' example); // In the write operation example, tmp1 and tmp2 are all OleVariant variables. Of course, you can also use the Count attribute of the BookMarks set to traverse BookMarks. When you use bookmarks to output text in a document, the original content on the bookmarks will be replaced by the new content. 3 Table MethodThe template created using this method is composed of one or more tables. The read/write content is read Based on the cell index. To insert a table, follow these steps: (1) Select Insert \ table from the main menu. (2) enter the number of columns and the number of rows, and click OK. (3) merge or split cells according to the template style. After the template is created, you can hide the table line as needed. The specific read/write example of cell content is as follows: For I: = 1 To WordDoc. Tables. Count do // table I
Begin
For j: = 1 To WordDoc. Tables. Item (I). Range. Cells. Count do // Cell j
Begin
MyCell: = WordDoc. Tables. Item (I). Range. Cells. Item (j );
Memo1.Lines. add (myCell. Range. Text); // read operation
End;
End; For I: = 1 To WordDoc. Tables. Count do // The I table
Begin
For j: = 1 To WordDoc. Tables. Item (I). Range. Cells. Count do // Cell j
Begin
MyCell: = WordDoc. Tables. Item (I). Range. Cells. Item (j );
MyCell. Range. Text: = 'table example'; // read operation
End;
In the example, myCell is the Cell type variable of Word2000. When using the table method to create a template, you can set one or more tables as needed. Regardless of the Two-dimensional Word Table or irregular table in the rule, the cell index number gradually increases from left to right, and the index number of the first cell of each table is 1. 4 Domain definitionThis method uses the domain definition in Word to implement character locating and content recognition. First, define the general framework of the template, and then insert the domain where the domain needs to be defined. Perform the following steps to insert a domain: (1) Select a domain in the insert main menu. (2) Select "document automation. (3) Select "DocVariable" from the domain name ". (4) use the naming Syntax of DocVariabel to define the domain variables in the document. Press Alt + F9 to display or hide the domain definition code. Multiple fields can be defined in a Word template. Variable names are used to differentiate multiple fields. In this example, the domain is defined in the main part of the Word template (except the header and footer ). The Read and Write examples are as follows: For I: = 1 to WordDoc. fields. count dobeginIf WordDoc. fields. item (I ). type _ = wdFieldDocVariable then WordDoc. fields. item (I ). result. text: = 'Use of the domain in the Word document '; // pay the end value For the domain; For I: = 1 to WordDoc. fields. count dobeginIf WordDoc. fields. item (I ). type _ = wdFieldDocVariable thenMemo1.Lines. add (WordDoc. fields. item (I ). result. text): // read the end of all DocVariable domain values. There are 87 types of domain values in the Word document. The detailed definitions can be found in Word_Tlb.Pas in Delphi. Read. When using domain variables, you must determine the domain type. In the above example, the output and reading of the text at the specified position are achieved through the traversal of the domain. Of course, the domain variable name can also be used to identify the domain. The specific method is to extract the domain definition string using the Code attribute of the domain, and then search for the domain variable in the domain definition string. Note: When assigning values to a domain, the original text in the domain will not be replaced by the new value, but will be added after the original text in the domain. In the above four methods, except for the special character mark method, only Word documents can be generated. The other three methods can be used to generate WORD Documents and read the content of Word documents created using templates. However, for the use of Word documents as the system manual input interface, the Bookmarks can be easily deleted by users by mistake, and the domain definition method is hard to be recognized by the user's eyes, even if it is identified, manual input is also very troublesome. In practical applications, the template files created by the table method are generally relatively stable in format. It is difficult for users to delete them by mistake unless they are interested. Therefore, compared with the other three methods, the template created by table method is more competent to create the input interface. A template file is a Word document with a special format, so it is easy to modify the format. To prevent the template format from being corrupted. You can set a password for the Word template. In actual use, enter the password manually or in the program, enter the password in the WordDoc. UnProtect method, and then open the template file for operations.

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.