A function in a database application recently written in Delphi 7 is to export the query results to a Word document and save them. Although the servers panel of Delphi 7 provides the twordapplication and tworddocument components, there is almost no way to write them in help. According to a lot of related articles on the Chinese journal, it is a pity that almost all of them are written in Delphi 5 (not compatible in Delphi 7), and they are just a brief introduction, very depressing. After a day of exploration, we finally used Delphi 7 to implement this function. The Code is as follows:
// Uses word2000, comobj;
// Wordapp: twordapplication;
// Worddoc: tworddocument;
Procedure tfrmdetails. btnexportclick (Sender: tobject); // click the export button
VaR
V: variant;
Template, newtemplate, documenttype, visible: olevariant;
Itemindex: olevariant;
Filename: olevariant;
Noprompt, originalformat: olevariant;
Routedocument, savechanges: olevariant;
Begin
// Specify the document path and file name
Filename: = 'C:/logadmin/doc/'{'duty schedule '{trim(dbtextid.caption=}'.doc ';
// If the corresponding word document of the log already exists, the system prompts whether to overwrite the log.
If fileexists (filename) = true then
Begin
Beep;
If application. MessageBox ('the document already exists. Do you want to overwrite it? ', 'Warning', mb_okcancel) = idcancel then
Abort;
End;
// Test whether Word 2000 is currently running
Try
V: = getactiveoleobject ('word. application ');
Except
// Run it if it is not running
V: = createoleobject ('word. Basic ');
End;
Try
// Connect to Word 2000
Wordapp. Connect;
Except
Beep;
Messagedlg ('the document cannot be generated. Check whether Word 2000 is installed! ', Mterror, [mbok], 0 );
Abort;
End;
// Display Word 2000
Wordapp. Visible: = true;
// Assign values to the real parameters used to call the Add function
Template: = emptyparam;
Newtemplate: = false;
Documenttype: = wdnewblankdocument;
Visible: = true;
// Call the Add function
Wordapp. Documents. Add (template, newtemplate, documenttype, visible );
// Connect to the new document
Itemindex: = 1;
Worddoc. connectto (wordapp. Documents. Item (itemindex ));
// Save the document
Worddoc. saveas (filename );
// Start writing content to the Word Document
With wordapp. selection do
Begin
Font. Size: = 20;
Font. Bold: = 2;
Paragraphs. Alignment: = wdalignparagraphcenter;
Typetext ('detailed duty log content ');
Typeparagraph; // line feed
Typeparagraph;
Font. Size: = 12;
Font. Bold: = 0;
Paragraphs. Alignment: = wdalignparagraphleft;
Typetext ('No.:' + dbtextid. Caption );
Typeparagraph;
Typetext ('date: '+ dbtextdate. Caption );
Typeparagraph;
Typetext ('temperature: '+ dbtextt. Caption );
Typeparagraph;
Typetext ('humidity: '+ dbtexth. Caption );
Typeparagraph;
Typetext ('Weather: '+ dbtextweather. Caption );
Typeparagraph;
Typetext ('person on duty: '+ dbtextname. Caption );
Typeparagraph;
Typetext ('duty time: '+ dbtexttime. Caption );
Typeparagraph;
Typetext ('with or without exceptions: '+ lbexception. Caption );
Typeparagraph;
Typetext ('tool used :');
Typeparagraph;
Typetext (dbmemotool. Text );
Typeparagraph;
Typetext ('site environment :');
Typeparagraph;
Typetext (dbmemoenv. Text );
Typeparagraph;
Typetext ('record 1 :');
Typeparagraph;
Typetext (dbmemor1.text );
Typeparagraph;
Typetext ('record 2 :');
Typeparagraph;
Typetext (dbmemor2.text );
Typeparagraph;
Typetext ('record 3 :');
Typeparagraph;
Typetext (dbmemor3.text );
Typeparagraph;
Typetext ('note :');
Typeparagraph;
Typetext (dbmemomemo. Text );
Typeparagraph;
End;
// Save the document
Noprompt: = false;
Originalformat: = wdoriginaldocumentformat;
Wordapp. Documents. Save (noprompt, originalformat );
// Close the document
Savechanges: = wdsavechanges;
Originalformat: = wdoriginaldocumentformat;
Routedocument: = false;
Wordapp. Documents. Close (savechanges, originalformat, routedocument );
// Disconnect from Word 2000
Wordapp. Disconnect;
Messagedlg ('Log content exported successfully! Save as '+ filename, mtinformation, [mbok], 0 );
// Close the form
Frmdetails. close;
End;