WPCubed Product Support

Jpeg from database in mail merge field

I have difficulty getting a jpeg stored in a database field into a mail merge field. You can download my sample from here: [](http://www.niftybits.de/downloads/MailMergeGraphic.zip)

Basically the code is like that:

unit Unit1;interfaceuses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  Dialogs, StdCtrls, WPRTEDefs, WPCTRMemo, WPCTRRich, FIBDatabase,  pFIBDatabase, DB, FIBDataSet, pFIBDataSet, Wpdbrich, WPOBJ_Image;type  TForm1 = class(TForm)    pFIBDatabase1: TpFIBDatabase;    pFIBTransaction1: TpFIBTransaction;    Button1: TButton;    Button2: TButton;    qry: TpFIBDataSet;    WPMMDataProvider1: TWPMMDataProvider;    DataSource1: TDataSource;    lAllRichText: TWPRichText;    lTemplate: TWPRichText;    procedure Button1Click(Sender: TObject);    procedure Button2Click(Sender: TObject);    procedure lTemplateMailMergeGetText(Sender: TObject;      const inspname: String; Contents: TWPMMInsertTextContents);  private    { Private-Deklarationen }  public    { Public-Deklarationen }  end;var  Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);begin  lTemplate.InputMergeField( 'SIGNATURE_PIC','SIGNATURE_PIC' );end;procedure TForm1.Button2Click(Sender: TObject);var  i: Integer;  lSection: TWPRTFSectionProps;begin  lTemplate.BeginUpdate;  qry.Open;  qry.First;  try    i := 0;    lAllRichText.BeginUpdate;    while not qry.Eof do    begin      lTemplate.MergeText;      if i=0 then // FIRST RUN      begin         lAllRichText.AsString := lTemplate.AsString;         lAllRichText.CPPosition := MaxInt; // to end      end      else if i>0 then // SUBSEQUENT RUNS      begin         // Append a new paragraph if the last line is not empty         if lAllRichText.ActivePosInPar>0 then             lAllRichText.InputString(#13);         // Need page break         lAllRichText.FastSetPageBreak(true, true);         // and append the text         lSection        := lAllRichText.FastAppendText(lTemplate,true);         lSection.Select := [wpsec_ResetOutlineNums];      end;      qry.Next;      Inc(i);    end;  finally    lTemplate.EndUpdate;  end;end;procedure TForm1.lTemplateMailMergeGetText(Sender: TObject;  const inspname: String; Contents: TWPMMInsertTextContents);var  lWpObj: TWPOImage;  lStream: TMemoryStream;begin  if inspname = 'SIGNATURE_PIC' then  begin    if Contents.CurrentObject=nil then    begin      lWpObj := TWPOImage.Create( lTemplate );      lStream := TMemoryStream.Create;      try        TBlobField( qry.FN('SIGNATURE_PIC')).SaveToStream( lStream );        lStream.Position := 0;        lWpObj.FileExtension := 'JPG';        lWpObj.LoadFromStream(lStream);        lWPObj.WidthTW := lWpObj.ContentsWidth;        lWpObj.HeightTW := lWpObj.ContentsHeight;        Contents.Obj := lWpObj;      except        ;      end;      lStream.Free;      lWpObj.Free;    end;  end;end;end.

What happens is that nothing is displayed just «».
When I assign

Contents.StringValue:='xxx';

then the text is displayed correctly. What am I doing wrong?

Regards,
Dirk.

#1 ♥ 0
2.4k Views 1 links 2 users read 2 min
support

Hi,

if you use the data provide please also use its OnGetTextToInsert event.

There you can set the parameter "done" to true to abort further processing.

Since after the first run CurrentObject will be <>nil you will have to load your data into this object on subsequent runs.

Or remove the line
if Contents.CurrentObject=nil then

Julian

#2 ♥ 0
This category is archived. No new posts are possible.