0 Replies Latest reply: Sep 26, 2007 8:56 AM by 843804 RSS

    Jacob Exception

    843804
      Hi everyone,

      i get the following message when trying to execute my java program:

      Exception in thread "AWT-EventQueue-1" com.jacob.com.ComFailException: A COM exception has been encountered:
      At Invoke of: ActiveExplorer
      Description: An unknown COM error has occured.
           at com.jacob.com.Dispatch.invokev(Native Method)
           at com.jacob.com.Dispatch.get(Dispatch.java)
           at Dropzone.DTListenerJ.drop(DTListenerJ.java:37)
           at java.awt.dnd.DropTarget.drop(Unknown Source)
           at sun.awt.dnd.SunDropTargetContextPeer.processDropMessage(Unknown Source)
           at sun.awt.dnd.SunDropTargetContextPeer.access$800(Unknown Source)
           at sun.awt.dnd.SunDropTargetContextPeer$EventDispatcher.dispatchDropEvent(Unknown Source)
           at sun.awt.dnd.SunDropTargetContextPeer$EventDispatcher.dispatchEvent(Unknown Source)
           at sun.awt.dnd.SunDropTargetEvent.dispatch(Unknown Source)
           at java.awt.Component.dispatchEventImpl(Unknown Source)
           at java.awt.Container.dispatchEventImpl(Unknown Source)
           at java.awt.Component.dispatchEvent(Unknown Source)
           at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
           at java.awt.LightweightDispatcher.processDropTargetEvent(Unknown Source)
           at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
           at java.awt.Container.dispatchEventImpl(Unknown Source)
           at java.awt.Component.dispatchEvent(Unknown Source)
           at java.awt.EventQueue.dispatchEvent(Unknown Source)
           at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
           at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
           at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
           at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
           at java.awt.EventDispatchThread.run(Unknown Source)

      This is the Sourcecode that generates the error:

      package Dropzone;

      import java.awt.dnd.DropTargetDragEvent;
      import java.awt.dnd.DropTargetDropEvent;
      import java.awt.dnd.DropTargetEvent;
      import java.awt.dnd.DropTargetListener;
      import java.io.File;
      import java.io.FileWriter;
      import java.io.IOException;
      import java.io.PrintWriter;
      import com.jacob.activeX.ActiveXComponent;
      import com.jacob.com.Dispatch;
      import com.jacob.com.Variant;

      public class DTListenerJ implements DropTargetListener {
           
           public void dragEnter(DropTargetDragEvent dtde) {
           System.out.println("Drag Enter");
           }
           
           public void dragExit(DropTargetEvent dte) {
                System.out.println("Drag Exit");
           }
           
           public void dragOver(DropTargetDragEvent dtde) {
           System.out.println("Drag Over");
           }
           
           public void dropActionChanged(DropTargetDragEvent dtde) {
           System.out.println("Drop Action Changed");
           }
           
           public void drop(DropTargetDropEvent dtde) {
                ActiveXComponent xl = new ActiveXComponent("Outlook.Application");

                Object explorer = Dispatch.get(xl,"ActiveExplorer").toDispatch();
                Object selection = Dispatch.get(explorer, "Selection").toDispatch();
                Variant count = Dispatch.get(selection, "Count");

                for (int mailIndex = 1; mailIndex <= count.toInt(); mailIndex++ ) {

                     Object mailItem = Dispatch.call(selection, "Item", new Variant(mailIndex)).toDispatch();

                     Variant senderName = Dispatch.get(mailItem, "SenderName");
                     Variant subject = Dispatch.get(mailItem, "Subject");
                     Variant body = Dispatch.get(mailItem, "Body");

                     String emailFileName = subject.toString() +".txt";

                     String fullPath = "C:\\bla" + File.pathSeparator + emailFileName;
                     try {
                          File email = new File(fullPath);
                          PrintWriter writer = new PrintWriter( new FileWriter(email) );
                          writer.println("From: "+ senderName );
                          writer.println("Subject: "+ subject);
                          writer.println("");
                          writer.print( body );
                          writer.close();
                     }
                     catch (IOException e) {
                          System.out.println("IOException writing e-mail with subject: '"+ subject +"'");
                          continue;
                     }

                     Object attachments = Dispatch.get(mailItem, "Attachments").toDispatch();
                          Variant attachmentCount = Dispatch.get(attachments, "Count");

                          if ( attachmentCount.toInt() > 0 ) {

                               for( int attachmentIndex = 1; attachmentIndex<=attachmentCount.toInt(); attachmentIndex++ ) {

                                    Object attachment = Dispatch.call(attachments, "Item", new Variant(attachmentIndex)).toDispatch();
                                    Variant fileNameVariant = Dispatch.get(attachment, "FileName");
                                    String fileName = fileNameVariant.toString();

                                    Variant saveResult = Dispatch.call(attachment, "SaveAsFile", "C:\\bla"+File.pathSeparator+fileName);
                               }
                          }
                     }

                }
           }