How do I write the contents of the database into the Word document? (Chen Yong) huguangtao (original) Keyword database, Word, OLE
When programming, because Microsoft Word's editing is very powerful, we often want to save some data as a Word document so that we have professional editing. When saving as a Word document, we can pass OLE, but often encounter to transfer the real Word Editing interface to the program so that the program interface is confusing. Also, ordinary calling words must specify the absolute address of the Word executable file, because many users are not consistent with the path to the Word installed, so we often encounter the problem of calling errors, can there be a way to call Word will not It shows that it is independent of version, installed paths? The answer is yes. Below, I will come to a small program to give you a demonstration, only playing a role of a throwing jade. For friends for reference only. First, we build an access2000 database data.mdb, we create a table UserData in the library, there is a "work number", "name", "gender", "department" four fields, all of which are characters, and then enter Some data, we build a form FORM1, in Form1, place the following controls: Control Name Property Value TLABEL1 CAPTION "Please enter the file name" TEDIT1 NAME "" to enter the file name, TButton1 caption with the extension "save "TBUTTON2 CAPTION" Exit "ADOTABLE1 ACTIVE
Connectionstring
TableName TrueProvider = Microsoft.jet.OLDB.4.0; Data Source = DATA.MDB; PERSIST Security Info = FalseUserData
Below is the related code: file: // --------------------------------------- ----------------------------------
#include
#include "unit1.h" ------------------------------------------- -------------------------------- # pragma package (smart_init) #pragma resource "* .dfm" TFORM1 * FORM1; -------------------------------------------------- -------------------------__ fastcall tform1 :: tForm1 (tComponent * Owner): TFORM (OWNER) {} -------- -------------------------------------------------- -----------------
void __fastcall TForm1 :: Button1Click (TObject * Sender) {Variant OLEObject; AnsiString dataword; if (Trim (Edit1-> Text) == "") {ShowMessage ( "Please enter the file name"); return;} String currentPath = GetCurrentDir () "//" Trim (edit1-> text); oleobject = creteoleObject ("word.basic"); oleobject.exec ("filenew")); oleobject.exec (Procedure ("endofdocument")) ; Adotable1-> first (); while (adotable1-> eof! = True) {dataword = adotable1-> FieldValues ["IDO"] adotable1-> FieldValues ["Name"] adotable1-> FieldValues ["Gender" ] Adotable1-> fieldvalues ["department"]; oleobject.exec ("insert") << DataWord; adotable1-> next (); oleobject.exec (Procedure ("StartofDocument"); oleObject.exec ("Filesaveas") << currentpath; oleobject.exec ("fileclose") << 1); showMessage ("file has been written! Thank you!");} ---------- -------------------------------------------------- ----------------- void __fastcall tform1 :: butt2click (Tobject * sender) {this-> close ();} ------------- -------------------------------------------------- ------------ Void __fastcall tform1 :: edit1click (TOBJECT * Sender) {edit1-> text = "";