Use Delphi 7 to control the method of generating documents in Word 2000

xiaoxiao2021-03-06  52

Recently, one of the functions written in a database application written by Delphi 7 is to export the results of the query to a Word document. Although TWordApplication and TwordDocument components are provided in the Servers panel of Delphi 7, but in the help, almost no use of them. So in China's journal online Down's related articles, only a pity is almost written in Delphi 5 (can not be compatible in Delphi 7), and it is just a simple introduction, it is very depressed. This function is finally realized with Delphi 7 after a day of exploration.

The following code: // uses Word2000, ComObj; // WordApp: TWordApplication; // WordDoc: TWordDocument; procedure TfrmDetails.btnExportClick (Sender: TObject); // click on the "Export" button var V: Variant; Template, NewTemplate, DocumentType , Visible: OleVariant; itemIndex: OleVariant; fileName: Olevariant; noPrompt, OriginalFormat: OleVariant; RouteDocument, SaveChanges: OleVariant; path and filename specified document fileName begin //: = 'C: / LogAdmin / doc /' 'duty Log ' Trim (DBTextid.caption) '. Doc '; // If the log of the log already prompts if you override if FileExists (filename) = true kilin beep; if Application.MessageBox (' document already exists Is it overwriting? ',' Warning ', MB_OKCANCEL) = IDCANCEL THEN ABORT; END; // Test whether the current 2000 try v: = getActiveoleObject (' Word.Application '); Except // Unlike 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; // Template assignments used for the call us: = EmptyParam; newTemplate: = false; DocumentType : = WdNewBlankDocument; Visible: = true; // Add the function call WordApp.Documents.Add (Template, NewTemplate, DocumentType, Visible); // connected to the new document itemIndex: = 1; WordDoc.ConnectTo (WordApp.Documents.Item (ItemIndex); // Document Save Save WordDoc.saves (FileName); // Start writing content with WITH WITH WID.SIZE: = 20; font.bold: = 2; paragraphone.Alignment : = wdalignparagraphCenter; Typetext ('duty log details'); typeparagraph; // wrap TypeParagramRapH; font.size: = 12; font.bold: = 0; paragrarignParaGraphleft; 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 ( 'on duty:' DBTextName.Caption); TypeParagraph; TypeText ( 'duty time:' DBTextTime.Caption); TypeParagraph; TypeText ( ' without exception: ' lbException.Caption); TypeParagraph; TypeText (' tools: '); TypeParagraph; TypeText (DBMemoTool.Text); TypeParagraph; TypeText (' field environment: '); TypeParagraph; TypeText (DBMemoEnv.Text) ; TypeParagraph; TypeText ( 'a record:'); TypeParagraph; TypeText (DBMemoR1.Text); TypeParagraph; TypeText ( 'two record:'); TypeParagraph; TypeText (DBMemoR2.Text); TypeParagraph; TypeText ( 'three record:' ); 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 and Word 2000 connection Wordapp.disconnect;

转载请注明原文地址:https://www.9cbs.com/read-109675.html

New Post(0)