home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 November / Chip_2002-11_cd1.bin / zkuste / delphi / kompon / d3456 / OUTLTB.ZIP / OutLookTools.pas next >
Pascal/Delphi Source File  |  2002-08-26  |  11KB  |  427 lines

  1. unit OutLookTools;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Classes, ComCtrls, SysUtils, Dialogs,
  7.   ComObj,Graphics, Controls, Forms, Variants;
  8.  
  9. {*******************************************************************************
  10. *                                                                              *
  11. * Outlook Connect, Version 1.3                                                 *
  12. *------------------------------------------------------------------------------*
  13. * by Perr Lothar                                                               *
  14. * e-mail: lothar.perr@gmx.net                                                  *
  15. *                                                                              *
  16. *******************************************************************************}
  17.  
  18.  
  19. const
  20. // OlOutlookBarViewType
  21.   olLargeIcon      = 0;
  22.   olSmallIcon      = 1;
  23. // OlDaysOfWeek
  24.   olFriday = 32;
  25.   olMonday = 2;
  26.   olSaturday = 64;
  27.   olSunday = 1;
  28.   olThursday = 16;
  29.   olTuesday = 4;
  30.   olWednesday = 8;
  31. //OlSortOrder
  32.   olAscending = 1;
  33.   olDescending = 2;
  34.   olSortNone = 0;
  35. //OlItemType
  36.   olAppointmentItem = 1;
  37.   olContactItem = 2;
  38.   olDistributionListItem = 7;
  39.   olJournalItem = 4;
  40.   olMailItem = 0;
  41.   olNoteItem = 5;
  42.   olPostItem = 6;
  43.   olTaskItem = 3;
  44. //OlDefaultFolders
  45.   olFolderDeletedItems = 3;
  46.   olFolderOutbox = 4;
  47.   olFolderSentMail = 5;
  48.   olFolderInbox = 6;
  49.   olFolderCalendar = 9;
  50.   olFolderContacts = 10;
  51.   olFolderJournal = 11;
  52.   olFolderNotes = 12;
  53.   olFolderTasks = 13;
  54.   olFolderDrafts = 16;
  55.  
  56. const
  57.   msoControlButton = 1;
  58.   msoButtonIcon = 1;
  59.   msoButtonCaption = 2;
  60.   msoButtonIconAndCaption = 3;
  61.  
  62. type
  63.   TCustomEnumWindowsProc = procedure(WinHandle : HWND);
  64.  
  65.   TWindowInfo = class(TObject)
  66.   public
  67.     Handle : HWND;
  68.   end;
  69.  
  70.   TOutlookConnect = class(TComponent)
  71.   private
  72.     MyOlApp:Variant;
  73.     MyNameSpace:Variant;
  74.     Active: Boolean;
  75.     MyOLEObject:String;
  76.     MyOLENameSpace:String;
  77.     fOnConnected: TNotifyEvent;
  78.     fOnDisConnected: TNotifyEvent;
  79.   protected
  80.   public
  81.     constructor Create(AOwner : TComponent); override;
  82.     destructor Destroy; override;
  83. // Global variables
  84.     function OutlookApplication: Variant;
  85.     function OutlookNameSpace: Variant;
  86.     function OutlookActiveExplorer: Variant;
  87.     procedure ConnectOutlook(Connect:Boolean);
  88.     function CurrentUser:String;
  89. // Default folders
  90.     function Contacts:Variant;
  91.     function Calendar:Variant;
  92.     function DeletedItems:Variant;
  93.     function Drafts:Variant;
  94.     function Inbox:Variant;
  95.     function Journal:Variant;
  96.     function Notes:Variant;
  97.     function Outbox:Variant;
  98.     function SentMail:Variant;
  99.     function Tasks:Variant;
  100.     function TopFolders(Index:Variant):Variant;
  101. // Selection handling (active explorer)
  102.     function GetSelection(Index:Word):Variant;
  103.     function GetSelectionCount:Word;
  104. // Contact handling (DefaultFolder-Contacts)
  105.     function CreateContact:Variant;
  106.     function Contact(Index:Variant):Variant;
  107.     function ContactCount:Word;
  108.     procedure ShowContact(Index:Word);
  109.     procedure DeleteContact(MyContact:Variant);
  110.     function FindContact(FindWhat:String):Variant;
  111. // Mail handling
  112.     function CreateMail(Recipient:String):Variant;
  113.     function AddRecipientToMail(MyMail:Variant; Recipient:String):Variant;
  114.     function AddAttachmentToMail(MyMail:Variant; FileName:String):Variant;
  115.  
  116. // Other outlook objects
  117.     function CommandBars:Variant;
  118.   published
  119.     property Connected : Boolean read Active write ConnectOutlook;
  120.     property OLEObject : String read MyOLEObject write MyOLEObject;
  121.     property OLENameSpace : String read MyOLENameSpace write MyOLENameSpace;
  122.     property OnConnect: TNotifyEvent read fOnConnected write fOnConnected;
  123.     property OnDisConnect: TNotifyEvent read fOnDisConnected write fOnDisConnected;
  124.   end;
  125.  
  126. procedure Register;
  127.  
  128. implementation
  129.  
  130. {$r Outlooktools.res}
  131.  
  132. procedure TOutlookConnect.ConnectOutlook(Connect:Boolean);
  133. begin
  134.   if Connect then
  135.   begin
  136.     try
  137.        MyOlApp:=CreateOleObject(OLEObject);
  138.        MyNameSpace:=MyOlApp.GetNamespace(OLENameSpace);
  139.        if (Assigned(fOnConnected)) then
  140.          fOnConnected(Self);
  141.     except
  142.       raise Exception.Create('Outlook registration failed');
  143.     end;
  144.   end else
  145.   begin
  146.     MyOlApp:=NULL;
  147.     MyNameSpace:=NULL;
  148.     if (Assigned(fOnDisConnected)) then
  149.       fOnDisConnected(Self);
  150.   end;
  151.   Active:=Connect;
  152. end;
  153.  
  154. function TOutlookConnect.CurrentUser:String;
  155. begin
  156.   if not Active then
  157.     raise Exception.Create('No connection to outlook');
  158.   try
  159.     Result:=MyNameSpace.CurrentUser.Name;
  160.   except
  161.     raise Exception.Create('Cannot import Item');
  162.   end;
  163. end;
  164.  
  165.  
  166. function TOutlookConnect.Contact(Index:Variant) : Variant;
  167. begin
  168.   if not Active then
  169.     raise Exception.Create('No connection to outlook');
  170.   try
  171.     Result:=myNameSpace.GetDefaultFolder(olFolderContacts).Items[Index];
  172.   except
  173.     raise Exception.Create('Cannot import Item');
  174.   end;
  175. end;
  176.  
  177. function TOutlookConnect.Contacts : Variant;
  178. begin
  179.   if not Active then
  180.     raise Exception.Create('No connection to outlook');
  181.   try
  182.     Result:=myNameSpace.GetDefaultFolder(olFolderContacts);
  183.   except
  184.     raise Exception.Create('Cannot connect to Contacts');
  185.   end;
  186. end;
  187.  
  188. function TOutLookConnect.ContactCount:Word;
  189. begin
  190.   if not Active then
  191.     raise Exception.Create('No connection to outlook');
  192.   try
  193.     Result:=myNameSpace.GetDefaultFolder(olFolderContacts).Items.Count;
  194.   except
  195.     Result:=0;
  196.   end;
  197. end;
  198.  
  199. function TOutlookConnect.GetSelection(Index:Word): Variant;
  200. begin
  201.   if not Active then
  202.     raise Exception.Create('No connection to outlook');
  203.   try
  204.     Result:=MyOlApp.ActiveExplorer.Selection(Index);
  205.   except
  206.     raise Exception.Create('No item selected');
  207.   end;
  208. end;
  209.  
  210. function TOutlookConnect.GetSelectionCount:Word;
  211. begin
  212.   if not Active then
  213.     raise Exception.Create('No connection to outlook');
  214.   try
  215.     Result:=MyOlApp.ActiveExplorer.Selection.Count;
  216.   except
  217.     result:=0;
  218.   end;
  219. end;
  220.  
  221. function TOutlookConnect.CreateContact:Variant;
  222. var
  223.   MyContact:Variant;
  224. begin
  225.   if not Active then
  226.     raise Exception.Create('No connection to outlook');
  227.   try
  228.     MyContact:=myNameSpace.GetDefaultFolder(olFolderContacts).Items.Add;
  229.     Result:=MyContact;
  230.   except
  231.     raise Exception.Create('Cannot create contact');
  232.   end;
  233. end;
  234.  
  235. procedure TOutlookConnect.DeleteContact(MyContact:Variant);
  236. begin
  237.   if not Active then
  238.     raise Exception.Create('No connection to outlook');
  239.   try
  240.     MyContact.Delete;
  241.   except
  242.     raise Exception.Create('Cannot delete item');
  243.   end;
  244. end;
  245.  
  246. function TOutlookConnect.FindContact(FindWhat:String):Variant;
  247. begin
  248.   if not Active then
  249.     raise Exception.Create('No connection to outlook');
  250.   try
  251.     Result:=myNameSpace.GetDefaultFolder(olFolderContacts).Items.Find(FindWhat);
  252.   except
  253.     raise Exception.Create('Error finding Item');
  254.   end;
  255. end;
  256.  
  257. function TOutlookConnect.CreateMail(Recipient:String):Variant;
  258. var
  259.   MyMail:Variant;
  260. begin
  261.   if not Active then
  262.     raise Exception.Create('No connection to outlook');
  263.   try
  264.     MyMail:=myNameSpace.GetDefaultFolder(olFolderOutBox).Items.Add;
  265.     MyMail.To:=Recipient;
  266.     Result:=MyMail;
  267.   except
  268.     raise Exception.Create('Cannot create mail');
  269.   end;
  270. end;
  271.  
  272. function TOutlookConnect.AddRecipientToMail(MyMail:Variant; Recipient:String):Variant;
  273. var
  274.   MyRecipient:Variant;
  275. begin
  276.   if not Active then
  277.     raise Exception.Create('No connection to outlook');
  278.   try
  279.     MyRecipient:=MyMail.Recipients.Add(Recipient);
  280.     Result:=MyRecipient;
  281.   except
  282.     raise Exception.Create('Cannot access mail');
  283.   end;
  284. end;
  285.  
  286. function TOutlookConnect.AddAttachmentToMail(MyMail:Variant; FileName:String):Variant;
  287. var
  288.   MyAttachment:Variant;
  289. begin
  290.   if not Active then
  291.     raise Exception.Create('No connection to outlook');
  292.   try
  293.     MyAttachment:=MyMail.Attachments.Add(FileName);
  294.     Result:=MyAttachment;
  295.   except
  296.     raise Exception.Create('Cannot access mail');
  297.   end;
  298. end;
  299.  
  300. function TOutlookConnect.OutlookApplication: Variant;
  301. begin
  302.   Result:=MyOlApp;
  303. end;
  304.  
  305. function TOutlookConnect.OutlookNameSpace: Variant;
  306. begin
  307.   Result:=MyNameSpace;
  308. end;
  309.  
  310. function TOutlookConnect.OutlookActiveExplorer: Variant;
  311. begin
  312.   Result:=MyOlApp.ActiveExplorer;
  313. end;
  314.  
  315. function TOutlookConnect.CommandBars : Variant;
  316. begin
  317.   if not Active then
  318.     raise Exception.Create('No connection to outlook');
  319.   Result:=MyOlApp.ActiveExplorer.CommandBars;
  320. end;
  321.  
  322. Procedure TOutLookConnect.ShowContact(Index:Word);
  323. var
  324.   MyContact:Variant;
  325. begin
  326.   if not Active then
  327.     raise Exception.Create('No connection to outlook');
  328.   try
  329.     MyContact:=myNameSpace.GetDefaultFolder(olFolderContacts).Items[Index];
  330.     MyContact.Display;
  331.   except
  332.     raise Exception.Create('Cannot display contact');
  333.   end;
  334. end;
  335.  
  336. function TOutlookConnect.Calendar:Variant;
  337. begin
  338.   if not Active then
  339.     raise Exception.Create('No connection to outlook');
  340.   Result:=myNameSpace.GetDefaultFolder(olFolderCalendar);
  341. end;
  342.  
  343. function TOutlookConnect.DeletedItems:Variant;
  344. begin
  345.   if not Active then
  346.     raise Exception.Create('No connection to outlook');
  347.   Result:=myNameSpace.GetDefaultFolder(olFolderDeletedItems);
  348. end;
  349.  
  350. function TOutlookConnect.Drafts:Variant;
  351. begin
  352.   if not Active then
  353.     raise Exception.Create('No connection to outlook');
  354.   Result:=myNameSpace.GetDefaultFolder(olFolderDrafts);
  355. end;
  356.  
  357. function TOutlookConnect.Inbox:Variant;
  358. begin
  359.   if not Active then
  360.     raise Exception.Create('No connection to outlook');
  361.   Result:=myNameSpace.GetDefaultFolder(olFolderInbox);
  362. end;
  363.  
  364. function TOutlookConnect.Journal:Variant;
  365. begin
  366.   if not Active then
  367.     raise Exception.Create('No connection to outlook');
  368.   Result:=myNameSpace.GetDefaultFolder(olFolderJournal);
  369. end;
  370.  
  371. function TOutlookConnect.Notes:Variant;
  372. begin
  373.   if not Active then
  374.     raise Exception.Create('No connection to outlook');
  375.   Result:=myNameSpace.GetDefaultFolder(olFolderNotes);
  376. end;
  377.  
  378. function TOutlookConnect.Outbox:Variant;
  379. begin
  380.   if not Active then
  381.     raise Exception.Create('No connection to outlook');
  382.   Result:=myNameSpace.GetDefaultFolder(olFolderOutbox);
  383. end;
  384.  
  385. function TOutlookConnect.SentMail:Variant;
  386. begin
  387.   if not Active then
  388.     raise Exception.Create('No connection to outlook');
  389.   Result:=myNameSpace.GetDefaultFolder(olFolderSentMail);
  390. end;
  391.  
  392. function TOutlookConnect.Tasks:Variant;
  393. begin
  394.   if not Active then
  395.     raise Exception.Create('No connection to outlook');
  396.   Result:=myNameSpace.GetDefaultFolder(olFolderTasks);
  397. end;
  398.  
  399. function TOutlookConnect.TopFolders(Index:Variant):Variant;
  400. begin
  401.   if not Active then
  402.     raise Exception.Create('No connection to outlook');
  403.   Result:=myNameSpace.Folders.Item(Index);
  404. end;
  405.  
  406. constructor TOutlookConnect.Create(AOwner : TComponent);
  407. begin
  408.   inherited Create(AOwner);
  409.   if Active then ConnectOutlook(True);
  410.   if OLEObject='' then OLEObject:='Outlook.Application';
  411.   if OLENameSpace='' then OLENameSpace:='MAPI';
  412. end;
  413.  
  414. destructor TOutlookConnect.Destroy;
  415. begin
  416.   ConnectOutlook(False);
  417.   inherited Destroy;
  418. end;
  419.  
  420. procedure Register;
  421. begin
  422.   RegisterComponents('Outlook', [TOutlookConnect]);
  423. end;
  424.  
  425. end.
  426.  
  427.