Autor Beitrag
Ares
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 128



BeitragVerfasst: Do 20.11.08 19:35 
Hallo!

Ich möchte mir ein kleines Tool schreiben, dass aus einem Ordner in meinem Outlook Daten sammelt. Dabei scheitere ich leider am Zugriff auf die E-Mailinhalte:

ausblenden Delphi-Quelltext
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:
procedure TForm1.Button1Click(Sender: TObject);
var
  OutlookApp  : TOutlookApplication;
  Inbox       : MAPIFolder;
  Folder      : MAPIFolder;
  NmSpace     : _Namespace;
  Mail        : MailItem;
  i: Integer;
begin
  try
    OutlookApp := TOutlookApplication.Create(nil);
    OutlookApp.Connect;

    NmSpace := OutlookApp.GetNamespace('MAPI');

    Inbox := NmSpace.GetDefaultFolder(olFolderInbox);

    // Klappt, weil es nur um mein Outlook geht und der Ordner 10 
    // vorhanden ist...
    Folder := Inbox.Folders.Item(10);

    // Der Ordner enthält nur Mails
    for i:=1 to Folder.Items.Count do begin
      Mail :=  MailItem(Folder.Items.Item(i));
      MachWas(Mail.Body);      
    end;
  except
  end;
end;


Beim Aufruf von Mail.Body kommt es dann aber zu einer Zugriffsverletzung (EAccessViolation). Der Ordner enthält nur E-Mail, Folder.Items.Count stimmt mit der Anzahl der enthaltenen E-Mails überein. Was muss ich also tun um auf den Quelltext der Mails zugreifen zu können?

Besten Dank
Ares
Xentar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2077
Erhaltene Danke: 2

Win XP
Delphi 5 Ent., Delphi 2007 Prof
BeitragVerfasst: Do 20.11.08 20:22 
Normalerweise geht eine Liste doch von 0 bis Count -1 ?

_________________
PROGRAMMER: A device for converting coffee into software.
Ares Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 128



BeitragVerfasst: Fr 21.11.08 08:34 
Zitat:
Normalerweise geht eine Liste doch von 0 bis Count -1 ?


Normalerweis, aber das ist bei Outlook/Mapi anders. Wenn dem nicht so wäre, müsst die Schleife in den ersten Durchläufen auf jeden Fall funktionieren, der Ordner enthält rechte viele Mails. Index 1 gibt es also so oder so. Aber schon beim ersten Aufruf kommt es zu dem Fehler.

Ändern auf 0 bis Count-1 bringt dagegen einen Fehler wegen falschem Index (0).
freedy
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 403
Erhaltene Danke: 1

Winows 7
Delphi XE
BeitragVerfasst: Fr 21.11.08 10:13 
Was für ein Fehler kommt denn? Nur die AccessViolation? Kannst du prüfen, welches Objekt evtl. nicht existiert? Wäre nur so eine Idee...
Xentar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2077
Erhaltene Danke: 2

Win XP
Delphi 5 Ent., Delphi 2007 Prof
BeitragVerfasst: Fr 21.11.08 10:15 
Kenn mich damit nun nicht aus, aber muss man dem vielleicht noch sagen, dass er die Mail erst einlesen soll, damit der Body quasi gefüllt wird?
Schau doch mal die Methodenliste von Mail durch.

_________________
PROGRAMMER: A device for converting coffee into software.
ene
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 779
Erhaltene Danke: 1

Vista, XP, W2K
Delphi, .Net, Deutsch und Englisch
BeitragVerfasst: Fr 21.11.08 12:29 
Hi,

in Delphi hab ich das noch nicht gemacht, aber irgendwie weiß ich nicht, was dein Folder sein soll und kann dir nur ein VBA-Beispiel geben:

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
Dim objOutlook As Outlook.Application
Dim objNameSpace As Outlook.NameSpace
Dim objDefaultMail As Outlook.MAPIFolder
Dim objMail As Outlook.Items
Dim lng As Long

Set objOutlook = New Outlook.Application
Set objNameSpace = objOutlook.GetNamespace("MAPI")
Set objDefaultMail = objNameSpace.GetDefaultFolder(olFolderInbox)
Set objMail = objDefaultMail.Items

lng = objMail.Count
For lng = lng To 1 Step -1
  Debug.Print objMail.Item(lng).Subject
Next lng

Set objMail = Nothing
Set objDefaultMail = Nothing
Set objNameSpace = Nothing
Set objOutlook = Nothing


Vielleicht hilt dir das ja weiter. Normalerweise müsstest du aber auch eine Meldung bekommen, dass jemand von aussen versucht auf dein Outlook zuzugreifen...

_________________
Wir, die guten Willens sind, geführt von Ahnungslosen, Versuchen für die Undankbaren das Unmögliche zu vollbringen.
Wir haben soviel mit so wenig so lange versucht, daß wir jetzt qualifiziert sind, fast alles mit Nichts zu bewerkstelligen.
Xentar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2077
Erhaltene Danke: 2

Win XP
Delphi 5 Ent., Delphi 2007 Prof
BeitragVerfasst: Fr 21.11.08 12:49 
Achso, nochwas: Bist du sicher, dass der Fehler beim Zugriff auf Mail.Body auftritt? Vielleicht steckt der Fehler in deiner Prozedur MachWas ?

_________________
PROGRAMMER: A device for converting coffee into software.
freedy
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 403
Erhaltene Danke: 1

Winows 7
Delphi XE
BeitragVerfasst: Fr 21.11.08 12:54 
MachWas() wird ja eine von ihm programmierte Prozedur sein. Denke mal, dass ihm der Debugger schon sagen würde, dass der Fehler dort auftritt. Ich frage mich nur, ob Mail nicht nil ist.
Ares Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 128



BeitragVerfasst: Fr 21.11.08 14:25 
Nein, der Fehler steck nicht in MachWas() sondern eindeutig im Zugriff auf den Body. Mail ist auch nicht nil.

Ich hab echt keinen Schimmer woran das liegen könnte.
freedy
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 403
Erhaltene Danke: 1

Winows 7
Delphi XE
BeitragVerfasst: Fr 21.11.08 14:30 
Dann behaupte ich jetzt mal, dass diese Zeile ein Problem darstellt.

ausblenden Delphi-Quelltext
1:
  Mail := MailItem(Folder.Items.Item(i));					


Du typcastest ja die Rückgabe in ein MailItem. Bist du dir sicher, dass das so geht? Ich will das nur mal eben in Frage stellen. Im Debugger müsstest du es ja einfach sehen können, wenn dort alle Attribute richtig ausgefüllt sind. Ansonsten hab ich keinen Schimmer mehr, woran es sonst liegen könnte.
Ares Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 128



BeitragVerfasst: Do 27.11.08 10:49 
Ich habs gelöst. Wenn ich Mail als oleVariant deklariere funktioniert das Ganze ohne Fehler. Warum es mit der Deklaration als MailItem nicht funktioniert ist mir allerdings weiter ein Rätsel...