Save CurrentItem - unreadable issue (Herbrew)

Add-in Express™ Support Service
That's what is more important than anything else

Save CurrentItem - unreadable issue (Herbrew)
 
Ada Huang




Posts: 7
Joined: 2016-03-28
Hi,

Our product implement that it will save the current mail item to draft when reply/forward email.

We found a issue.
If the content or subject of our mail is Herbrew, it sometimes will be unreadable after save the mail item.
But, the issue can't be reproduced always.
Could you help me? Thank you.


Please refer to the below image and source code.

[img]https://drive.google.com/file/d/0B9Lnb4s2zk-xQjR0MDZaTEVZb0E/view?usp=sharing[/img]


static private MailItem GetMailItemFromInspector(object sender) {
    MailItem mail = null;
    if (sender != null && (sender as Inspector).CurrentItem is MailItem) {
        mail = (sender as Inspector).CurrentItem as MailItem;
    }

    if (mail != null) {
        mail.Save();
        return mail;
    }
}
Posted 14 Jul, 2016 04:55:36 Top
Andrei Smolin


Add-in Express team


Posts: 18829
Joined: 2006-05-11
Hello Ada,

This code fragment doesn't release COM objects created. Say, you call Inspector.CurremtItem twice; you should regard this as creating two COM objects representing the same Outlook item. You need to release *every* COM object you create. Find more details in https://www.add-in-express.com/docs/net-office-tips.php#releasing.

Below is a variant of that code fragment; it releases the COM object created by calling Inspector.CurremtItem if the item is *not* a MailItem.

static private MailItem GetMailItemFromInspector(object sender) { 
    if (sender != null {
        MailItem mail = null;
        object item = (sender as Inspector).CurrentItem;
        if (item is MailItem) { 
            mail = item as MailItem; 
        } 
 
        if (mail != null) { 
            mail.Save(); 
            return mail; 
        } else {
            if (item != null) Marshal.ReleaseComObject(item);
        }   
    }
    return null;
} 



Andrei Smolin
Add-in Express Team Leader
Posted 14 Jul, 2016 05:47:04 Top
Ada Huang




Posts: 7
Joined: 2016-03-28
Hi Andrei,

Thanks for your reply.

I add the ReleaseComObject code, but the issue still can be reproduced.


And, I found after Save the currentItem, I will receive the ActiveInspector event twice:

The first trigger, the content of the CurrentItem.Body is fine.
The second trigger, some words of the CurrentItem.Body are unreadable.

Please refer to the below code:

1. Add the AddinExpress.MSO.ADXOlInspector_EventHandler

adxOutlookEvents.NewInspector += new AddinExpress.MSO.ADXOlInspector_EventHandler(this.adxOutlookEvents_NewInspector);
adxOutlookEvents.InspectorActivate += new AddinExpress.MSO.ADXOlInspector_EventHandler(this.adxOutlookEvents_ActiveInspector);

2. Implement the event

private void adxOutlookEvents_NewInspector(object sender, object inspector, string folderName) {
    MailItem mail = null;

    if (inspector != null)
    {
        object item = (inspector as Inspector).CurrentItem;
        if (item is MailItem)
        {
            mail = item as MailItem;
        }

        if (mail != null)
        {
            mail.Save();
        }
        else
        {
            if (item != null) Marshal.ReleaseComObject(item);
        }
    }
}

private void adxOutlookEvents_ActiveInspector(object sender, object inspector, string folderName) {
    MailItem mail = null;
 
    if (inspector != null)
    {
        object item = (inspector as Inspector).CurrentItem;
        if (item is MailItem)
        {
            mail = item as MailItem;
        }

        if (mail == null)
        {
            if (item != null) Marshal.ReleaseComObject(item);
        }
    }
}



And, I found the content of CurrentItem.Body will be fine if I save mail by emulating key press.

[DllImport("user32.dll")]
public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);

private void adxOutlookEvents_NewInspector(object sender, object inspector, string folderName) {
    {
        keybd_event(0xA2, 0xb8, 0, 0);        // key_down_ctrl
        keybd_event(0x53, 0x9f, 0, 0);         // key_down_s
        keybd_event(0x53, 0x9f, 0x0002, 0); // key_up_s
        keybd_event(0xA2, 0xb8, 0x0002, 0); // key_up_ctrl
    }
}



Could you give me some suggestions?
Thank you.

Have a nice day.
Ada
Posted 14 Jul, 2016 22:50:20 Top
Andrei Smolin


Add-in Express team


Posts: 18829
Joined: 2006-05-11
Hello Ada,

1. You have COM objects left non-released. In adxOutlookEvents_NewInspector, a COM object isn't released if the mail is saved; in adxOutlookEvents_ActiveInspector, a COM object isn't released if the inspector contains a MailItem. Also, you need to isolate this code from other parts of your add-in; I suspect the other parts contain unreleased COM objects as well.
2. I wouldn't save the item in the NewInspector event. What do you need to achieve?


Andrei Smolin
Add-in Express Team Leader
Posted 15 Jul, 2016 03:02:24 Top
Ada Huang




Posts: 7
Joined: 2016-03-28
Hi Andrei,

1. I need to check all COM objects are released or not on our code.
2. When receive the adxOutlookEvents_NewInspector, we will add the UserProperties to the CurrentItem and then save the mail.
But, some words will be unreadable if we save the mail first.
Maybe as you say, the COM object doesn?Â?Ð?ét be released. I will check it, thanks for your help.

Have a nice day.
Ada
Posted 17 Jul, 2016 08:57:45 Top
Andrei Smolin


Add-in Express team


Posts: 18829
Joined: 2006-05-11
Hello Ada,

Don?Â?Ð?ét hesitate to contact me again if I can help any further.


Andrei Smolin
Add-in Express Team Leader
Posted 18 Jul, 2016 02:06:42 Top
Ada Huang




Posts: 7
Joined: 2016-03-28
Hi Andrei,

After modify code to check the issue, the CurrentItem.Body still includes some unreadable words.
I check the number when call ReleaseComObject function, it is zero.

Please refer to the below code.
I just call Save function to check the issue.


private void adxOutlookEvents_NewInspector(object sender, object inspector, string folderName) { 
    MailItem mail = null; 
    int comNum = 0;

    if (inspector != null) 
    { 
        object item = (inspector as Inspector).CurrentItem; 
        if (item is MailItem) 
        { 
            mail = item as MailItem; 
        } 
 
        if (mail != null) 
        { 
            mail.Save(); 
            comNum = Marshal.ReleaseComObject(mail); 
        } 
        else 
        { 
            if (item != null) 
                comNum = Marshal.ReleaseComObject(item); 
        } 
    } 
} 
 
private void adxOutlookEvents_ActiveInspector(object sender, object inspector, string folderName) { 
} 



I want to save the mail when do reply/forward mail.
As you say before, you wouldn't save the item in the NewInspector event.
Do you have any suggestion?
Or, could you help to check the special mail?
Thank you.

Have a nice day.
Ada
Posted 18 Jul, 2016 04:39:17 Top
Andrei Smolin


Add-in Express team


Posts: 18829
Joined: 2006-05-11
Hello Ada,

http://temp.add-in-express.com/support/MyAddin118-Save-in-NewInspector.zip

I can't reproduce the issue with this add-in if *all* other COM add-ins are turned off. It handles the NewInspector event in this way (actually, this is an equivalent of your code):

private void adxOutlookAppEvents1_NewInspector(object sender, object inspector, string folderName) {
    Outlook.MailItem mail = null;
    int comNum = 0;

    if (inspector != null) {
        object item = (inspector as Outlook.Inspector).CurrentItem;
        if (item is Outlook.MailItem) {
            mail = item as Outlook.MailItem;
        }

        if (mail != null) {
            mail.Save();
        }
        if (item != null)
            comNum = Marshal.ReleaseComObject(item);
    }
}



Andrei Smolin
Add-in Express Team Leader
Posted 18 Jul, 2016 07:35:06 Top
Ada Huang




Posts: 7
Joined: 2016-03-28
Hi Andrei,

I create a MyAddin1 project to test the special mail ?Â?Ð?ìFW סקר שירות ומוכנות - תחום ביטחון.msg?Â?Ð?í.
And, the result is the same.
The subject title will be unreadable when do reply/forward this mail if call save function in the NewInspector event.

Please refer to the code and the special mail.
https://drive.google.com/file/d/0B9Lnb4s2zk-xWmI3N2lYNDV6ZkU/view?usp=sharing

Could you help to check this mail?
Thank you very much.

Have a nice day.
Ada
Posted 19 Jul, 2016 04:23:02 Top
Andrei Smolin


Add-in Express team


Posts: 18829
Joined: 2006-05-11
Hello Ada,

I can't reproduce the issue using Add-in Express 8.2.4371 and Outlook 2016 16.0.7030.1012.

Ada Huang writes:
I create a MyAddin1 project to test the special mail


Please confirm that you have *all* other COM add-ins disabled.

I've noticed a different issue: if you open an email saved in Drafts, you'll find that its body is empty. I suppose this occurs because you save the item in NewInspector. Try to use InspectorActivate instead.


Andrei Smolin
Add-in Express Team Leader
Posted 19 Jul, 2016 05:19:35 Top