0

早上好,

我正在使用 Jacob 1.17 阅读我所有的 Outlook 联系人图片并将它们保存到文件中。该程序对于前 199 个联系人工作得很好。之后Dispatch.call失败并以以下异常终止:

Exception in thread "main" com.jacob.com.ComFailException: Invoke of: SaveAsFile
Source: Microsoft Outlook
Description: Cannot save the attachment. Cannot create file: ContactPicture.jpg.
Right-click the folder you want to create the file in, and then click Properties on
the shortcut menu to check your permissions for the folder.

    at com.jacob.com.Dispatch.invokev(Native Method)
    at com.jacob.com.Dispatch.invokev(Dispatch.java:625)
    at com.jacob.com.Dispatch.callN(Dispatch.java:453)
    at com.jacob.com.Dispatch.call(Dispatch.java:541)
    at outlookStuff.ManageContactsOutlook.tmpTest(ManageContactsOutlook.java:217)
    at mainPackage.Main.main(Main.java:32)

我真的不确定方法。我测试了一组不同的联系人——同样的错误。将所有对象设置为 null 以使垃圾收集器参与其中,但这无济于事。

造成麻烦的代码:

public void tmpTest(int intOutlookFolder, String strWorkingDir) {
Dispatch dipNamespace = this.axc.getProperty("Session").toDispatch();
Dispatch dipContactsFolder = Dispatch.call(dipNamespace, "GetDefaultFolder", (Object) new Integer(intOutlookFolder)).toDispatch();
Dispatch dipContactItems = Dispatch.get(dipContactsFolder, "items").toDispatch();

    @SuppressWarnings("deprecation")
    int count = Dispatch.call(dipContactItems, "Count").toInt();

    for (int i=1; i<=count; i++) {
        Dispatch dipContact;            
        dipContact = Dispatch.call(dipContactItems, "Item", new Integer(i)).toDispatch();

        String strEntryID = Dispatch.get(dipContact, "EntryID").toString().trim();

        //For Testing
        Status.printStatusToConsole("Outlook Contact "+strEntryID+" loaded");

        byte[] byteContactPicture = null;
        String strPathToTmpPicture = null;
        Dispatch dipAttachments = Dispatch.get(dipContact, "Attachments").toDispatch();
        @SuppressWarnings("deprecation")
        int countAttachements = Dispatch.call((Dispatch) dipAttachments, "Count").toInt();
        for (int j=1; j<=countAttachements; j++) {
            Dispatch currentAttachement;            
            currentAttachement = Dispatch.call(dipAttachments, "Item", new Integer(j)).toDispatch();

            if (Dispatch.get(currentAttachement, "FileName").toString().equals("ContactPicture.jpg")) {                 
                strPathToTmpPicture = strWorkingDir+strEntryID+".jpg";

                //The Crashing Part
                Dispatch.call(currentAttachement, "SaveAsFile", strPathToTmpPicture);

                File tmpFile = new File(strPathToTmpPicture);
                if (tmpFile.exists()) {
                    try {
                        byteContactPicture = org.apache.commons.io.FileUtils.readFileToByteArray(tmpFile);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                currentAttachement = null;
                tmpFile = null;
            }
            currentAttachement = null;
        }
        dipAttachments = null;
    }
    dipContactItems = null;
    dipContactsFolder = null;
    dipNamespace = null;
}

有人可以有一个想法吗?

感谢航空

4

0 回答 0