<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Implementing IMessageFilter in an Office add-in</title><link>http://blogs.msdn.com/andreww/archive/2008/11/19/implementing-imessagefilter-in-an-office-add-in.aspx</link><description>First a warning: this is an advanced scenario, and you should not attempt to use this technique unless you’re sure you know what you’re doing. The reason for this warning is that while the technique described here is pretty simple, it’s also easy to get</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Implementing IMessageFilter in an Office add-in | MS Tech News</title><link>http://blogs.msdn.com/andreww/archive/2008/11/19/implementing-imessagefilter-in-an-office-add-in.aspx#9127491</link><pubDate>Thu, 20 Nov 2008 08:39:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9127491</guid><dc:creator>Implementing IMessageFilter in an Office add-in | MS Tech News</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://mstechnews.info/2008/11/implementing-imessagefilter-in-an-office-add-in/"&gt;http://mstechnews.info/2008/11/implementing-imessagefilter-in-an-office-add-in/&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: Implementing IMessageFilter in an Office add-in</title><link>http://blogs.msdn.com/andreww/archive/2008/11/19/implementing-imessagefilter-in-an-office-add-in.aspx#9146084</link><pubDate>Thu, 27 Nov 2008 12:40:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9146084</guid><dc:creator>incre-d</dc:creator><description>&lt;p&gt;I presumed I could use this technique in Excel 2003. I'm currently using VS2008 with Excel 2003.&lt;/p&gt;
&lt;p&gt;I see that if I have pressed f5 i can get the &amp;quot;retry?&amp;quot; message.&lt;/p&gt;
&lt;p&gt;From this article I was hoping I could replace the Application.OnTime event in VBA. I've experimented with various things mostly catching the exception and waiting till it's no longer fired. I was hoping this article would get around that technique.&lt;/p&gt;
&lt;p&gt;My standard test is to block excel by holding down the mouse with a range selected, but the error is then thrown in the CallExcel method.&lt;/p&gt;
&lt;p&gt;Additionally sometimes I get &lt;/p&gt;
&lt;p&gt;{&amp;quot;COM object that has been separated from its underlying RCW cannot be used.&amp;quot;}. I especially get this if I've blocked the thread with a form, or holding down the mouse. These error messages will continue. If I leave the workbook for a bit then it eventually works once, and then fails again. So the failures look sporadic.&lt;/p&gt;
&lt;p&gt;I found 3 threads on the forums which deal with this message.&lt;/p&gt;
&lt;p&gt;My Implementation:&lt;/p&gt;
&lt;p&gt;I implemented your code then added code to fire workbooknewsheet event which then calls the InvokeAsyncCallToExcel()&lt;/p&gt;
&lt;p&gt;Regards&lt;/p&gt;
&lt;p&gt;code from my addin...&lt;/p&gt;
&lt;p&gt;private void ThisAddIn_Startup(object sender, System.EventArgs e)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;#region VSTO generated code &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;this.Application = (Excel.Application)Microsoft.Office.Tools.Excel.ExcelLocale1033Proxy.Wrap(typeof(Excel.Application), this.Application);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;this.Application.WorkbookNewSheet += new Microsoft.Office.Interop.Excel.AppEvents_WorkbookNewSheetEventHandler(Application_WorkbookNewSheet);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;#endregion&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;void Application_WorkbookNewSheet(Microsoft.Office.Interop.Excel.Workbook Wb, object Sh)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;InvokeAsyncCallToExcel();&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
</description></item><item><title>re: Implementing IMessageFilter in an Office add-in</title><link>http://blogs.msdn.com/andreww/archive/2008/11/19/implementing-imessagefilter-in-an-office-add-in.aspx#9163410</link><pubDate>Tue, 02 Dec 2008 09:59:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9163410</guid><dc:creator>andreww</dc:creator><description>&lt;p&gt;incre-d - I've tested the exact same code with a new solution built with VS2008 to target Excel 2003, and it behaves in exactly the same way.&lt;/p&gt;
&lt;p&gt;Using the code I provided, I don't see a reason for the COM object/RCW separation error you're getting - although, of course, once you do get this error, there's really nothing you can do to recover - and you have to assume your application is in an indeterminate state.&lt;/p&gt;
&lt;p&gt;The technique I describe is really only useful in the scenarios I mention. I don't really see how you could use it to model the Application.Time behavior - that method is used to schedule a VBA macro procedure to be run at a specified time in the future, which seems to be unrelated to the scenarios I describe.&lt;/p&gt;
</description></item><item><title>re: Implementing IMessageFilter in an Office add-in</title><link>http://blogs.msdn.com/andreww/archive/2008/11/19/implementing-imessagefilter-in-an-office-add-in.aspx#9164067</link><pubDate>Tue, 02 Dec 2008 13:42:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9164067</guid><dc:creator>incre-d</dc:creator><description>&lt;p&gt;Hi, &lt;/p&gt;
&lt;p&gt;So I went and tested the original zip file on a fresh build of VS 2008 sp1 with excel 2007.&lt;/p&gt;
&lt;p&gt;I have 3 scenarios, and the log I get below.&lt;/p&gt;
&lt;p&gt;I see in your code you specifically comment that it is to handle a modal form being open, and waiting for user input. And in that scenario, it works as intended/expected. &lt;/p&gt;
&lt;p&gt;The scenario I am trying to find an elegant solution for is where the user is holding down the mouse key with a range selected. Not exactly what your code was written for (the modal form) but to me a similar issue, how to handle when excel is locking the thread when the mouse key is pressed. Perhaps there is no need to elegantly handle this, and catching the exception and re-firing the failed event after a delay is sufficient. But I was hoping there was a native excel method of handling this type of issue.&lt;/p&gt;
&lt;p&gt;Many thanks&lt;/p&gt;
&lt;p&gt;My 3 scenarios&lt;/p&gt;
&lt;p&gt;--------------&lt;/p&gt;
&lt;p&gt;Press Asynch with no user interference.&lt;/p&gt;
&lt;p&gt;----&lt;/p&gt;
&lt;p&gt;The thread 0x19848 has exited with code 0 (0x0).&lt;/p&gt;
&lt;p&gt;Press Asynch, immediately follow with F5, wait for retry message, press cancel and retry.&lt;/p&gt;
&lt;p&gt;----&lt;/p&gt;
&lt;p&gt;RetryRejectedCall&lt;/p&gt;
&lt;p&gt;MessagePending&lt;/p&gt;
&lt;p&gt;The thread 0x1a5f4 has exited with code 0 (0x0).&lt;/p&gt;
&lt;p&gt;Press Asynch, highlight a range, and keep the mouse button down.&lt;/p&gt;
&lt;p&gt;----&lt;/p&gt;
&lt;p&gt;A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll&lt;/p&gt;
&lt;p&gt;System.Runtime.InteropServices.COMException (0x800AC472): Exception from HRESULT: 0x800AC472&lt;/p&gt;
&lt;p&gt;Server stack trace: &lt;/p&gt;
&lt;p&gt;Exception rethrown at [0]: &lt;/p&gt;
&lt;p&gt; &amp;nbsp; at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&amp;amp; msgData, Int32 type)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; at Microsoft.Office.Interop.Excel.Range.set_Value2(Object )&lt;/p&gt;
&lt;p&gt; &amp;nbsp; at ExcelAddInMessageFilter.ThisAddIn.CallExcel() in C:\development\ExcelAddInMessageFilter\ExcelAddInMessageFilter\ExcelAddInMessageFilter\ThisAddIn.cs:line 97&lt;/p&gt;
&lt;p&gt;The thread 0x1a35c has exited with code 0 (0x0).&lt;/p&gt;
&lt;p&gt;Exception rethrown at [0]: &lt;/p&gt;
&lt;p&gt; &amp;nbsp; at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&amp;amp; msgData, Int32 type)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; at Microsoft.Office.Interop.Excel.Range.set_Value2(Object )&lt;/p&gt;
&lt;p&gt; &amp;nbsp; at ExcelAddInMessageFilter.ThisAddIn.CallExcel() in C:\development\ExcelAddInMessageFilter\ExcelAddInMessageFilter\ExcelAddInMessageFilter\ThisAddIn.cs:line 97&lt;/p&gt;
&lt;p&gt;The thread 0x1a4b4 has exited with code 0 (0x0).&lt;/p&gt;
</description></item><item><title>re: Implementing IMessageFilter in an Office add-in</title><link>http://blogs.msdn.com/andreww/archive/2008/11/19/implementing-imessagefilter-in-an-office-add-in.aspx#9172922</link><pubDate>Thu, 04 Dec 2008 04:53:28 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9172922</guid><dc:creator>andreww</dc:creator><description>&lt;p&gt;incre-d - thanks for the clarification. I believe this specific scenario is standard Excel behavior, by design.&lt;/p&gt;
&lt;p&gt;You'll notice that while the user has the mouse key down as part of a selection, the worksheet is not writeable, although calls to the OM that only attempt to read worksheet values will succeed.&lt;/p&gt;
&lt;p&gt;So, in my example, if you replace this:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;this.Application.ActiveCell.Value2 = &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;DateTime.Now.ToShortTimeString();&lt;/p&gt;
&lt;p&gt;...with, say, this:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;Debug.WriteLine(this.Application.ActiveCell.Value2.ToString());&lt;/p&gt;
&lt;p&gt;then, the call succeeds.&lt;/p&gt;
</description></item><item><title>Multiple Threads</title><link>http://blogs.msdn.com/andreww/archive/2008/11/19/implementing-imessagefilter-in-an-office-add-in.aspx#9237940</link><pubDate>Fri, 19 Dec 2008 00:30:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9237940</guid><dc:creator>Leor</dc:creator><description>&lt;p&gt;Above, you state:&lt;/p&gt;
&lt;p&gt;&amp;quot;If you don’t create any background threads in your add-in, and therefore make all OM calls on the same thread your add-in was created on, your call won’t fail, it simply won’t be invoked until the host is unblocked.&amp;quot;&lt;/p&gt;
&lt;p&gt;In my application, I am not explicitly creating new threads. &amp;nbsp;I have a console application which manipulates an office application. &amp;nbsp;The application is written in C#, but uses components written in VB. &amp;nbsp;It appears as if my client is having it's calls rejected by the office application because it is too busy.&lt;/p&gt;
&lt;p&gt;Question: &amp;nbsp;Do you have any idea why this may be the case, if my application should hang and not issue any new calls until the previous one has returned? &amp;nbsp;Either way, I will try implementing the message filter given above. &amp;nbsp;Thanks!&lt;/p&gt;
</description></item><item><title>re: Implementing IMessageFilter in an Office add-in</title><link>http://blogs.msdn.com/andreww/archive/2008/11/19/implementing-imessagefilter-in-an-office-add-in.aspx#9253895</link><pubDate>Sat, 27 Dec 2008 01:13:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9253895</guid><dc:creator>andreww</dc:creator><description>&lt;p&gt;Leor - the scenario I describe is an in-process add-in. The scenario you are describing is an out-of-process automation client. Obviously, making calls cross-process in this way will have superficially similar results to making calls cross-thread in-process.&lt;/p&gt;
</description></item><item><title>re: Implementing IMessageFilter in an Office add-in</title><link>http://blogs.msdn.com/andreww/archive/2008/11/19/implementing-imessagefilter-in-an-office-add-in.aspx#9253911</link><pubDate>Sat, 27 Dec 2008 01:38:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9253911</guid><dc:creator>Leor</dc:creator><description>&lt;p&gt;Thanks. &amp;nbsp;I had no idea. &amp;nbsp;I thought they were both out of process.&lt;/p&gt;
&lt;p&gt;For those of you interested, it turns out that I created a message filter, implemented it, and co-registered it. &amp;nbsp;However, it appears as if it is not registering. &amp;nbsp;Perhaps they need to be in the same process in order to register.&lt;/p&gt;
&lt;p&gt;Overall, I'd say that my software solution is pretty complicated, and I wouldn't be surprised if I find out that the reason its not running properly is because it has something to do with this (&lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/vbteam/archive/2008/03/24/stathread-vs-mtathread-whorst.aspx"&gt;http://blogs.msdn.com/vbteam/archive/2008/03/24/stathread-vs-mtathread-whorst.aspx&lt;/a&gt;).&lt;/p&gt;
</description></item><item><title>re: Implementing IMessageFilter in an Office add-in</title><link>http://blogs.msdn.com/andreww/archive/2008/11/19/implementing-imessagefilter-in-an-office-add-in.aspx#9427668</link><pubDate>Tue, 17 Feb 2009 18:07:02 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9427668</guid><dc:creator>Richard (VSTO EE in Microsoft Support)</dc:creator><description>&lt;p&gt;A little warning to those who attempt to implement this: &lt;/p&gt;
&lt;p&gt;(1) Depending on your situation, it may not apply to your code in the manner you think it should. For example, since events raised by delegates in .NET can run from worker threads which don't use the message filter becasue (1) they are running in MTA and (2) their are multiple threads (pooled) in which the event might be raised, making it impossible to fully know what thread will handle the event in all cases. &lt;/p&gt;
&lt;p&gt;(2) Implementing this in managed code is actually risky because the COM Interop layer actually allows for reentrancy during invocation of COM calls. This is a problem because this interfaces is expected to be input-synchronous (meaning it is not allowed to switch to another thread during the handling of the funtions in this callback). &amp;nbsp;This caries the same risk of deadlock that running manauged code from loader lock has. &lt;/p&gt;
&lt;p&gt;This solution can work, but it can also blow up in your face. So there is a risk/reward tradeoff here. Just FYI in case you find it didn't solve your problem or added some new ones.&lt;/p&gt;
</description></item><item><title>re: Implementing IMessageFilter in an Office add-in</title><link>http://blogs.msdn.com/andreww/archive/2008/11/19/implementing-imessagefilter-in-an-office-add-in.aspx#9427826</link><pubDate>Tue, 17 Feb 2009 19:38:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9427826</guid><dc:creator>andreww</dc:creator><description>&lt;p&gt;Richard is absolutely correct - this is a technique that can be useful in some scenarios, but that can easily go horribly wrong. That's why I started the post with a stern warning - and it is a warning that certainly bears re-iteration.&lt;/p&gt;
</description></item><item><title>Message Hooks in Add-ins</title><link>http://blogs.msdn.com/andreww/archive/2008/11/19/implementing-imessagefilter-in-an-office-add-in.aspx#9443374</link><pubDate>Wed, 25 Feb 2009 03:27:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9443374</guid><dc:creator>Andrew Whitechapel</dc:creator><description>&lt;p&gt;Just like my earlier post on message filters , this is an advanced scenario – so be warned: you almost&lt;/p&gt;
</description></item><item><title>Implementing IMessageFilter in an Powerpoint add-in</title><link>http://blogs.msdn.com/andreww/archive/2008/11/19/implementing-imessagefilter-in-an-office-add-in.aspx#9588594</link><pubDate>Tue, 05 May 2009 15:53:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9588594</guid><dc:creator>mangesh</dc:creator><description>&lt;p&gt;Hi Andrew, &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; I am implementing a Powerpoint 2007 VSTO 2005 SE addin and I wish to show a Progress dialog, which displays the messages related to the actions being performed in &amp;nbsp;a &amp;nbsp;Synchronous BackgroundWorker Thread. The background worker thread extracts information from the powerpoint application and does some processing. &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;However I get the exception quoted below.&lt;/p&gt;
&lt;p&gt;&amp;quot;The message filter indicated that the application is busy. (Exception from HRESULT: 0x8001010A (RPC_E_SERVERCALL_RETRYLATER))&amp;quot;&lt;/p&gt;
&lt;p&gt;The progress dialog ( win form) merely has a label to display the message from the background thread and a cancel button to cancel the processing in the background thread.&lt;/p&gt;
&lt;p&gt;Do you think I should try out the approach mentioned here. Will it help me. What are the pitfalls.&lt;/p&gt;
</description></item><item><title>re: Implementing IMessageFilter in an Office add-in</title><link>http://blogs.msdn.com/andreww/archive/2008/11/19/implementing-imessagefilter-in-an-office-add-in.aspx#9589215</link><pubDate>Tue, 05 May 2009 19:49:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9589215</guid><dc:creator>andreww</dc:creator><description>&lt;p&gt;mangesh - the key issue is that you're doing things on another thread. Calls on the main thread simply get queued. Calls on another thread may get rejected - which is what you're seeing. If you're already catching the exception, you can simply retry the call. To re-iterate my comments about the use of message filters - I would avoid using this technique if you possibly can.&lt;/p&gt;
</description></item></channel></rss>