Mega Code Archive

 
Categories / Delphi / Examples
 

Output a DB Grid to Microsoft Word

Title: Output a DB Grid to Microsoft Word Question: I finally figured out how to output the results of a DB Grid to Microsoft Word. The form uses multiple edit boxes to accept user input to search the database for the fields the user wants to search on. The DBgrid is then populated with the fields and I then use a control button to view the results in MS Word. I hope this is helpful. Answer: procedure TUserDefinedDentalCriteria.Button4Click(Sender: TObject); var i, RecNo, numchoices : integer; slist : TStringList; RecChange : Boolean; apfile, sline : String; WordApp : Variant; begin slist := TStringList.Create; RecNo := 1; RecChange := False; apfile := ' Postmortem File'; numchoices := 4; slist.Add(chr(9)+chr(9)+chr(9)+chr(9)+chr(9)+('DENTAL QUERY')); slist.Add(chr(32)); if Edit7.Text = '' then numchoices := numchoices - 1; if Edit5.Text = '' then numchoices := numchoices - 1; if Edit3.Text = '' then numchoices := numchoices - 1; case numchoices of 1: begin slist.Add(chr(9)+chr(9)+chr(9)+'************************************'); slist.Add(chr(9)+chr(9)+chr(9)+'* Your search for: ' + edit2.Text + ' on tooth #' + edit1.Text + ' *'); slist.Add(chr(9)+chr(9)+chr(9)+'* Returned the following results *'); slist.Add(chr(9)+chr(9)+chr(9)+'************************************'); end; 2: begin slist.Add(chr(9)+chr(9)+chr(9)+'**************************************'); slist.Add(chr(9)+chr(9)+chr(9)+'* Your search for: ' + edit2.Text + ' on tooth #' + edit1.Text + ' *'); slist.Add(chr(9)+chr(9)+chr(9)+'* and: ' + edit4.Text + ' on tooth #' + edit3.Text + ' *'); slist.Add(chr(9)+chr(9)+chr(9)+'* Returned the following results *'); slist.Add(chr(9)+chr(9)+chr(9)+'**************************************'); end; 3: begin slist.Add(chr(9)+chr(9)+chr(9)+'************************************'); slist.Add(chr(9)+chr(9)+chr(9)+'* Your search for: ' + edit2.Text + ' on tooth #' + edit1.Text + ' *'); slist.Add(chr(9)+chr(9)+chr(9)+'* and: ' + edit4.Text + ' on tooth #' + edit3.Text + ' *'); slist.Add(chr(9)+chr(9)+chr(9)+'* and: ' + edit6.Text + ' on tooth #' + edit5.Text + ' *'); slist.Add(chr(9)+chr(9)+chr(9)+'* Returned the following results *' ); slist.Add(chr(9)+chr(9)+chr(9)+'************************************'); end; 4: begin slist.Add(chr(9)+chr(9)+chr(9)+'************************************'); slist.Add(chr(9)+chr(9)+chr(9)+'* Your search for: ' + edit2.Text + ' on tooth #' + edit1.Text + ' *'); slist.Add(chr(9)+chr(9)+chr(9)+'* and: ' + edit4.Text + ' on tooth #' + edit3.Text + ' *'); slist.Add(chr(9)+chr(9)+chr(9)+'* and: ' + edit6.Text + ' on tooth #' + edit5.Text + ' *'); slist.Add(chr(9)+chr(9)+chr(9)+'* and: ' + edit8.Text + ' on tooth #' + edit7.Text + ' *'); slist.Add(chr(9)+chr(9)+chr(9)+'* Returned the following results *'); slist.Add(chr(9)+chr(9)+chr(9)+'************************************'); end; end; slist.Add(chr(32)); slist.Add(chr(9)+chr(9)+chr(9)+chr(9)+chr(9) + 'Record #' + IntToStr(RecNo)); slist.Add(apfile); while not DBGrid1.DataSource.DataSet.Eof do begin if RecChange = True then begin RecNo := RecNo + 1; slist.Add(chr(9)+chr(9)+chr(9)+chr(9)+chr(9) + 'Record #' + IntToStr(RecNo)); slist.Add(apfile); RecChange := False; end; for i := 0 to DBGrid1.FieldCount - 1 do begin if (i 3) and (i //TColumn in the DBGrid used to separate //two files, therefore I don't want to use it begin sline := sline + (DBGrid1.Columns[i].Title.Caption + ': ' + DBGrid1.Fields[i].Text +chr(9)+chr(9)+chr(9)); end else begin if i 3 then begin sline := sline + (DBGrid1.Columns[i].Title.Caption + ': ' + DBGrid1.Fields[i].Text +chr(9)+chr(9)+chr(9)); end else begin slist.Add(sline); sline := ''; slist.Add(chr(32)); apfile := ' Antemortem File'; slist.Add(apfile); apfile := ' Postmortem File'; end; end; if i = DBGrid1.FieldCount - 1 then begin slist.Add(sline); sline := ''; slist.Add(chr(32)); slist.Add('-------------------------------------------------------------------'); slist.Add(chr(32)); DBGrid1.DataSource.DataSet.Next; RecChange := True; end; end; slist.SaveToFile('QueryData.doc'); end; WordApp := CreateOleObject('Word.Application'); WordApp.Documents.Open('QueryData'); WordApp.Visible := True; end; end. The Results of the above code looks like this: NOTE: The data displayed is test data and it represents exact matches between two files in the database based on the users input request. DENTAL QUERY ************************************** * Your search for: m on tooth 1 * * and: vp on tooth #2 * * Returned the following results * ************************************** Record #1 Postmortem File SSN: 001-11-0001 Name: Grafitti Disaster #: Antemortem File SSN: 001-01-0101 Name: Disaster #: ------------------------------------------------------------------- Record #2 Postmortem File SSN: 001-11-0001 Name: Grafitti Disaster #: Antemortem File SSN: 102-16-5211 Name: Butts Disaster #: ------------------------------------------------------------------- Record #3 Postmortem File SSN: 001-11-0001 Name: Grafitti Disaster #: Antemortem File SSN: 818-90-2134 Name: Fry Disaster #: ------------------------------------------------------------------- Record #4 Postmortem File SSN: 2PB0001 Name: Fry Disaster #: 2PB0001 Antemortem File SSN: 001-01-0101 Name: Disaster #: