Catch Outlook events when attaching/sending a file from another application - c#

I'm trying to create an add-in for Outlook 2007/2010 to modify recipients of newly created email messages. Everything works fine if the user creates a new email message inside Outlook (I'm using the Inspectors.NewInspector event). But if the user uses another application (MS Word or Adobe Acrobat for example) to try to email an attachment, the NewInspector event isn't fired when the compose email window is displayed. Is there a straightforward approach to catching an event that is raised in a situation like this?
I've tried using the Application.ItemLoad event, but I can't access any methods or properties after I successfully cast it to an Outlook.MailItem (I get an error stating System.Runtime.InteropServices.COMException: The item’s properties and methods cannot be used inside this event procedure). I'm using C# in Visual Studio 2010.

If the Outlook.exe process is not running then the NewInspector won't fire since ThisAddIn_Startup is not called except when the user opens up Outlook directly.
Since Outlook is not already running when the external application opens the new Inspector window - you would have to manually call ThisAddIn_Startup yourself or attach any custom events that need to fire when the Inspector ribbon is loaded. The best place to do this is by handling CreateRibbonExtensibilityObject or RequestService methods.
protected override object RequestService(Guid serviceGuid)
{
if (serviceGuid == typeof(Office.IRibbonExtensibility).GUID)
this.ThisAddIn_Startup(this, null);
return base.RequestService(serviceGuid);
}
The only caveat with this is that you need to support method re-entry with ThisAddIn_Startup since the Ribbon and Outlook can both call the startup routine now. You will need to safely administer a lock to ensure you don't keep calling your Init (ThisAddIn_Startup) routine more than once.

Related

How to get save confirmation dialog result of outlook

I don't know how to get result of save confirmation dialog of outlook (in C#).
This confirmation box is opened up by following:
Open mail in outlook
Made some changes in content
Close the mail
Please refer this image
I want to get this opened save confirm box and its result.
Thanks in advance.
Track the MailItem.Close / Write / AfterWrite events - Close will fire immediately before the prompt is shown, Write fires before the message is saved, AfterWrite immediately afterward.
There is no trivial way of getting such information. But you may track the sequence of events fired for the item and realize the answer following that way.
First, to handle item-level events you may develop a wrapper for an Outlook item like described in the Implement a wrapper for inspectors and track item-level events in each inspector article.
The MailItem.Close event is fired when the inspector associated with an item (which is an instance of the parent object) is being closed.
The MailItem.Write event is fired when an instance of the parent object is saved, either explicitly (for example, using the Save or SaveAs methods) or implicitly (for example, in response to a prompt when closing the item's inspector).
So, if you have got Close fired following by the Write event - that is your case!

PropertyChange not fire on Reading Panel when I reply or forward email on Office 2013

I am using Add-in Express(v 7.6.4084) to create an Outlook plugin.
The problem is, that the MailItem.PropertyChange isn't triggered when reply an email on reading panel.
I have searched several topics but wasn't able to find a solution.
Seems you are talking about the inline response feature. If so, you need to use the ActiveInlineResponse property to get an item object representing the active inline response item in the explorer reading pane. It fires the PropertyChange event as expected on my PC with Outlook 2013 installed.
Note, you need to declare the source object at the global scope to prevent it from being swiped by the garbage collector.

C# VSTO Outlook 2013 - Explorer Close handle Save Event

I have a RibbonBar with some Buttons that appears in the Outlook Explorer (Double Click on a mail).
Some progresses in the background edit the attachments of the mail. If the user close the Explorer, outlook will ask to save the edited mail (save the changes).
is there a Event to handle this? i don't want that the message appear if my programm has changed the mail.
I wasn't succesfull on the search for an Event. But maybe i just missed something.
There is no need to handle any event for this.
If the user close the Explorer, outlook will ask to save the edited mail (save the changes).
This is a good indicator that changes were not saved or Outlook objects (actually underlying COM objects) were not released correctly. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. You can read more about that in the Systematically Releasing Objects article in MSDN.
Some progresses in the background edit the attachments of the mail.
Don't use the Outlook object model from secondary threads. Office applications (Outlook in your case) use the single threaded apartment model. You can gather the required info on the main thread (keep it in a string for example) and process it on another thread. Or you can use a low-level API (Extended MAPI) which allows to run multiple threads. Also you may consider using any wrapper around that API. Frankly speaking, Outlook is a big wrapper around that API.

Office Communicator: How to identify the opening of an IM window

I'm developing an "automatic message" add-in for Office Communicator 2007, but I need to know how to identify if another user opens the IM Window (not me, but another user).
I have the following event:
private void communicator_OnIMWindowCreated(object pIMWindow)
{
if ((chk_Enabled.Checked))
{
IMessengerConversationWndAdvanced imWindow = pIMWindow as IMessengerConversationWndAdvanced;
imWindow.SendText(TxtAutoMessage.Text);
}
}
Is there a way? Thanks!
unfortunately the Communicator Automation API doesn't support this directly. The only workaround I've found involves trapping the OnIMWindowContactAdded event.
For a conversation started by you, the following events fire in this order:
OnIMWindowCreated
OnIMWindowContactAdded (for yourself)
OnIMWindowContactAdded (for the other participant)
For a conversation started by another participant, the following events fire in this order:
OnIMWindowCreated
OnIMWindowContactAdded (for the other participant)
So when the participant initiates the conversation, you don't see yourself added as the contact.
You could use this as follows
On trapping OnIMWindowCreated, store the window handle (pIMWindow.HWND) in a dictionary (so you can handle multiple conversation windows)
On trapping OnIMWindowContactAdded, look for the handle in the dictionary. If this is the first Added event you've seen for the window, the rule is: if the contact is you (IsSelf), then you started the conversation. Otherwise, another contact started the conversation.
It's not the most satisfactory solution (they never are when you work with the Automation API ;o) ), but it should get you there.

Can not create CommandBar when Outlook is run minimized (Outlook 2007 Add-In)

Original post:
When Outlook is launched, the Add-In is loaded and adds a toolbar with some buttons.
toolBar = OutlookApp.ActiveExplorer().CommandBars.Add(MENU_TAG, MsoBarPosition.msoBarTop, false, true);
Everything was working fine, but now one user has his Outlook shortcut set to launch Outlook minimized.
And then OutlookApp.ActiveExplorer() return null.
Is there some event I can use to catch when there is an ActiveExplorer and then add the commandbar?
OutlookApp.Explorers.NewExplorer doesn't work.
Also, when I show a messagebox before I add the CommandBar: everything works fine, even with Outlook minimized... Why?
edit:
Accessing the Explorers proprerty directly did work, as someone said in the answers. So this solves the problem for a minimized outlook... But...
One of the users does not have Outlook run minimized, and still the plugin loads before any gui is available. There are not even 1 explorer in the Explorers collection :( How is this possible?
edit 2:
I tried using a timer as suggested by 76mel, checking ActiveExplorer for null every 100ms. This adds the buttons as expected, but I can set the Picture property of the button.
I get this exception:
Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))
If there is no UI = no explorer :(
Try waiting until the Explorers.NewExplorer event fires to be able to get a CommandBars object.
Update:
Yes it looks like a timer will do the trick ok a bit hacky.
So wire up a timer when you have a null ActiveExplorer and check for the ActiveExplorer onTick. Once the user pops outlook you get you active explorer and you can then add you tool bars.
I'm not familiar with managed addins, but I found this answer.
If there is no ActiveExplorer, try to access the Explorers collections directly, like in Explorers[1].
I had the same problem in my ECE and solved it by waiting for OnObjectChange callback that would be called when the user changes a folder in Outlook, and then I try to recreate the toolbar. This might roughly correspond to the FolderSwitch event in the Outlook object model.
Just my 2c.

Categories