Italian community of Lazarus and Free Pascal

Programmazione => Generale => Topic aperto da: Fabio - Luglio 26, 2012, 10:33:14 am

Titolo: Office Automation con Outlook
Inserito da: Fabio - Luglio 26, 2012, 10:33:14 am
Per svariate necessità avrei bisogno di interfacciarmi ad un Outlook ( quindi ovviamente solo su Windows ) e agire sulla rubrica.

Ho cercato un po' di informazioni e ho trovato questo esempio che serve a mandare un email:

Codice: [Seleziona]
uses ComObj;

...

procedure TForm1.Button1Click(Sender: TObject);
const olMailItem=0;

var Mail,Message:variant;

 

begin
  // attach to running outlook
  // Mail:=GetActiveOleObject('Outlook.Application');
  // open Outlook
  Mail:=CreateOleObject('Outlook.Application');
  Message:=Mail.CreateItem(olMailItem);
  Message.Recipients.Add('first at recipient');
  Message.Recipients.Add('second at recipient');
  Message.Subject:= 'This is the subject';
  Message.Body:= 'Hi from lazarus';
  Message.CC:='cc at recipient';
  Message.Attachments.Add('C:\lazarus\readme.txt') ;
  Message.Send();
end;

Poi suggeriscono di usare le documentazioni per VB/VBA che sono decisamente più facili da trovare e adattarle esempio questo
http://support.microsoft.com/kb/220595/it

Però non ho le costanti, non so se devo aggiungere la libreria "MSOUTL.OLB" come riferimento o cosa, ad esempio si pianta giustamente arrivando al
Codice: [Seleziona]
Set olItem = olApp.CreateItem(olContactItem)
che non trova "olContactItem"

Potrei fare tutto in VB e via, ma se possibile volevo farlo con Lazarus.
Avete qualche esperienza in merito?

Grazie.
Titolo: Re:Office Automation con Outlook
Inserito da: Fabio - Luglio 26, 2012, 10:37:50 am
Intanto qui ho trovato il valore della costante "olContactItem" che sembra essere 2
http://technet.microsoft.com/en-us/library/ee692870
Titolo: Re:Office Automation con Outlook
Inserito da: nomorelogic - Luglio 26, 2012, 03:42:08 pm
le costanti le devi dichiarare tu nella unit
oggi sei fortunato ;)


Codice: [Seleziona]
const
   // items
   olMailItem        = 0;
   olAppointmentItem = 1;
   olContactItem     = 2;
   olTaskItem        = 3;
   olJournalItem     = 4;
   // folder
   olFolderCalendar                 =  9; // The Calendar folder.
   olFolderContacts                 = 10; // The Contacts folder.
   olFolderTasks                    = 13; // The Tasks folder.
   olFolderConflicts                = 19; // The Conflicts folder (subfolder of Sync Issues folder). Only available for an Exchange account.
   olFolderDeletedItems             =  3; // The Deleted Items folder.
   olFolderDrafts                   = 16; // The Drafts folder.
   olFolderInbox                    =  6; // The Inbox folder.
   olFolderJournal                  = 11; // The Journal folder.
   olFolderJunk                     = 23; // The Junk E-Mail folder.
   olFolderLocalFailures            = 21; // The Local Failures folder (subfolder of Sync Issues folder). Only available for an Exchange account.
   olFolderManagedEmail             = 29; // The top-level folder in the Managed Folders group. For more information on Managed Folders, see Help in Microsoft Outlook. Only available for an Exchange account.
   olFolderNotes                    = 12; // The Notes folder.
   olFolderOutbox                   =  4; // The Outbox folder.
   olFolderSentMail                 =  5; // The Sent Mail folder.
   olFolderServerFailures           = 22; // The Server Failures folder (subfolder of Sync Issues folder). Only available for an Exchange account.
   olFolderSyncIssues               = 20; // The Sync Issues folder. Only available for an Exchange account.
   olFolderToDo                     = 28; // The To Do folder.
   olPublicFoldersAllPublicFolders  = 18; // The All Public Folders folder in the Exchange Public Folders store. Only available for an Exchange account.
   olFolderRssFeeds                 = 25; // The RSS Feeds folder.
   // field type
   olText = 1;

   // attachments
   wdStory = 6;

Titolo: Re:Office Automation con Outlook
Inserito da: Fabio - Luglio 26, 2012, 04:47:58 pm
Ho messo questo:
Codice: [Seleziona]
const
  olFolderContacts=10;
...
procedure TfrmMain.Button1Click(Sender: TObject);
var
  olApp, olNs, olContacts: OleVariant;
  Each: Integer;

begin
  olApp:=CreateOleObject('Outlook.Application');
  olNs:=olApp.GetNamespace('MAPI');
  olNs.Logon;
  olContacts:=olNs.GetDefaultFolder(olFolderContacts).Items;
  for Each:=1 to olContacts.Count do
  begin
    ShowMessage(olContacts.Item[Each].FullName);
  end;
end;

Se faccio uno ShowMessage di olContacts.Count mi riporta il numero corretto dei contatti ma quando vado a stampare la proprietà FullName mi dice
Citazione
raised exception class 'EOleSysError' parametro non corretto
Titolo: Re:Office Automation con Outlook
Inserito da: nomorelogic - Luglio 26, 2012, 05:07:56 pm
lavorare con Ole non è facile...

prova qualcosa del genere e vedi se riesci a leggere i campi:

Codice: [Seleziona]
var 
   itmContact, itmProperty: OleVariant;
   ScanProp: integer;
   sName, sValue: string;
begin
...
    itmContact:= olContacts.Items[scan];
    for ScanProp := 0 to itmContact.ItemProperties.Count - 1 do
       begin
             itmProperty := itmContact.ItemProperties.Item[ScanProp];
             sName  := itmProperty.Name;
             sValue := VarToStr( itmProperty.Value );
...


Titolo: Re:Office Automation con Outlook
Inserito da: Fabio - Luglio 27, 2012, 07:40:02 am
Mi dice parametro non corretto già subito su
Codice: [Seleziona]
itmContact:= olContacts.Item[Each];

E se faccio così:
Codice: [Seleziona]
itmContact:=olContacts;

Alla riga:
Codice: [Seleziona]
for ScanProp := 0 to itmContact.ItemProperties.Count - 1 do

Dice:
Citazione
Method 'ItemProperties' is not supported by automation object
Titolo: Re:Office Automation con Outlook
Inserito da: nomorelogic - Luglio 27, 2012, 09:21:42 am
strano, quello che ti ho postato era codice delphi che a me funziona e il messaggio "Method 'ItemProperties' is not supported by automation object" mi spiazza un po'.

Quando sviluppavo quella unit comunque facevo delle prove in vbs per vedere la sintassi, poi proprietà e metodi erano gli stessi che usavo in delphi.
Prova a vedere con vbs, ci sono un mare di esempi in giro.
Titolo: Re:Office Automation con Outlook
Inserito da: Fabio - Luglio 27, 2012, 04:00:39 pm
Mah guarda alla fine per l'utilità che avevo ho risolto in 20min facendolo in VBS, peccato perchè volevo farlo in Lazarus ma ci stavo mettendo troppo tempo tra cercare documentazione e codici funzionanti.

Grazie ugualmente.
Titolo: Re:Office Automation con Outlook
Inserito da: nomorelogic - Luglio 28, 2012, 10:15:51 am
Mi dice parametro non corretto già subito su
Codice: [Seleziona]
itmContact:= olContacts.Item[Each];

l'ho visto solo ora, la proprietà è "Items" e non "Item"
Titolo: Re:Office Automation con Outlook
Inserito da: Fabio - Luglio 28, 2012, 04:07:51 pm
Vado a memoria che ora non ce l'ho sotto mano ma con Items mi diceva proprio che non esisteva la proprietà.
Te lo confermo eventualmente lunedì che sono in ufficio e ho di nuovo quel codice.