1 (edited by CDB 2020-11-13 09:31:53)

Topic: [SOLVED]Tablegrid contents to Email

I have a need to take selected items from a table grid and place them in the body of an email.

The code below using a stringlist works to an extent, however whilst my test 'showMessage' does show what I want, by the time it gets to an email body only the first two items ever show.

I could copy to clipboard and then manually paste into the email body, but that is unsatisfactory as I hope to make this automatic with the user only having to press 'send' in their email client.

My current code is

procedure frmEmail_SendEmail_OnClick (Sender: TObject; var Cancel: boolean);
const

  CRFL = #13#10;
var
  emailList : TStringlist;

  indx,colmn : integer;

begin
     emailList:= TStringList.Create;
     emailList.Clear;

     for indx := 0 to frmEmail.tbgEmailList.RowCount -1  do
     begin

         for colmn := 0 to frmEmail.tbgEmailList.Columns.Count -1 do
        begin
         emailList.add(frmEmail.tbgEmailList.Cells[colmn,indx]);

        end;
        emailList.add(CRFL);
     end;
    // try

        //st := clipboardGet;


      openURL('mailto:'+frmEmail.cmbEmailAddress.Text+'?Subject='+frmEmail.cmbSupplierList.Text+' Order'+'&Body='+ emailList.text);
      emailList.free;
     // SQLExecute('UPDATE orders SET orderEmailed = 1 WHERE orders.id =');
end;
On a clear disk you can seek forever

Re: [SOLVED]Tablegrid contents to Email

In case anyone else would like to know how to send emails where you can't access the information needed to use the SendMail function, here is how I've managed to send an email using Outlook.

This code has been scrounged from StackOverflow where it was scrounged from somewhere else.

procedure Form1_SendEmail_OnClick (Sender: TObject; var Cancel: boolean);
const
  objMailItem = 0;
var
  OKToUse: boolean;
  Outlook: Variant;
  vMailItem: variant;
begin
 // OKToUse := false;


      Outlook := CreateOleObject('Outlook.Application');
    
    vMailItem := Outlook.GetNamespace('MAPI');
    vMailItem.Logon;  {[color=blue]this allows an email to be sent if Outlook is not open at the time of sending[/color]}
    vMailItem := Outlook.CreateItem(objMailItem);
    vMailItem.Recipients.Add('someperson@someplace.something');
    vMailItem.Subject := 'What a wonderful test email'; {[color=blue]this could be a text box with the string contained in it[/color]}
    vMailItem.Body := 'another string or stringlist with the body text';
    vMailItem.Send;
  

  Outlook := nil;  {[color=blue]free the object - this might not be necessary, some sources say it automatically frees when out of scope[/color]}
end;
On a clear disk you can seek forever

Re: [SOLVED]Tablegrid contents to Email

Thank you very much CDB for posting your solution! I've been keeping an eye on this topic, this is something i've also been trying to do since it would be extremely timesaving and helpful in my little work projects, i'm going to try it out over the weekend. Do you have any idea if it would be possible to style the body, or perhaps even open an outlook email template?

Re: [SOLVED]Tablegrid contents to Email

Hi dbk,

First I should note that using the above code you never see your email message, by that I mean the email is created and sent 'behind the scenes', unlike using mailto: or openfile(mailto:) which opens the email create page.

I don't know the answer to the rest of your question. I do know that the Outlook object has a number of item folders that can be searched such as contacts, calendar, email folders.

This link to Microsoft might help https://docs.microsoft.com/en-us/previo … office.11)

or

http://delphi-kb.blogspot.com/2011/06/o … _6280.html

On a clear disk you can seek forever

Re: [SOLVED]Tablegrid contents to Email

Hello CDB, thank you for your answer and supplying extra info.

I've experimented with your code a bit, following code will open Outlook and prepare a new email message to review and then press 'send', which is what i was trying to achieve. Now i'm looking into a way to style the body.


procedure Form1_SendEmail_OnClick (Sender: TObject; var Cancel: boolean);
const
  objMailItem = 0;
var
  OKToUse: boolean;
  Outlook: Variant;
  vMailItem: variant;
begin
 // OKToUse := false;

    Outlook := CreateOleObject('Outlook.Application');
    
    vMailItem := Outlook.GetNamespace('MAPI');
    vMailItem.Logon;  {[color=blue]this allows an email to be sent if Outlook is not open at the time of sending[/color]}
    vMailItem := Outlook.CreateItem(objMailItem);
    vMailItem.Recipients.Add('someperson@someplace.something');
    vMailItem.Subject := 'What a wonderful test email'; {[color=blue]this could be a text box with the string contained in it[/color]}
    vMailItem.Body := 'another string or stringlist with the body text';
    vMailItem.Display;

  Outlook := nil;  {[color=blue]free the object - this might not be necessary, some sources say it automatically frees when out of scope[/color]}
end;

Re: [SOLVED]Tablegrid contents to Email

OpenFile('mailto:'+ Form1.Edit6.text + '?subject='+ 'His work is ready' + '&body=' +'Spett. Sig./Sig.ra ' + Form1.ComboBox4.text + '%0D%0A%0D%0A' + 'His work Cod. '+ Form1.Edit7.text + ' Yes ready! %0D%0A%0D%0A ' + messaggi.Edit2.Text +  '%0D%0A' + '%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A' + messaggi.Edit3.Text );


%0D serves to break a line

Domebil