Attachments.Add - index out of bounds (continued)

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

Attachments.Add - index out of bounds (continued)
 
degree


Guest


This is a continuation of this topic: https://www.add-in-express.com/forum/read.php?FID=5&TID=14594 (that one is closed). Sot the problem is that some users still get errors when adding attachments. We found out that after attachment is added and it is bigger than exchange limit an exception is thrown. We handle this exception to re-add a different file instead. However at the moment the exception is thrown the mailItem.Attachments collection grows from 0 to 1, even though the attachment is not added (and no attachment is visible in the UI). Calling attachments.Remove(attachments.Count) does not take any effect, attachments.Count is still 1. And after that nothing can be attached - getting the index out of bounds error.

Users report that they can get around this problem by adding a small file, then removing it, then attaching the large file.

Here are some logs to visualize this:



Root attempt - there are 0 attachment(s)

Root attempt failed - there are 1 attachment(s).

Error (probably total size exceed exchange server limit).
Exception: System.Runtime.InteropServices.COMException (0x80004005): The file you're attaching is bigger than the server allows. Try putting the file in a shared location and sending a link instead.
   at Microsoft.Office.Interop.Outlook.Attachments.Add(Object Source, Object Type, Object Position, Object DisplayName)
   at AddinModule.CustomAttachSingle(Attachments attachments, String filePath)

// here we call attachments.Remove(attachments.Count)

Custom attempt #1 - there are 1 attachment(s)
Custom attempt #1 failed - there are 2 attachment(s).
Error: System.Runtime.InteropServices.COMException (0x80020009): Array index out of bounds.
   at Microsoft.Office.Interop.Outlook.Attachments.Add(Object Source, Object Type, Object Position, Object DisplayName)
   at AddinModule.CustomAttachSingle(Attachments attachments, String filePath)

Custom attempt #2 - there are 2 attachment(s)
Error: System.Runtime.InteropServices.COMException (0x80020009): Array index out of bounds.
   at Microsoft.Office.Interop.Outlook.Attachments.Add(Object Source, Object Type, Object Position, Object DisplayName)
   at AddinModule.CustomAttachSingle(Attachments attachments, String filePath)


Is there something we can do about it? Can this still be COM-release related? We have double-checked the flow to release all COM objects. Can this be a bug in Outlook itself?
Posted 25 Aug, 2017 01:57:44 Top
Andrei Smolin


Add-in Express team


Posts: 18825
Joined: 2006-05-11
Hello,

I suppose this may relate to a problem in Outlook. Still, this may also be caused by a non-released COM object. What Outlook build number is used on an affected machine(s)? Is it 32-bit or 64bit?

Do you modify the item and/or its attachments before the issue occurs? Do you connect to the item's events?

Can you send me AddinModule.cs and AddinModule.Designer.cs (if exists) from your add-in project? If yes, please send them to the support email address; find it in {Add-in Express installation folder}\readme.txt. Please make sure your email contains a link to this topic.


Andrei Smolin
Add-in Express Team Leader
Posted 25 Aug, 2017 09:10:44 Top
degree


Guest


Hi Andrei

Thanks for your response. I have sent the code via email just now. I should also mention that this also happens immediately after Outlook startup. User starts creating new email, attaches first file and the problem occurs. So there are no leftovers from some other, older messages/attachments. I don't have the info about build number/version yet - will let you know as soon as I get it. Other things are specified in the email. Let me know if you need anything more.
Posted 28 Aug, 2017 04:16:15 Top
Andrei Smolin


Add-in Express team


Posts: 18825
Joined: 2006-05-11
Hello Andrzej,

In your code I see a number of COM objects that you need to release.

Consider the following sketch imitating part of your code:


var supportMail = OutlookApp.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem;
if (supportMail != null)
{
    supportMail.UserProperties.Add(Settings.fileMailUserPropertyName, Outlook.OlUserPropertyType.olText, false).Value = Guid.NewGuid().ToString();
    supportMail.Display();


This fragment creates and leaves unreleased these COM object:
- the COM object stored in the supportMail variable
- the COM object returned by calling supportMail.UserProperties
- the COM object returned by calling UserProperties.Add

You need to create local variables to store the COM objects #2 and #3. You should release the variables storing the COM objects #1-#3 after use.

Also make sure that whenever you call OutlookApp.ActiveExplorer(), you release the COM object created by this call.

These may or may not relate to the issue. Try to fix these problems and let me know if the issue is gone.


Andrei Smolin
Add-in Express Team Leader
Posted 28 Aug, 2017 07:38:30 Top
degree


Guest


Thanks Andrei, I will try that fix, however this piece of code is only called when someone does a specific action with our addin (which is not the case with the user scenario we are struggling with here). But anyway thanks for this and the hint around the explorer - I will fix other similar places. Will get back to you as soon as I test it out.
Posted 28 Aug, 2017 09:06:11 Top
Andrei Smolin


Add-in Express team


Posts: 18825
Joined: 2006-05-11
Hello Andrzej,

I've noticed that I haven't said on the issue itself. I've tested adding a big attachment to an email. This produces the following message for me: "The file you're attaching is bigger than the server allows. Try putting the file to a shared location and sending a link instead". Right after I close the message I check the number of attachments: it is zero for me.

That is, I add an attachment to an open email, and [after getting an exception message and closing it] execute the following VBA macro:
Dim mail As Outlook.MailItem
Set mail = Application.ActiveInspector().CurrentItem
Debug.Print mail.Attachments.Count


Note that unlike .NET, VBA releases all COM objects after use.


Andrei Smolin
Add-in Express Team Leader
Posted 29 Aug, 2017 06:27:53 Top
degree


Guest


Just wanted to wrap this up - the error is still there, we are not able to reproduce it, but also it is persistent for some users. Releasing all possible COM objects did not help and also removing of attachments via code does not "fix" the broken collection. However the collection gets fixed when user first attaches a small file and then removes it. Magic. I think this is an Outlook internal issue. We applied some changes in n our workflow to mitigate this problem.
Posted 08 Sep, 2017 09:10:10 Top
Andrei Smolin


Add-in Express team


Posts: 18825
Joined: 2006-05-11
Thank you. What Outlook build number(s) is used on the affected machine(s)?


Andrei Smolin
Add-in Express Team Leader
Posted 11 Sep, 2017 07:07:14 Top
degree


Guest


Office Professional Plus 2016 Version 1707 Build 8326.2096
Posted 12 Sep, 2017 01:05:50 Top
Andrei Smolin


Add-in Express team


Posts: 18825
Joined: 2006-05-11
Okay. Thank you. This topic will get closed after 30 days of inactivity. If you find anything of value, please write me to the support email address and I'll reopen it.


Andrei Smolin
Add-in Express Team Leader
Posted 14 Sep, 2017 07:32:59 Top