Trying to figure out behavior of attachment move events

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

Trying to figure out behavior of attachment move events
 
Steve Obarowski


Guest


The scenario: I have an email selected with multiple attachments. I select multiple attachments and drag them to a folder that has OutloookItemsEvents event handlers attached.

If I have no special code defined, I get multiple events for each attachment. In particular, I am looking at ProcessItemAdd because it's the first one called. So if I have 3 attachments selected, I get 3 ProcessItemAdd events. With the special code in, however, I only get the first one. The others seems to get lost after I call ShowDialog() during the first event.

ProcessItemChange is the only event that still comes through multiple times, but I can't use it because it can take upwards of 30 seconds to fire.
Posted 19 Jun, 2017 10:46:17 Top
Andrei Smolin


Add-in Express team


Posts: 18817
Joined: 2006-05-11
Hello Steve,

You could process the ItemAdd event and raise the dialog *after* a delay; check the trick we describe in section Wait a little at https://www.add-in-express.com/docs/net-office-tips.php#wait-a-little.


Andrei Smolin
Add-in Express Team Leader
Posted 20 Jun, 2017 03:20:52 Top
Steve Obarowski


Guest


Thanks for the response, Andrei, but I'm confused as to where this would get implemented and how to tie it to the ItemAdd event in my OutlookItemsEvents object. Do I make the SendMessage call from my current ItemAdd handler? Do you have any samples that do something similar? The sample in the linked article doesn't provide much context.
Posted 20 Jun, 2017 08:36:14 Top
Andrei Smolin


Add-in Express team


Posts: 18817
Joined: 2006-05-11
Steve,

The idea is to show the dialog windows *after* the add-in processes the ItemAdd event for the last attachment dropped to the folder. To create such a delay, you call ADXAddinModule.SendMessage(%a unique integer > 1024%) in the ItemAdd event. The next phase is the OnSendMessage event: when it occurs and e.Message is equal to %a unique integer > 1024%, you open the dialog window. Store the data required for the dialog in a class-level variable(s).

The delay occurs because the message (i.e. %a unique integer > 1024%) is sent to a hidden window that Add-in Express creates for your add-in. Windows cannot deliver the message momentarily and this creates the desired effect: the message is handled by the window procedure of the hidden window (the result is the OnSendMessage event of the add-in module) only after Outlook completes handling moving the attachments.

The above won't work if they issue a wait between moving two attachments. In this case, the OnsendMessage event will occur after you handle a single attachment. If this is the case, you will need to periodically scan the folder items. Let's hope for the better.


Andrei Smolin
Add-in Express Team Leader
Posted 20 Jun, 2017 09:02:38 Top
Steve Obarowski


Guest


OK, that makes sense, and I've started to implement. The only question left is: How do I know when it's the last dropped attachment? They seem to be atomic events, and there's no way to know how many were originally dragged over.
Posted 20 Jun, 2017 13:52:14 Top
Andrei Smolin


Add-in Express team


Posts: 18817
Joined: 2006-05-11
Hello Steve,

Steve Obarowski writes:
How do I know when it's the last dropped attachment?


You can't know this. The actual solution depends on how Outlook is implemented: if you find that you receive OnSendMessage after handling every attachment, you'll need to scan the folder. Otherwise, you'll receive the first OnSendMessage event after the last attachment is processed.


Andrei Smolin
Add-in Express Team Leader
Posted 21 Jun, 2017 03:32:11 Top
Steve Obarowski


Guest


Thanks again, Andrei, and that's what I figured. However, your suggestions have led me to what I think will be a workable solution. Basically, I will have a queue of items to add and I'll poll it very often. The ItemAdd event will simply add the items to the queue, then the main addin module will handle the rest.
Posted 21 Jun, 2017 09:53:37 Top
Andrei Smolin


Add-in Express team


Posts: 18817
Joined: 2006-05-11
It looks really nice!


Andrei Smolin
Add-in Express Team Leader
Posted 21 Jun, 2017 09:57:19 Top
Steve Obarowski


Guest


Just fyi, I finally got it working after several trial-and-error sessions. Some observations in case these problems come up:

1) I could not use the DocumentItem object as it came in with the event in the item parameter. When I went to access it, I got the "COM object separated" error.

2) I then tried to use a copy of the document by calling the DocumentItem.Copy() method. This got me past the error, but the call to Copy() apparently generates another ItemAdd event.

What I had to do was to save off the file to a temp area (which we were already doing, I just had to move it earlier in the process), add an item to a queue in the main app thread which had the file path, subject and folder information. I then have a timer that polls that queue every half second. If there are items there, it brings up my dialog, garners the necessary info and does its processing. Finally, I remove the object from the queue.

All seems to be working great.

Thanks again for your guidance!
Posted 21 Jun, 2017 22:23:05 Top
Andrei Smolin


Add-in Express team


Posts: 18817
Joined: 2006-05-11
Hello Steve,

Steve Obarowski writes:
1) I could not use the DocumentItem object as it came in with the event in the item parameter. When I went to access it, I got the "COM object separated" error.


I assume you get this issue when accessing DocumentItem somewhere outside of the ItemAdd event; otherwise #2 contradicts to #1 in your message.

Is it possible that you cache the DocumentItem in a class-level variable? If so, you should understand that Add-in Express releases almost all of the COM objects passed to your event handler as soon as the event handler completes. Using a released variable produces the "COM object separated" error. To bypass this error in the scenario discussed, you need to store the EntryId of the DocumentItem, not the DocumentItem itself; to get the DocumentItem when required, you call Outlook.Namespace.GetItemFromId().


Andrei Smolin
Add-in Express Team Leader
Posted 22 Jun, 2017 03:04:58 Top