Problem mit SWT/OLE bei mehrmaligen Zugriff auf Attachments

shio

Grünschnabel
Guten Tag,

mich plagt jetzt schon sehr lange ein Problem und zwar.

Ich benutze das SWT Package um über ole auf die Attachments eines selectierten E-Mails zu kommen. Im Hintergrund geht es dabei um E-Mail Archivierung.

Als als erstes habe ich eine Methode die nichts anderes macht als die Attachments zu durchsuchen und schaut ob diese Dateien archiviert werden können (msg, pdf, jpg,...)

Code:
  private void analyzeMail()
  {
    try
    {

Display display = Display.getCurrent();
        Shell shell = new Shell(display);
        shell.setText("Outlook Automation");
        shell.setLayout(new FillLayout());
        LOG.note("DlgArchive: analyzeMail()");
        OleFrame frm = new OleFrame(shell, SWT.NONE);

        OleClientSite site = new OleClientSite(frm, SWT.NONE, "Outlook.Application");
        OleAutomation auto = new OleAutomation(site);
        Variant mapiNamespace = auto.invoke(property(auto,"ActiveExplorer"));
        
        OleAutomation activeExplorerAuto = mapiNamespace.getAutomation();
        Variant selected= activeExplorerAuto.invoke(property(activeExplorerAuto,"Selection"));
        OleAutomation selection= selected.getAutomation();
        Variant mailItems=selection.invoke(property(selection,"Item"),new Variant[] { new Variant(actSel) });
        
        OleAutomation mailItem= mailItems.getAutomation();

       Variant attachment = mailItem.invoke(property(mailItem,"Attachments"));
       OleAutomation attachmentAutomation = attachment.getAutomation();
       globalAttachmentAuto=attachmentAutomation;
       Variant attachmentCount = attachmentAutomation.invoke((property(attachmentAutomation,"Count")));
       
       
       for(int j=1, jcnt=attachmentCount.getInt();j<= jcnt;j++){
                Variant attachmentdetail = attachmentAutomation.invoke((property(attachmentAutomation,"Item")),new Variant[] { new Variant(j)});
                OleAutomation attachmentdetailAutomation = attachmentdetail.getAutomation();

                Variant index    = attachmentdetailAutomation .getProperty(property(attachmentdetailAutomation,"Index"));
                Variant type     = attachmentdetailAutomation .getProperty(property(attachmentdetailAutomation,"Type"));
                
          if (type != null && (type.getString()).compareTo("1") == 0)
        {     // Type == 1 ist Anhang, gibt's auch inline, etc.
          Variant filename = attachmentdetailAutomation .getProperty(property(attachmentdetailAutomation,"filename"));

          
          if (Tools.checkFileFormat(filename.getString()))
          {
            //Speichere hier die filenname in einem Vector
          }
          else
          {
            LOG.note("Dateiformat der Datei: " + filename.getString() + " wird nicht unterstuetzt.");
          }
        }
                attachmentdetailAutomation.dispose();
      }
       attachmentAutomation.dispose();
       
        shell.dispose();
        auto.dispose();

        site.deactivateInPlaceClient();
        site.dispose();

        frm.dispose();
    }
    catch (Throwable t)
    {
      LOG.error (Tools.formatException (t));
    }
    
  }
  
      private static Variant invoke(OleAutomation auto, String command,String value) {

	return auto.invoke(property(auto, command),new Variant[] { new Variant(value) });

	}
    private static Variant invoke(OleAutomation auto, String command) {

		return auto.invoke(property(auto, command));

	}
    private static int property(OleAutomation auto, String name) {
    try{
		return auto.getIDsOfNames(new String[] { name })[0];
    }catch(NullPointerException e){
      System.out.print("Kommando "+name+" wurde nicht gefunden");
      return 0;
    }
	}

Soweit so gut, jetzt kann der Benutzer auswählen ob er der das ganze Email archivieren will oder nur ein Attachment. Wenn er das ganze Email archieviert funktioniert das.

Aber wenn ich nur 1 Attachment archivieren will dann kann ich aus irgendeinem Grund nicht mehr auf diese zugreifen...

Dies erfolgt so:

Code:
 private void saveAttachment(String filename, long index)
  {
    Display display = new Display();
    //Display display = Display.getCurrent();
        Shell shell = new Shell(display);

        shell.setText("Outlook Automation");
        shell.setLayout(new FillLayout());

        OleFrame frm = new OleFrame(shell, SWT.NONE);
        OleAutomation auto = new OleAutomation(site);
        Variant mapiNamespace = auto.invoke(property(auto,"ActiveExplorer"));
        OleAutomation activeExplorerAuto = mapiNamespace.getAutomation();
        
        Variant selected= activeExplorerAuto.invoke(property(activeExplorerAuto,"Selection"));
        OleAutomation selection= selected.getAutomation();
        
        Variant mailItems=selection.invoke(property(selection,"Item"),new Variant[] { new Variant(actSel) });
        OleAutomation mailItem= mailItems.getAutomation();
     
     Variant attachment = mailItem.invoke((property(mailItem,"Attachments")));
     OleAutomation attachmentAutomation = attachment.getAutomation();     
     Variant attachdetail = attachmentAutomation.invoke((property(attachmentAutomation,"Item")),new Variant[] { new Variant(index)});
     OleAutomation attachmentdetailAutomation = attachdetail.getAutomation(); 
//Genau hier wirft er eine Nullpointer Exception und ich komm nicht dahinter 
///warum ich nicht mehr auf die Attachments komme
    
    try
    {
      Variant file=invoke(attachmentdetailAutomation,"SaveAsFile",filename);
    }
    catch (Exception e)
    {
      LOG.showErrorPane(this, "Fehler", 
            "Fehler beim Speichern des Email-Attachments.", 
            Tools.formatException (e));
    }
    }

Vielleicht kann mir jemand helfen der mehr Erfahrung damit hat?
 
Zurück