I first want to explain that this article is what I used to write "using Delphi 7 to control Word 2000 generation document" http://blog.9cbs.net/neowang/archive/2004/10/08/127522.aspx Continued (so far, this article has been viewed 214 11 times). Because the user's needs have changed, I have to extrude the time to rewrite the source program to meet the new needs in the review of the natural dialectics of the review, and see it on the face of the old classmates. On the other hand, I don't want to take a Christmas. The valuable time of the festival). The original duty log is the output of the direct line to a blank document (you can also see if it is output to new document created by Normal.dot as template). This time I sent me a time. Word painted table, requiring the output of the program to be inserted into the blank position corresponding to the table. This form is not a standard M × N form, but the consolidated cell processing, which increases the difficulty. I originally intended to manipulate the objects in VBA to draw an identical form, but I tried it later, I found that this method was very complicated, the workload was large, and I gave up in the middle. At this time, I think of the document that conversion to the classmates into a template (file> new> template, then copy the form, save as "duty log list. DOT", so that one of the same form is, next Just insert the corresponding content. Of course, there must be some corresponding modifications in the program: 1) When calling the add method of the Documents collection, the Template parameter no longer uses the EMPTYPARAM value, but is specified as "duty log table .dot", which adds the document will "Duty log list. DOT" is template; 2) When inserting the corresponding content, I am using a relatively stupid method, but for this irregular table, it may be the most effective way: calculate the table. There are multiple paragraphs in the CCC, and then insert content in the paragraphs that should be inserted into the content through statements. I also lazy yourself, define a integer variable paracount, assign it to WordDoc.Paragraphs.count, and finally displayed with a message box, get this form 38 sections, finally comment out the above code :) 3) The down work is the number, the number of pieces of the insertion content, write a code to insert the corresponding content. For example: "Duty Time" (header) is paragraph 6, which is next to the blank paragraph that fills the content is paragraph 7, so writes the code in step 7 into the actual content. Finally, I want to say that a help file in Word 2000, VBAWRD9.CHM, detailing the VBA used in Word 2000 in detail, introduces a lot of useful objects, attributes, and methods, and some VB code As an example, it is easy to think of the corresponding Delphi code.
This file can be found in the installation disc of Office 2000, forgot, please search:) // Click "Export to Word Document" button Event Handler Procedure TFRMDetails.btnexportClick (Sender: Tobject); var variant; // Word variants representative of the type of variable Template, NewTemplate, DocumentType, Visible: OleVariant; //Documents.Add () method of itemIndex parameters: OleVariant; fileName: Olevariant; noPrompt, OriginalFormat: OleVariant; RouteDocument, SaveChanges: OleVariant; // paracount: integer; begin // Specify the path and file name filename: = 'c: / logadmin / doc /' 'Duty log' Trim (dbtextdate.caption) '.doc'; // If The corresponding Word document of the log already exists whether it is prompted to override if FileExists (filename) = true dam, if Application.MessageBox ('document already exists, whether it is overwriting?', 'Warning', mb_okcancel) = IDCANCANCANCEL THEN ABORT; END; // Test if the current 2000 try v: = getActiveoleObject ('Word.Application'); Except // is not running V: = CreateoleObject ('word.basic'); end; try // Connect to Word 2000 Wordapp.connect; Except Beep; Messagedlg ('Can't generate documents, please confirm if Word 2000!', MTerror, [Mbok], 0); Abort; end; // Display Word 2000 Wordapp.Visible: = true; // Assign the value to the actors used by the Add function // template: = EmptyParam; // Using blank documents, "NORMAL.DOT" template template: = 'duty log table .dot'; // use "Duty Logs. DOT" template newTemplate: = false; DocumentType: = wdNewBlankDocument; Visible: = true; // call the Add function adds a document WordApp.Documents.Add (Template, NewTemplate, DocumentType, Visible); // connected to the new document itemIndex: = 1; WordDoc.ConnectTo (WordApp.Documents. Item (ItemIndex); // Document Save As WordDoc.saves (FileName); // Write the content to the Word document // paracount: = WordDoc.Paragraphs.count; // Search the total number of paragraphs in the template 38 / / Messagedlg (IntenTR (PARCOUNT), Mtinformation, [Mbok], 0);
// Insert a duty date worddoc.paragraphs.Item (3) in paragraph 3.Range.insertafter (dbtextdate.caption); // Insert a duty name wordDoc.Paragraphs.Item (5). Range in paragraph 5. INSERTAFTER (DBTextName.caption); // Insert a duty time in the duty Time WordDoc.Paragraphs.Item (7) .range.CAption; // Insert temperature WordDoc.Paragraphs.Item in paragraph 10 ( 10) .Range.insert.caption; // Insert the humidity worddoc.paragraphs.Item (12) in paragraph 12.Range.InsertAfter (dbtexth.caption); // Insert the weather WordDoc in paragraph 14. Paragraphs.Item (14) .range.insertafter (dbtextWeather.caption); // Insert tool worddoc.paragraphs.Item (17) in paragraph 17.Range.InsertAfter (dbmemotool.text); // in paragraph 19 Insert the scene worddoc.paragraphs.Item (19) .Range.insertafter (dbmemoenv.text); // Insert an abnormality worddoc.Paragraphs.Item (21) .Range.insertAfter in paragraph 21; / Insert a recorded worddoc.paragraphs.Item (26) .Range.insertAfter (DBMEMOR1.TEXT) in paragraph 26; // Insert a record in the 29th WordDoc.Paragraphs.Item (29) .Range.insertafter DBMEMOR2.TEXT); // Insert the record three WordDoc.Paragraphs.Item (32) in paragraph 33.Range.InsertAfter; // Insert Note WordDoc.Paragraphs.i in paragraph 36 Tem (36) .Range.insertafter (DBMemMemo.text); // Save document NOPROMPT: = false; OriginalFormat: = WordApp.Documents.save (noPrompt, OriginalFormat);
// Close Document SaveChanges: = wdSaveChanges; OriginalFormat: = wdOriginalDocumentFormat; RouteDocument: = false; WordApp.Documents.Close (SaveChanges, OriginalFormat, RouteDocument); // Word disconnection and connection of WordApp.Disconnect 2000;
Messagedlg ('log content export success! Save as' filename, mtinformation, [mbok], 0); frmDetails.close; end; finally completed the program before 1 hour, I can also feel at ease and pass, I wish my own Christmas first, I wish you all a Merry Christmas!