views:

1494

answers:

3

I have a DBGrid on a form and I have made multiple selections, I now need to send the selected cells (they are email addresses) to the "TO Box" of Outlook how can I do this, I will appreciate any help ( Delphi5) Thanks in advance

+3  A: 

To get list of selected E-Mails you can use this procedure. For outlook you might want to use shellexec and mailto: or use API if there's any.

var
i: Integer;
S: TStringList;
begin
S:=TStringList.Create;
if DBGrid1.SelectedRows.Count > 0 then
begin
for i:=0 to DBGrid1.SelectedRows.Count-1 do
begin
Table1.GotoBookmark(pointer(DBGrid1.SelectedRows[i]));
S.Add(Table1EMail.AsString);
end;
//Outlook procedure goes here 
end;

S.Free;
end;
Ertugrul Tamer Kara
A: 

Every (almost) control in Windows is a window itself. It has got its class and instance name. Since the construction of every MailTo window in each mail client remains the same, after gaining knowledge how to find appropriate control a solution can be built.
This is where Spy++ from Visual Studio comes in handy (if you do not have it, try to find some similar tool, there is a freeware version at http://msdn.microsoft.com/pl-pl/magazine/cc163617(en-us).aspx but it lacks cool searching tool).
So, After starting Spy++ and mail program, we hit “New mail” and mailing window will appear. Refresh in Spy++, and use “Find window” tool – click on your TO list, and you will se how it is built.
I started with Outlook Express. The mail window is of class ATH_Note, then inside address area is a window of class OE_Envelope and inside this window there are several windows, some of them are of class RichEdit20W. The “To” field is the first one.

procedure UpdateToOE;
var
  Window:Thandle;
Text:PChar;
begin
  {Lets find Mail window}
  Window:=FindWindow('ATHNote',nil);
  if (Window = 0) then Exit;
  {Lets find adress area inside}
  Window:= FindWindowEx(Window,0,'OEEnvelope',nil);
  if (Window = 0) then Exit;
  {Lets find TO field - remeber this is the first field of this class}
  Window:= FindWindowEx(Window,0,'RichEdit20W',nil);
  if (Window = 0) then Exit;
  {Prepare text into PChar}
  Text:='[email protected]';
  {Send message WMSETTEXT which will set our text in control}
  SendMessage(Window,WMSETTEXT,0,Integer(Text));

  {Sending one extra space to prevent OE does not notice - answer to grzegorz's question}
  SendMessage(Window,WM_CHAR,32,1);
  //done!
End;


Note: FindWindowEx when second param is 0 will always search for FIRST in the row – so, but if you will do sth like this:

Window:=FindWindow('ATH_Note',nil);<br>
if (Window = 0) then Exit;<br>
Window:= FindWindowEx(Window,0,'OE_Envelope',nil);<br>
if (Window = 0) then Exit;<br>
Sibling:= FindWindowEx(Window,0,'RichEdit20W',nil);<br>
if (Sibling = 0) then Exit;<br>
Window:=FindWindowEx(Window, Sibling, 'RichEdit20W',nil);<br>
if (Window = 0) then Exit;<br>
Text:='[email protected]';<br>
SendMessage(Window,WM_SETTEXT,0,Integer(Text));<br>

The text will be put in a SECOND edit field. See msdn for FindWindowEx.

So, this is good for OE (XP SP3 IE7). But what with MS Outlook? I checked it with Spy++ at work and “To” Field is a second in a row “RichEdit20WPT” class (note T on the end), parent class is “#32770 (Dialog)”, parent of this is “AfxWndW” and once again parent class is “AfxWndW” (this is some kind MS-style TPanel in TPanel) and – tadam! – mail window is of a class “rctrl_renwnd32”. So the pseudocode for this will be:

Window:=FindWindow('rctrl_renwnd32',nil);<br>
Window:= FindWindowEx(Window,0,’AfxWndW’,nil);<br>
Window:= FindWindowEx(Window,0,’AfxWndW’,nil);<br>
Window:= FindWindowEx(Window,0,’#32770 (Dialog)’,nil);<br>
//Search for FIRST (don’t know what it is)<br>
Sibling:= FindWindowEx(Window,0,’RichEdit20WPT’,nil);<br>
//Search for TO field<br>
Window:= FindWindowEx(Window,Sibling,’RichEdit20WPT’,nil);<br>
Text:='[email protected]';<br>
SendMessage(Window,WM_SETTEXT,0,Integer(Text));<br>



Probably you will want to use WM_GETTEXT to extract current text and update new text accordingly, but this is beyond the scope of getting into edit field.
BTW: This code strongly depends of outlook version, so try to check your version with Spy++ before).

smok1
This is terribly convoluted and totally unnecessary. You can use MAPI or automate Outlook to create the email; it's better to do it in an approved and documented way rather than resort to crude hacks. And what do you do when you run your code on Vista as a non-admin user and it doesn't work?
Ken White
+1  A: 

smok1: have you checked if your solution is actually working? Try to click Send button. OE says there is no address antered although it is in text box. Or click the icon on the left of text box. OE doesn't see address enterered using WM_SETTEXT. You have to manually type it.

Yes, you are right. I have improved the code by adding WM_CHAR - this actualy makes code work, beacause one extra space forces OE to see entered email address.
smok1