trichview.com

trichview.support




Re: mail merge a table with a variable number of rows


Return to index


Author

Message

Robert Baker

Posted: 02/14/2005 21:51:40


> Which code do you already use to replace a field with its value?


procedure TfrmMail.FillFields(RVData: TCustomRVData);

var i,j,r,c: Integer;

    table: TRVTableItemInfo;

    FieldName, FieldValue: String;

    s: String;

    FieldEnd: Integer;

    Changed: Boolean;


    function GetFieldValueFromDatabase(const FieldName: String): String;

    begin

      try

        if FieldName = '_TodaysDate'

          then Result := DatetoStr(date)

        else result :=

myDataSource.DataTable.FieldByName(FieldName).AsString;

      except

        Result := '{error: illegal field code}';

      end;

    end;

begin

  for i := 0 to RVData.ItemCount-1 do

    if RVData.GetItemStyle(i)=rvsTable then begin

      table := TRVTableItemInfo(RVData.GetItem(i));

      for r := 0 to table.Rows.Count-1 do

        for c := 0 to table.Rows[r].Count-1 do

          if table.Cells[r,c]<>nil then

            FillFields(table.Cells[r,c].GetRVData);

      end

    else if RVData.GetItemStyle(i) >= 0 then begin

      s := RVData.GetItemTextA(i);

      FieldEnd := 0;

      Changed := False;

      for j := Length(s) downto 1 do begin

        if s[j]='}' then

          FieldEnd := j

        else if (s[j]='{') and (FieldEnd > 0) then begin

          FieldName :=  Copy(s, j+1, FieldEnd-j-1);

          FieldValue := GetFieldValueFromDatabase(FieldName);

          Delete(s, j, FieldEnd-j+1);

          Insert(FieldValue, s, j);

          FieldEnd := 0;

          Changed := True;

        end;

      end;

      if Changed then

        RVData.SetItemTextA(i, s);

    end;

end;





Powered by ABC Amber Outlook Express Converter