How to handle PropertyChangeEventHandler in outlook

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

How to handle PropertyChangeEventHandler in outlook
 
Mishal Parmar


Guest


Hello,

We are using add-in express for office and .net.

How to handle ItemEvents_10_PropertyChangeEventHandler. When two compose windows are open.
When i change sending method from latest open compose window it gave me event. But when i change sending method from another compose window it didn't give any event.

Can you help how to handle those event when multiple compose window are open.?




public static MailItem mailItem;
private void adxOutlookAppEvents1_NewInspector(object sender, object inspector, string folderName)
        {
            Outlook.Inspector ins = (Outlook.Inspector)inspector;
            object obj = ins.CurrentItem;
            if (obj is Outlook.MailItem)
            {
                mailItem = (Outlook.MailItem)obj;

                mailItem.PropertyChange += new ItemEvents_10_PropertyChangeEventHandler(OnMail_PropertyChange);
                mailItem.Open -= new ItemEvents_10_OpenEventHandler(OnMailItemReadable);
                mailItem.Open += new ItemEvents_10_OpenEventHandler(OnMailItemReadable);
                ((ItemEvents_10_Event)mailItem).Close -= new ItemEvents_10_CloseEventHandler(OnMailItemClose);
                ((ItemEvents_10_Event)mailItem).Close += new ItemEvents_10_CloseEventHandler(OnMailItemClose);               

            }
        }

   private void OnMail_PropertyChange(string property)
        {
            if (property == "SendUsingAccount")
            {
              // actions
            }
        }
Posted 25 Sep, 2018 06:17:43 Top
Andrei Smolin


Add-in Express team


Posts: 18787
Joined: 2006-05-11
Hello Mishal,

The issue may be caused by using a single mailitem variable; I suppose you would need to use an array of such variables.

I suggest that you don't connect to events of an Outlook item in this way. Instead, you can use the Outlook Item (not items!) Events class; see the Add New Item dialog of your add-in project. You use this class to specify a set of rules; the rules are used to handle the Outlook item the events class is connected in a specific way. To handle other mail items in a different ways you specify another events class - another set of handling rules. Please check section Events Classes, see the PDF file in the folder {Add-in Express}\Docs on your development PC.

Also note that you should release COM objects created in your code to avoid possible issues. I strongly suggest that you also check section Releasing COM objects in the manual.


Andrei Smolin
Add-in Express Team Leader
Posted 25 Sep, 2018 08:06:28 Top
Mishal Parmar


Guest


Hello Andrei,

Thank you for your respond. As you suggested i have implemented Outlook Item Events class.

I am confuse how to manage multiple outlook item event class.
Now i need to connect this event class for multiple Compose mail and inline reply of mail also.

So which way should i implement?
When i should ConnectTo to mailitem and how i manage all the instances of the OutlookItemEvents class.


private void adxOutlookAppEvents1_NewInspector(object sender, object inspector, string folderName) 
{ 
	Outlook.Inspector ins = (Outlook.Inspector)inspector; 
	object obj = ins.CurrentItem; 
	if (obj is Outlook.MailItem) 
	{ 
		mailItem = (Outlook.MailItem)obj; 

	   ConnectToEventsOfSelectedItem(ins.CurrentItem);             

	} 
} 

OutlookItemEvents originalEmailEvents = null;
private void ConnectToEventsOfSelectedItem(object mailItem)
{

	try
	{
		if (originalEmailEvents != null && originalEmailEvents.IsConnected) 
		originalEmailEvents.RemoveConnection();
		originalEmailEvents = new OutlookItemEvents(this);
		originalEmailEvents.ConnectTo(mailItem, true);

	}
	catch (System.Exception ex)
	{
		Log.WriteLine("Exception: " + ex.Message);
	}
	finally
	{
		if (explorer != null) Marshal.ReleaseComObject(explorer);
		if (selection != null) Marshal.ReleaseComObject(selection);
		if (item != null && !(item is Outlook._MailItem)) Marshal.ReleaseComObject(item);
	}
}




public class OutlookItemEvents : AddinExpress.MSO.ADXOutlookItemEvents
{
	public OutlookItemEvents(AddinExpress.MSO.ADXAddinModule module): base(module)
	{
	}

	public override void ProcessCustomPropertyChange(string name)
	{
		// TODO: Add some code
	}

	public override void ProcessForward(object forward, AddinExpress.MSO.ADXCancelEventArgs e)
	{
		// TODO: Add some code
	}

	public override void ProcessOpen(AddinExpress.MSO.ADXCancelEventArgs e)
	{
		// TODO: Add some code
	}

	public override void ProcessPropertyChange(string name)
	{
		// TODO: Add some code
	}

	public override void ProcessRead()
	{
		// TODO: Add some code
	}

	public override void ProcessReply(object response, AddinExpress.MSO.ADXCancelEventArgs e)
	{
		// TODO: Add some code
	}

	public override void ProcessReplyAll(object response, AddinExpress.MSO.ADXCancelEventArgs e)
	{
		// TODO: Add some code
	}

	public override void ProcessSend(AddinExpress.MSO.ADXCancelEventArgs e)
	{
		// TODO: Add some code
	}

	public override void ProcessWrite(AddinExpress.MSO.ADXCancelEventArgs e)
	{
		// TODO: Add some code
	}

	

	public override void ProcessBeforeRead()
	{
		// TODO: Add some code
	}

	public override void ProcessAfterWrite()
	{
		// TODO: Add some code
	}
}

Posted 26 Sep, 2018 02:49:28 Top
Andrei Smolin


Add-in Express team


Posts: 18787
Joined: 2006-05-11
Hello Mishal,

You should create an array (or dictionary) of OutlookItemEvents instances. You should disconnect an OutlookItemEvents instance from an item when the item is closed; also, delete the OutlookItemEvents instance from the array (dictionary) at this moment.


Andrei Smolin
Add-in Express Team Leader
Posted 26 Sep, 2018 03:53:14 Top
Mishal Parmar


Guest


Hello Andrei,


What should be dictionary key for that. So that would be easy to identify unique object of mailitem.

So that it help me to remove and add object.

I have to create instance at time of NewInspector and ExplorerInlineResponseEx.

And disconnect and remove those instance in ProcessClose of OutlookItemEvents class.


Regards
Mishal Parmar
Posted 26 Sep, 2018 04:41:36 Top
Andrei Smolin


Add-in Express team


Posts: 18787
Joined: 2006-05-11
Mishal Parmar writes:
What should be dictionary key for that.


An integer identifying an instance of the OutlookItemEvents class. Below is a raw sketch; you may need to verify and polish it:


public class OutlookItemEventsClass1 : AddinExpress.MSO.ADXOutlookItemEvents
{
    public static int counter { get; private set; }
    public int index { get; private set; }
    public OutlookItemEventsClass1(AddinExpress.MSO.ADXAddinModule module): base(module)
    {
        index = counter;
        counter++;
    }



Andrei Smolin
Add-in Express Team Leader
Posted 26 Sep, 2018 05:06:25 Top
Mishal Parmar


Guest


Hello Andrei,

Thank you for your response. Thats solve the my problem.

I have connected multiple sending method in outlook.
for ex. I have opened compose window with mishal@xyz.com and same time i am doing inline Reply with mishal@pqr.com.

When i am switching to compose and came back to inline reply response window i need trigger event.
At that event i want email address of sending method.

I have try using Explore Active window event. But, it gave me email address of Compose window.
What should i have to do to get proper event and proper email address.

-regards
Mishal Parmar
Posted 27 Sep, 2018 10:58:00 Top
Andrei Smolin


Add-in Express team


Posts: 18787
Joined: 2006-05-11
Hello Mishal,

Mishal Parmar writes:
I have try using Explore Active window event. But, it gave me email address of Compose window.


It? What's it? I assume the "it" is the Explorer.Selection property. You need to use Explorer.ActiveInlineResponse instead.


Andrei Smolin
Add-in Express Team Leader
Posted 27 Sep, 2018 11:27:24 Top
Mishal Parmar


Guest


Hello Andrei,

It means MailItem. MailItem of active explorer. While switching from Compose window to Inline Response.
I am getting email address of compose window in below code instead of inline respond mailItem sender email address.

In compose window email address is mishal@xyz.com and inline reply email address is mishal@pqr.com.
So while switching window to inline reply. I get event of Explorer Activate. In this below code as a senderMail address i am getting email as "mishal@xyz.com". That is wrong. I want email address as mishal@pqr.com, which is set in inline reply window.


private void adxOutlookAppEvents1_ExplorerActivate(object sender, object explorer)
{
	if (explorer is Outlook.Explorer)
	{
		var current_explorer = (Outlook.Explorer)explorer;

		if (AppConfig.IsInlinResponseSupported && current_explorer.ActiveInlineResponse != null)
		{
			var replyMail = current_explorer.ActiveInlineResponse as Outlook.MailItem;
			if (replyMail == null)
			{
				return;
			}

			string senderMail = string.Empty;

			var senderAddressEntry = replyMail.Sender;
			if (senderAddressEntry != null)
			{
				senderMail = senderAddressEntry.Address;
			}
			if (replyMail.SendUsingAccount != null)
			{
				senderMail = replyMail.SendUsingAccount.SmtpAddress;
			}
		}
	}
}
Posted 28 Sep, 2018 05:03:00 Top
Andrei Smolin


Add-in Express team


Posts: 18787
Joined: 2006-05-11
Mishal Parmar writes:
It means MailItem. MailItem of active explorer.


There are two different MailItem objects that may be available in the active explorer window: 1) MailItem selected: Explorer.Selection[1], and 2) MailItem edited: Explorer.ActiveInlineResponse. Note that although I refer to MailItem, the selected item can be of any other item type: TaskItem, ContactItem, whatever. Your code should foresee this to avoid an exception.

Mishal Parmar writes:
In compose window email address is mishal@xyz.com and inline reply email address is mishal@pqr.com. So while switching window to inline reply. I get event of Explorer Activate. In this below code as a senderMail address i am getting email as "mishal@xyz.com". That is wrong.


In the code fragment provided, you do not release COM objects. Please check section Releasing COM objects - see the PDF file in the folder {Add-in Express}\Docs on your development PC; note that you may be required to check other links that this section provides - release COM objects that your code creates and let me know whether this fixes the issue or not.

What Outlook build are you using?


Andrei Smolin
Add-in Express Team Leader
Posted 28 Sep, 2018 06:27:19 Top