Question

I'm automating Outlook and I need to control who the email appears to be from. The users will have two or more Accounts set up in Outlook and I need to be able to select which account to send the email from. Any ideas?

Needs to be supported on Outlook 2003 and above. I'm using Delphi 2006 to code this, but that doesn't really matter.

Was it helpful?

Solution

A person named Sue Mosher wrote up a pretty summary on this issue in microsoft.public.office.developer.outlook.vba.

In short, it boils down to either of this:

  • use MailItem.SentOnBehalfOfName, which only works in Exchange enviromnents (I suppose that is the case for you) - when the user has "Send As" permissions for the other Exchange mailbox, this is pretty much the same thing as switching accounts.
  • use a small hack that involves fiddling with the CommandBars
  • use Outlook Redemption
  • (in OL2007, you would have MailItem.SendUsingAccount)

OTHER TIPS

Expanding a bit on the accepted answer, I needed a Delphi implementation of Sue's set_account function. Couldn't find anything on the internet anywhere for this, so here is a Delphi interpretation of Sue's code.

Function SetAccount(TargetAccount:string; var MailItem:OLEVariant):boolean;
var OLI,CBs,CBP,MC:olevariant;
    strAccountBtnName:String;
    i,t:Integer;
    FoundAccount:Boolean;
Const ID_ACCOUNTS = 31224;
begin
    FoundAccount:=false;
    OLI:=MailItem.GetInspector;
    CBs:=OLI.CommandBars;
    CBP:=CBs.FindControl(, ID_ACCOUNTS);
    t:=1;
    while (not FoundAccount) and (t<=CBP.Controls.Count) do begin
       MC:=CBP.Controls[t];
       i:=Pos(' ',MC.Caption);
       if i > 0 Then strAccountBtnName:=Copy(MC.Caption,i+1,Length(MC.Caption)-i)
       else strAccountBtnName:=MC.Caption;
       if strAccountBtnName = TargetAccount then begin
           MC.Execute;
           FoundAccount:=true;
       end;
       inc(t);
    end;
    Result:=FoundAccount;
end;

Credit to Sue Mosher, thank you, couldn't have done it without you :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top