Automation avec Outlook quand il est déjà ouvert
Bonjour,
Je fais des essais pour créer des dossier dans les différentes boites d'Outlook (Boîte de réception, Eléments envoyés et Brouillons).
Ce code fonctionne si Outlook est fermé mais j'ai une exception avec le message "Échec de l’exécution du serveur" si Outlook est déjà ouvert.
J'ai mis des messageBox un peu partout pour savoir où ça plante mais, j'en ai aucun qui apparaît. Ce qui signifie que ça plante sur
Code:
1 2 3 4 5
| try
Outlook := GetActiveOleObject('Outlook.Application');
except
Outlook := CreateOleObject('Outlook.Application');
end; |
Le code complet de ma procédure
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| procedure TestOutlook();
var
Outlook :OleVariant;
Mapi :variant;
Inbox, Sentbox, DraftBox : Variant;
Folder : Variant;
i : Integer;
const
olFolderSentMail = $00000005; // Elément envoyés
olFolderInbox = $00000006; // Boîte de réception
olFolderDrafts = $00000010; // Brouillons
begin
try
try
try
Outlook := GetActiveOleObject('Outlook.Application');
except
Outlook := CreateOleObject('Outlook.Application');
end;
ShowMessage('1');
Mapi := Outlook.GetNamespace('MAPI');
ShowMessage('2');
Inbox := Mapi.GetDefaultFolder(olFolderInbox);
ShowMessage('3');
Sentbox := Mapi.GetDefaultFolder(olFolderSentMail);
ShowMessage('4');
DraftBox := Mapi.GetDefaultFolder(olFolderDrafts);
ShowMessage('5');
Folder := Unassigned;
for i := 0 to Inbox.Folders.Count - 1 do
begin
Folder := Inbox.Folders[i];
if (Folder.Name = 'Toto dans Inbox') then
Break;
end;
if (Folder = Unassigned) then
Inbox.Folders.Add('Toto dans Inbox');
ShowMessage('6');
Folder := Unassigned;
for i := 0 to Sentbox.Folders.Count - 1 do
begin
Folder := Sentbox.Folders[i];
if (Folder.Name = 'Toto dans Sentbox') then
Break;
end;
if (Folder = Unassigned) then
Sentbox.Folders.Add('Toto dans Sentbox');
Folder := Unassigned;
for i := 0 to DraftBox.Folders.Count - 1 do
begin
Folder := DraftBox.Folders[i];
if (Folder.Name = 'Toto dans DraftBox') then
Break;
end;
if (Folder = Unassigned) then
DraftBox.Folders.Add('Toto dans DraftBox');
except
on E:Exception do
begin
ShowMessage('Outlook : ' + E.Message);
end;
end;
finally
Outlook := Unassigned;
Mapi := Unassigned;
Inbox := Unassigned;
Sentbox := Unassigned;
DraftBox := Unassigned;
Folder := Unassigned;
end;
end; |
Je travaille avec Dephi 7 et Outlook 2010.