Add or get attachment - index out of bounds of array exception

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

Add or get attachment - index out of bounds of array exception
 
degree




Posts: 34
Joined: 2010-07-27
Hello devs!

We are encountering "Index out of bounds of array" exception when getting attachment from an Attachments collection and also when adding attachments to a mailItem.Attachments collection. The problem occurrs for some users only, and I am not able to reproduce it on the development environment. This is on Outlook 2016.


Sample code (removed COM object releasing for clarity):


var mailAttachments = mail.Attachments;
var attachmentCount = mailAttachments.Count;
try
{
	for (int i = attachmentCount; i >= 1; i--)
	{
		Attachment attachment = null;

		try
		{
			attachment = mailAttachments[i];
			...


ERROR: System.Runtime.InteropServices.COMException (0x80020009): Datatabellen er utenfor grensene.
at Microsoft.Office.Interop.Outlook.Attachments.get_Item(Object Index)
at SendExtender.ProcessAttachments(MailItem mail)
at SendExtender.Execute(MailItem mail)





Another sample:


var attachments = currMail.Attachments;
try
{
	foreach (var f in fileNames)
	{
		try
		{
			attachments.Add(f, Outlook.OlAttachmentType.olByValue, 1, Path.GetFileName(f));
		}
		catch
		{
			string repfile = Path.GetTempPath() + Path.GetFileName(f) + ".extension";
			File.WriteAllText(repfile, f);
			attachments.Add(repfile, Outlook.OlAttachmentType.olByValue, 1, Path.GetFileName(repfile));
			...


ERROR:

Assembly Codebase: file:///C:/Windows/assembly/GAC_MSIL/Microsoft.Office.Interop.Outlook/15.0.0.0__71e9bce111e9429c/Microsoft.Office.Interop.Outlook.dll
Assembly Full Name: Microsoft.Office.Interop.Outlook, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c
Assembly Version: 15.0.0.0
Exception Source: Microsoft Outlook

Exception Type: System.Runtime.InteropServices.COMException
Exception Message: Datatabellen er utenfor grensene.
Exception Target Site: Add

---- Stack Trace ----

Microsoft.Office.Interop.Outlook.Attachments.Add(Source As Object, Type As Object, Position As Object, DisplayName As Object)
Microsoft.Office.Interop.Outlook.dll: N 00000 (0x0) JIT
AddinModule.CustomAttach(currMail As MailItem)
Microsoft.Office.Interop.Outlook.dll: N 0266 (0x10A) IL
AddinModule.adxRibbonButtonAttach_OnClick(sender As Object, control As IRibbonControl, pressed As Boolean)
Microsoft.Office.Interop.Outlook.dll: N 0038 (0x26) IL
AddinExpress.MSO.ADXRibbonButton.DoInternalAction(e As ADXRibbonOnActionEventArgs)


What is the problem and how to solve it?
Posted 23 Jun, 2017 03:01:39 Top
Andrei Smolin


Add-in Express team


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

#1. Do you delete attachments in that loop?
#2. What code line generates the exception? Can you re-check this?

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 23 Jun, 2017 04:35:33 Top
degree




Posts: 34
Joined: 2010-07-27
Hello

1. No
2. In each code sample it is the last line. In the second sample it is inside a catch, because the first attempt to add the file failed (most probably due to exchange server size limit). The second code sample is actually the most importatnt issue.

As said before - this does happen only for a few users. I was hoping there is some known problem with Attachments collection - maybe when adding twice, after first addition fails? So its internal counter is increased, but no attachment added, or so.
Posted 23 Jun, 2017 06:04:38 Top
Andrei Smolin


Add-in Express team


Posts: 14140
Joined: 2006-05-11
1. Start with turning all other COM add-ins off.
2. Make sure your code releases all COM object in all branches. Say, do you release the COM object returned by Attachments.Add?
3. Try to debug the code by printing Attachments.Count before you get the exception.
4. Try to repair Office on the affected machines.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 23 Jun, 2017 06:48:09 Top
degree




Posts: 34
Joined: 2010-07-27
Hello Andrei

I am not releasing results of Attachments.Add - I will fix this. And will try with your other suggestions as well. Thanks for the tips.
Posted 23 Jun, 2017 09:23:15 Top
degree




Posts: 34
Joined: 2010-07-27
I have applied more COM releasing logic. It seemed to help to some extent, but the user is still getting the error in one particular case - replying to an email which has images, Outlook asks whether it should download them. When user chooses "No" and then tries to attach a file - same exception (index outside of bounds of array) is thrown on the Attachments.Add call. Other COM addins are disabled. What else can we do?
Posted 28 Jun, 2017 06:43:13 Top
Andrei Smolin


Add-in Express team


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

I suggest that you create a test add-in. The add-in should show a Ribbon button; clicking the button should add an attachment to the email being edited.

You unregister your main add-in, register the test one, reproduce the user actions above, and click the button to add an attachment. Does this produce the same exception? If not, this means, your add-in still contains unreleased COM objects.

I strongly suggest that you check section Releasing COM objects at https://www.add-in-express.com/docs/net-office-tips.php#releasing.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 28 Jun, 2017 07:11:37 Top
degree




Posts: 34
Joined: 2010-07-27
Please note that I am not able to reproduce the issue at all on my environment. I would have to create a dummy addin and distribute it to our users.
Posted 30 Jun, 2017 04:08:22 Top
Andrei Smolin


Add-in Express team


Posts: 14140
Joined: 2006-05-11
degree writes:
Please note that I am not able to reproduce the issue at all on my environment.


Alas, this is often the case on this end.

You can get more info if you add a number of debug messages to your code; use System.Diagnostics.Debug.WriteLine(). If the add-in is built in the Debug configuration, you can collect the messages at run time using DebugView (see http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx).

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 30 Jun, 2017 04:59:33 Top