<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>Excel Programming</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/gabhan_berry/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/gabhan_berry/feed.xml</link>
    <description>An Excel Programmability blog by Gabhan Berry</description>
    <lastBuildDate>Fri, 10 Apr 2015 19:55:12 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Custom Task Panes</title>
      <link>https://docs.microsoft.com/archive/blogs/gabhan_berry/custom-task-panes</link>
      <pubDate>Wed, 13 Aug 2008 18:29:00 GMT</pubDate>
      <dc:creator><![CDATA[Gabhan Berry]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/gabhan_berry/2008/08/13/custom-task-panes/</guid>
      <description><![CDATA[A Custom Task Pane (CTP) is a user interface component&nbsp;in Office which is used to provide a...]]></description>
      <content:encoded><![CDATA[<P>A Custom Task Pane (CTP) is a user interface component&nbsp;in Office which is used to provide a non-modal window. Some of Excel's features, such as the PivotTable field list and Reseach tool, use CTPs.&nbsp;&nbsp;</P>
<P mce_keep="true">CTPs are exposed to customer extensions via the Office extensibility COM libraries.&nbsp;Any Excel developer can develop their own CTP and have it work just like the built-in CTPs. </P>
<P>Despite what some people think,&nbsp;you do not need&nbsp;VSTO to build a CTP.</P>
<P>In this post I will develop an Excel feature called: <EM>annotations</EM>. Annotations (as implemented here) are basically free-from textual comments that can be added to a workbook. They are added into the file but are not displayed on the grid. You can use them to store notes or commentary&nbsp;in&nbsp;the workbook. They are displayed and edited in a Custom Task Pane as shown below. </P>
<P mce_keep="true"><IMG src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/08/86/22/20/CTP_1.png" original-url="http://blogs.msdn.com/photos/gabhan_berry/images/8862220/original.aspx" mce_src="http://blogs.msdn.com/photos/gabhan_berry/images/8862220/original.aspx"></P>
<P>The annotations&nbsp;are stored within the CustomXMLParts collection of the workbook. CustomXMLParts are a new feature in Excel 2007 which enable Excel developers to store their own XML within a workbook. I will cover CustomXMLParts in my next post. </P>
<P mce_keep="true"><STRONG>What are Custom Task Panes?</STRONG></P>
<P mce_keep="true">A CTP is a simple window that contains an ActiveX control. Excel manages the CTP window (creates it, destroys it, handles its window messages etc.) and the ActiveX control&nbsp;provides all the features. The CTP is really just a container, but a container&nbsp;that is nicely integrated into&nbsp;Excel.&nbsp;A CTP can be docked&nbsp;inside&nbsp;the main Excel&nbsp;window or can float. The user can resize it, move it&nbsp;and close it and all of this is handled for us by Excel. </P>
<P mce_keep="true"><STRONG>Building a Custom Task Pane</STRONG></P>
<P mce_keep="true">Creating a CTP itself is very simple. CTP functionality is provided&nbsp;via a COM interface called <FONT face="Courier New"><SPAN style="COLOR: #2b91af">ICustomTaskPaneConsumer</SPAN></FONT>. From a .NET point of view, this interface is implemented&nbsp;in <FONT face="Courier New">Microsoft.Office.Core</FONT>.&nbsp;When Excel loads our COM addin it performs a <FONT face="Courier New">QueryInterface </FONT>call to see whether or not our addin implements the <SPAN style="COLOR: #2b91af"><FONT face="Courier New">ICustomTaskPaneConsumer </FONT></SPAN>interface.&nbsp;If&nbsp;it does, Excel calls the <SPAN style="COLOR: #2b91af"><FONT face="Courier New">ICustomTaskPaneConsumer</FONT></SPAN>.<FONT face="Courier New">CTPFactoryAvailable </FONT>method. It is&nbsp;in this&nbsp;method that our addin creates the CTP. </P>
<P mce_keep="true">Let's take a look at the code.</P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">public</SPAN><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: blue">class</SPAN> <SPAN style="COLOR: #2b91af">Connect</SPAN> : <SPAN style="COLOR: #2b91af">Object</SPAN>, Extensibility.<SPAN style="COLOR: #2b91af">IDTExtensibility2</SPAN>, <SPAN style="COLOR: #2b91af">IRibbonExtensibility</SPAN>, <SPAN style="COLOR: #2b91af">ICustomTaskPaneConsumer</SPAN> {<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&nbsp; public</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: blue">void</SPAN> CTPFactoryAvailable(<SPAN style="COLOR: #2b91af">ICTPFactory</SPAN> CTPFactoryInst) {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>_ctpFactory = CTPFactoryInst;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: #2b91af">CustomTaskPane</SPAN> ctp = _ctpFactory.CreateCTP(<SPAN style="COLOR: #a31515">"ExcelExtensions.AnnotationsCtrl"</SPAN>, <SPAN style="COLOR: #a31515">"Annotations"</SPAN>, <SPAN style="COLOR: #2b91af">Missing</SPAN>.Value);</SPAN></P><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&nbsp;&nbsp;&nbsp; _annotationsExt = <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">AnnotationsExtension</SPAN>(ctp, _application);<o:p></o:p></SPAN></P><o:p></o:p></SPAN>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;</SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">}</SPAN></P>
<P mce_keep="true">The <SPAN style="COLOR: #2b91af"><FONT face="Courier New">Connect</FONT></SPAN> class implements the <FONT face="Courier New"><SPAN style="COLOR: #2b91af">ICustomTaskPaneConsumer</SPAN> </FONT>interface. Its implementation of the <FONT face="Courier New">CTPFactoryAvailable</FONT> method does two things. Firstly, it stores the <SPAN style="COLOR: #2b91af"><FONT face="Courier New">ICTPFactory</FONT></SPAN> object passed to it by Excel and secondly it creates a CTP using the <SPAN style="COLOR: #2b91af"><FONT face="Courier New">ICTPFactory</FONT></SPAN>.<FONT face="Courier New">CreateCTP</FONT> method. The first parameter to the <FONT face="Courier New">CreateCTP</FONT> method is the ProgId of the ActiveX control that the CTP is to host. The CTP will create an instance of this ActiveX control and place it within the CTP window.&nbsp;&nbsp;</P>
<P mce_keep="true"><FONT face="Courier New">CreateCTP</FONT> returns a <SPAN style="COLOR: #2b91af"><FONT face="Courier New">CustomTaskPane</FONT></SPAN> object which we store for later use. The <SPAN style="COLOR: #2b91af"><FONT face="Courier New">CustomTaskPane</FONT></SPAN> object is how our addin can interact with the CTP window itself. In the example above, we store the <SPAN style="COLOR: #2b91af"><FONT face="Courier New">CustomTaskPane</FONT></SPAN> object inside an&nbsp; <SPAN style="COLOR: #2b91af">AnnotationsExtension</SPAN>&nbsp;object but we can actually store it anywhere we like. The <SPAN style="COLOR: #2b91af">AnnotationsExtension </SPAN>object is a class inside our addin - the details of this class&nbsp;will be&nbsp;covered in the next post - for now, all we need to know is that it is a conceptual wrapper of our CTP. </P>
<P mce_keep="true">So, the basic workflow in creating a Custom Task Pane is as follows:</P>
<P mce_keep="true"><IMG src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/08/86/21/91/CTP_2.png" original-url="http://blogs.msdn.com/photos/gabhan_berry/images/8862191/original.aspx" mce_src="http://blogs.msdn.com/photos/gabhan_berry/images/8862191/original.aspx"></P>
<P mce_keep="true"><STRONG>Displaying a Custom Task Pane</STRONG></P>
<P mce_keep="true">So far we have created a CTP. We have a reference to it and the CTP knows which ActiveX control it is hosting. However, we need to explcitly display the CTP in order for it to be visible. </P>
<P mce_keep="true">We do this via the <SPAN style="COLOR: #2b91af"><FONT face="Courier New">CustomTaskPane</FONT></SPAN>.<FONT face="Courier New">Visible</FONT> property. Setting this property to true or false will display or hide the CTP (notice that the CTP is hidden - not destroyed). </P>
<P mce_keep="true">This makes sense because we'd typically want to show or hide our CTP based on some user action, like a user clicking a button on the Ribbon. In this example we'll add an Annotations button onto the Review tab of the Ribbon. </P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&lt;</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: #a31515; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">tab</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> </SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">idMso</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">=</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">"<SPAN style="COLOR: blue">TabReview</SPAN>"<SPAN style="COLOR: blue">&gt;<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-tab-count: 4">&nbsp; </SPAN>&lt;</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: #a31515; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">group</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> </SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">id</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">=</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">"<SPAN style="COLOR: blue">AnnotationsGroup</SPAN>"<SPAN style="COLOR: blue"> </SPAN><SPAN style="COLOR: red">label</SPAN><SPAN style="COLOR: blue">=</SPAN>"<SPAN style="COLOR: blue">Notes</SPAN>"<SPAN style="COLOR: blue"> </SPAN><SPAN style="COLOR: red">insertBeforeMso</SPAN><SPAN style="COLOR: blue">=</SPAN>"<SPAN style="COLOR: blue">GroupComments</SPAN>"<SPAN style="COLOR: blue">&gt;<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-tab-count: 5">&nbsp;&nbsp;&nbsp; </SPAN>&lt;</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: #a31515; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">toggleButton</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> </SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">id</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">=</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">"<SPAN style="COLOR: blue">AnnotationsButton</SPAN>"<SPAN style="COLOR: blue"> </SPAN><SPAN style="COLOR: red">onAction</SPAN><SPAN style="COLOR: blue">=</SPAN>"<SPAN style="COLOR: blue">OnAnnotationsClicked</SPAN>"<SPAN style="COLOR: blue"> </SPAN><SPAN style="COLOR: red">label</SPAN><SPAN style="COLOR: blue">=</SPAN>"<SPAN style="COLOR: blue">Annotations</SPAN>"<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-tab-count: 7">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">imageMso</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">=</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">"<SPAN style="COLOR: blue">ExchangeFolder</SPAN>"<SPAN style="COLOR: blue"> </SPAN></SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">supertip</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">=</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">"<SPAN style="COLOR: blue">Add new and view existing annotations.</SPAN>" </SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">size</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">=</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">"<SPAN style="COLOR: blue">large</SPAN>"<SPAN style="COLOR: blue">/&gt;<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-tab-count: 4">&nbsp; </SPAN>&lt;/</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: #a31515; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">group</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&lt;/</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: #a31515; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">tab</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: blue; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&gt;</SPAN></P>
<P mce_keep="true">
<P mce_keep="true">We then implement the OnAnnotationsClicked event handler in our COM addin. </P><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="COLOR: blue">public</SPAN><FONT color=#000000> </FONT><SPAN style="COLOR: blue">void</SPAN><FONT color=#000000> OnAnnotationsClicked(</FONT><SPAN style="COLOR: #2b91af">IRibbonControl</SPAN><FONT color=#000000> Control, </FONT><SPAN style="COLOR: blue">bool</SPAN><FONT color=#000000> IsPressed) {<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><FONT color=#000000><SPAN style="mso-spacerun: yes">&nbsp; </SPAN>_annotationsExt.Visible = IsPressed;<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><FONT color=#000000>}</FONT></SPAN></P></SPAN>
<P mce_keep="true">
<P mce_keep="true">Because we added a toggleButton control to the Ribbon, our event handler gets passed a boolean which indicates whether the toggleButton is pressed or not. All we need to do is set the Visible property of the CTP equal to this value.</P>
<P mce_keep="true"><STRONG>Writing the ActiveX Control</STRONG></P>
<P mce_keep="true">We now have enough code to create and display a CTP when a button on the Ribbon is pressed. The final bit (and the harder bit) is writing the ActiveX control ... this is the thing that actually implements our custom features.</P>
<P mce_keep="true">In this example, our ActiveX control reads and write annotations to the CustomXMLParts collection of the active workbook. I'll be covering the CustomXMLParts collection, how it is stored&nbsp;in the new Office XML file formats and how the ActiveX control works next time.</P>
<P mce_keep="true">What's great is that the CTP&nbsp;can host any ActiveX control. So even though the CTP itself is native code, we can&nbsp;write our ActiveX control in .NET code and it all works. </P>
<P mce_keep="true"><STRONG>Summary</STRONG></P>
<P mce_keep="true">To create a Custom Task Pane using managed code:</P>
<UL>
<LI>
<DIV mce_keep="true">Add a reference to Microsoft.Office.Core;</DIV></LI>
<LI>
<DIV mce_keep="true">Implement the <FONT face="Courier New"><SPAN style="COLOR: #2b91af">ICustomTaskPaneConsumer</SPAN> </FONT>interface on your addin class (i.e., the&nbsp;<FONT face="Courier New"><SPAN style="COLOR: #2b91af">Connect</SPAN> </FONT>class);</DIV></LI>
<LI>
<DIV mce_keep="true">In your implementation of <FONT face="Courier New"><SPAN style="COLOR: #2b91af">ICustomTaskPaneConsumer</SPAN></FONT>.<FONT face="Courier New">CTPFactoryAvailable </FONT>call <FONT face="Courier New">CreateCTP </FONT>on the supplied <SPAN style="COLOR: #2b91af"><FONT face="Courier New">ICTPFactory </FONT></SPAN>object;</DIV></LI>
<LI>
<DIV mce_keep="true">In the call to <FONT face="Courier New">CreateCTP</FONT>, specify the&nbsp;ProgId of the ActiveX control you want to host in the CTP;</DIV></LI>
<LI>
<DIV mce_keep="true">Store&nbsp;the&nbsp;<SPAN style="COLOR: #2b91af"><FONT face="Courier New">CustomTaskPane </FONT></SPAN>object returned from <FONT face="Courier New">CreateCTP </FONT>and toggle its <FONT face="Courier New">Visible </FONT>property to show and hide the CTP as and when required;&nbsp;</DIV></LI></UL>
<P mce_keep="true"><STRONG>Links</STRONG></P>
<P mce_keep="true">The following link gives more information on creating Custom Task Panes:</P>
<UL>
<LI>
<DIV mce_keep="true"><A href="http://msdn.microsoft.com/en-us/library/aa942864.aspx">http://msdn.microsoft.com/en-us/library/aa942864.aspx</A></DIV></LI></UL>
<P mce_keep="true"><STRONG>Next Time ...</STRONG></P>
<P mce_keep="true">We'll take a look at how the Annotations ActiveX control uses the CustomXMLParts collection to store the annotations entered by the user. </P>
<P mce_keep="true">&nbsp;</P>
<P mce_keep="true">&nbsp;</P>
<P mce_keep="true">&nbsp;</P>
<P mce_keep="true">&nbsp;</P>
<P mce_keep="true">&nbsp;</P>]]></content:encoded>
    </item>
    <item>
      <title>How to code a .NET RefEdit Control</title>
      <link>https://docs.microsoft.com/archive/blogs/gabhan_berry/how-to-code-a-net-refedit-control</link>
      <pubDate>Wed, 11 Jun 2008 23:02:00 GMT</pubDate>
      <dc:creator><![CDATA[Gabhan Berry]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/gabhan_berry/2008/06/11/how-to-code-a-net-refedit-control/</guid>
      <description><![CDATA[One of the controls frequently used by Excel is the reference edit box. This control is used to...]]></description>
      <content:encoded><![CDATA[<P>One of the controls frequently used by Excel is the reference edit box. This control is used to gather a range address from the user. The user specifies the range either by typing in&nbsp;its address or by using the mouse and clicking on the actual cells they want to use. </P>
<P>Here's a screenshot of&nbsp;the inbuilt reference edit box:</P>
<P mce_keep="true"><IMG src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/08/59/25/64/RefEdit0.png" original-url="http://blogs.msdn.com/photos/gabhan_berry/images/8592564/original.aspx" mce_src="http://blogs.msdn.com/photos/gabhan_berry/images/8592564/original.aspx"></P>
<P mce_keep="true">Whenever the user is required to specify a range, using&nbsp;the reference edit box is my preferred way to gather the address from the user because it enables (what I call) visual selection of ranges.&nbsp;Range addresses can be tedious to work with whereas&nbsp;visually clicking on the cells you want is a much easier and more efficient way of working. &nbsp;</P>
<P mce_keep="true">Custom solutions can also use visual range selection because&nbsp;Excel ships a control called the RefEdit control. This control is implemented&nbsp;in&nbsp;REFEDIT.DLL&nbsp;located in the Office folder (on my laptop this is: C:\PROGRAM FILES\MICROSOFT OFFICE\OFFICE12) and mimics the inbuilt reference edit control.</P>
<P mce_keep="true">Well that's the good news ... now the bad: the REFEDIT.DLL cannot be used from managed code. In fact, it can only be used from VBA. Years ago, that wasn't a problem because most solutions were VBA based. However, these days we live in a .NET world and more and more solutions are being written in managed code.&nbsp;</P>
<P mce_keep="true">So, what are the options if we want to use the RefEdit control from managed code? </P>
<P mce_keep="true">The way I see it, there are three options. Firstly, we could write an XLA containing a VBA userform on which we place the RefEdit control. We would then have to link the managed code and the VBA userform via macros somehow. The downsides of this option are that we&nbsp; create a mixed code solution i.e., we have some VBA and some managed code. This may or may not be an issue for us. </P>
<P mce_keep="true">The second option is to buy a commercial RefEdit control that can be used&nbsp;by .NET WinForms. Actually, I don't know if such a thing exists but let's assume that it does. </P>
<P mce_keep="true">The third option is to write a managed code RefEdit control from scratch. This option is probably the most work and might be quite hard. However, I decided to give it a go... </P>
<P mce_keep="true">In this post, I present a managed code RefEdit control written in C#. I have implemented the control as a .NET UserControl which can be added into&nbsp;the Visual Studio toolbox and dragged and dropped onto any WinForm just like any other .NET control.</P>
<P mce_keep="true">The control I present does not behave <EM>exactly</EM> like Excel's reference edit boxes but I think it behaves close enough that most people wouldn't notice&nbsp;the difference.</P>
<P mce_keep="true">Here's a screenshot of the control in action:</P>
<P mce_keep="true"><IMG src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/08/59/40/13/RefEdit2.png" original-url="http://blogs.msdn.com/photos/gabhan_berry/images/8594013/original.aspx" mce_src="http://blogs.msdn.com/photos/gabhan_berry/images/8594013/original.aspx"></P>
<P mce_keep="true">This example uses the&nbsp;managed code&nbsp;addin I presented in&nbsp;a previous post (<A class="" href="http://blogs.msdn.com/gabhan_berry/archive/2008/03/10/analysing-column-data-using-c-and-drawing-custom-bar-charts.aspx" mce_href="http://blogs.msdn.com/gabhan_berry/archive/2008/03/10/analysing-column-data-using-c-and-drawing-custom-bar-charts.aspx">here</A>) but the control can be used on any WinForm.</P>
<P mce_keep="true">You are free to download the control and use it in your solutions. It is <STRONG>totally free</STRONG> (in all senses of the word) and you can take the code and modify it without restriction. All I ask is that you let me know if you improve it or find any bugs so that I can incorporate those improvements for the benefit of the community.</P>
<P mce_keep="true">Note: The RefEdit control also uses the&nbsp;<FONT face="Courier New"><SPAN style="COLOR: #2b91af">ExcelUtility</SPAN> </FONT>class I published previously in <A class="" href="http://blogs.msdn.com/gabhan_berry/archive/2008/02/06/excel-s-optional-parameters-and-missing-value-a-c-workaround.aspx" mce_href="http://blogs.msdn.com/gabhan_berry/archive/2008/02/06/excel-s-optional-parameters-and-missing-value-a-c-workaround.aspx"><FONT color=#0000cc>this post</FONT></A>.</P>
<P mce_keep="true"><STRONG>How to code a RefEdit Control in C#</STRONG></P>
<P mce_keep="true">Let's first think a bit about the behaviour of the RefEdit control. It does some fancy things in very subtle ways. It's one of those wonderful UI concepts that we use everyday without even thinking about its complexities. </P>
<P mce_keep="true">When we click on the button in the RefEdit control, the window on which the RefEdit control resides is minimised and all controls on the window are not displayed. Clicking on the button again restores the window to its original state.</P>
<P mce_keep="true"><IMG src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/08/59/39/92/RefEdit1.png" original-url="http://blogs.msdn.com/photos/gabhan_berry/images/8593992/original.aspx" mce_src="http://blogs.msdn.com/photos/gabhan_berry/images/8593992/original.aspx">&nbsp;</P>
<P mce_keep="true">The other feature to note is how the visual selection works. When the&nbsp;text box&nbsp;in the RefEdit control has focus,&nbsp;the address of the selected cells in Excel is displayed. Even if multiple, discrete ranges are selected the addresses&nbsp;are displayed. The user can even&nbsp;change&nbsp;sheets and select ranges on different sheets. This all takes place while the window containing the RefEdit control is still visible but is in its minimised state and all the time the addresses of all the selected ranges are displayed in the RefEdit control.</P>
<P mce_keep="true">These two features are the killer features of the RefEdit control. If we can code these features then we are well on our way to our goal of having a managed code RefEdit control.</P>
<P mce_keep="true">So, let's take a look at how we do this.</P>
<P mce_keep="true">First, we need to have a function that can minimise its parent window and hide all controls on the window except the RefEdit control. Also, this function needs to remember the window's previous state and restore the window to that state once the user has selected their range.</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">protected</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: blue">void</SPAN> ResizeParent() {<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN><SPAN style="COLOR: #2b91af">Form</SPAN> parentForm = (<SPAN style="COLOR: #2b91af">Form</SPAN>)Parent;<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN><SPAN style="COLOR: blue">foreach</SPAN> (<SPAN style="COLOR: #2b91af">Control</SPAN> c <SPAN style="COLOR: blue">in</SPAN> parentForm.Controls)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>c.Visible = _state.ParentMinimised;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;</SPAN>Visible = <SPAN style="COLOR: blue">true</SPAN>;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;</SPAN><SPAN style="COLOR: blue">if</SPAN> (!_state.ParentMinimised) {</SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN>_state.ParentClientSize = parentForm.ClientSize;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN>_state.PrevX = Left;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN>_state.PrevY = Top;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN>_state.Anchor = Anchor;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN>Anchor = <SPAN style="COLOR: #2b91af">AnchorStyles</SPAN>.None;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN>parentForm.ClientSize = <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">Size</SPAN>(Width, Height); <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN>Left = 0;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN>Top = 0;<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN>_state.ParentPrevBorder = parentForm.FormBorderStyle;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN>parentForm.FormBorderStyle = <SPAN style="COLOR: #2b91af">FormBorderStyle</SPAN>.FixedDialog;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN>_state.ParentControlBox = parentForm.ControlBox;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN>parentForm.ControlBox = <SPAN style="COLOR: blue">false</SPAN>;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;</SPAN><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;</SPAN><SPAN style="COLOR: blue">else</SPAN> {<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>parentForm.ClientSize = _state.ParentClientSize;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN>Anchor = _state.Anchor;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN>Left = _state.PrevX;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN>Top = _state.PrevY;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN>parentForm.FormBorderStyle = _state.ParentPrevBorder;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN>parentForm.ControlBox = _state.ParentControlBox;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">}</SPAN></P>
<P mce_keep="true">
<P mce_keep="true">Notice that the first line of code casts the control's&nbsp;<FONT face="Courier New">Parent</FONT> to a <SPAN style="COLOR: #2b91af"><FONT face="Courier New">Form</FONT></SPAN> object. This is our first limitation. If this cast fails then our control won't work. Why would&nbsp;the cast fail? It would fail if the parent is not a <SPAN style="COLOR: #2b91af"><FONT face="Courier New">Form</FONT></SPAN>. This means that our RefEdit control's direct parent must be a <SPAN style="COLOR: #2b91af"><FONT face="Courier New">Form</FONT></SPAN>. In other words, we can't place our RefEdit control within another control (such as a group box); it must be placed directly on a <SPAN style="COLOR: #2b91af"><FONT face="Courier New">Form.</FONT></SPAN></P>
<P mce_keep="true">The <FONT face="Courier New">_state</FONT> object is an instance of the following struct: </P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">struct</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: #2b91af">RefEditState</SPAN> {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">public</SPAN> <SPAN style="COLOR: #2b91af">Size</SPAN> ParentClientSize;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">public</SPAN> <SPAN style="COLOR: blue">bool</SPAN> ParentMinimised;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">public</SPAN> <SPAN style="COLOR: blue">int</SPAN> PrevX;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">public</SPAN> <SPAN style="COLOR: blue">int</SPAN> PrevY;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">public</SPAN> <SPAN style="COLOR: #2b91af">FormBorderStyle</SPAN> ParentPrevBorder;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">public</SPAN> <SPAN style="COLOR: blue">bool</SPAN> ParentControlBox;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">public</SPAN> <SPAN style="COLOR: #2b91af">AnchorStyles</SPAN> Anchor;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">public</SPAN> Excel.<SPAN style="COLOR: #2b91af">Worksheet</SPAN> Sheet;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">public</SPAN> <SPAN style="COLOR: blue">bool</SPAN> UseSheetAddress;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>}</SPAN></P>
<P mce_keep="true">We use this struct to store state information&nbsp;while we are minimising the parent window.&nbsp;Any property of the parent window that we change during the minimising we first save in the <FONT face="Courier New">_state</FONT> object. If the window is to be minimised (this happens when the user clicks on the button in the RefEdit control), all controls except the RefEdit control are hidden and the window is resized so that it is fits snuggly around the RefEdit control. That is, the window is made to look like this:</P>
<P mce_keep="true"><IMG src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/08/59/86/96/RefEdit2.png" original-url="http://blogs.msdn.com/photos/gabhan_berry/images/8598696/original.aspx" mce_src="http://blogs.msdn.com/photos/gabhan_berry/images/8598696/original.aspx"></P>
<P mce_keep="true">When the window is to be restored (when the user clicks on the RefEdit button again) we re-apply the settings stored in <FONT face="Courier New">_state</FONT>.</P>
<P mce_keep="true"><STRONG>Updating the RefEdit Control&nbsp;with the Selected Range</STRONG></P>
<P mce_keep="true">The RefEdit control now resizes and restores&nbsp;its parent window whenever the user clicks on the button. Now we need to display the address of the ranges the user selects in Excel, while the user&nbsp;is selecting them. </P>
<P mce_keep="true">So, if the user clicks $A$1 the RefEdit should display $A$1 in its text box. If the user&nbsp;selects the range $A$1:$B$3 the RefEdit should display $A$1:$B$3. Also, we want to support selecting multiple ranges. So, if the user selects $A$1 and $F$3 the RefEdit control should display $A$1,$F$3. &nbsp;</P>
<P mce_keep="true">We detect which ranges the user is selecting by sinking the&nbsp;<SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">SheetSelectionChange</SPAN>&nbsp;event on the <SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">Excel.<SPAN style="COLOR: #2b91af">Application</SPAN></SPAN>&nbsp;object. </P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">private</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: blue">void</SPAN> SinkEvents() {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&nbsp; _con.ExcelApp.SheetSelectionChange += <SPAN style="COLOR: blue">new</SPAN> Excel.<SPAN style="COLOR: #2b91af">AppEvents_SheetSelectionChangeEventHandler</SPAN>(ExcelEvent_SheetSelectionChange);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&nbsp; _con.ExcelApp.SheetActivate += <SPAN style="COLOR: blue">new</SPAN> Excel.<SPAN style="COLOR: #2b91af">AppEvents_SheetActivateEventHandler</SPAN>(ExcelEvent_SheetActivate);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">}</SPAN></P>
<P mce_keep="true">
<P mce_keep="true">However, we don't want to always sink these events. We only want to sink them while the RefEdit is <EM>active</EM>. By <EM>active</EM> I mean the user has either clicked inside the RefEdit's text box or has clicked the RefEdit's button. In other words, we only sink these events while the user is using the RefEdit control. </P>
<P mce_keep="true">We trap the Enter event of the RefEdit's address textbox and sink the Excel events whenever this event fires. We also save the active sheet in the _state object.&nbsp;<SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="COLOR: blue">void</SPAN> _addressTxt_Enter(<SPAN style="COLOR: blue">object</SPAN> sender, <SPAN style="COLOR: #2b91af">EventArgs</SPAN> e) {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN>SinkEvents();<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN><SPAN style="COLOR: blue">if</SPAN> (_state.Sheet == <SPAN style="COLOR: blue">null</SPAN>)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>_state.Sheet = <SPAN style="COLOR: #2b91af">ExcelUtility</SPAN>.ApplicationInvoke&lt;Excel.<SPAN style="COLOR: #2b91af">Worksheet</SPAN>&gt;(_con.ExcelApp, <SPAN style="COLOR: #a31515">"ActiveSheet"</SPAN>);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">}</SPAN></SPAN>&nbsp;</P>
<P mce_keep="true">We detach from the events when the stops using the RefEdit control. </P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">private</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: blue">void</SPAN> UnsinkEvents() {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN>_con.ExcelApp.SheetSelectionChange -= ExcelEvent_SheetSelectionChange;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN>_con.ExcelApp.SheetActivate -= ExcelEvent_SheetActivate;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">}</SPAN></P><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">private</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: blue">void</SPAN> _addressTxt_Leave(<SPAN style="COLOR: blue">object</SPAN> sender, <SPAN style="COLOR: #2b91af">EventArgs</SPAN> e) {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&nbsp; UnsinkEvents();<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">}</SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"></SPAN>Notice that we also sink the <FONT face="Courier New">SheetActivate </FONT>event. We do this because the RefEdit control allows users to select ranges on any sheet; not just the sheet that was active at the time the RefEdit was displayed. Therefore, if the user selects a range&nbsp;on a different sheet we need to include the sheet name in the address of that range. For example, if Sheet1 is active when the RefEdit control is displayed but the user selects&nbsp;$A$1 on Sheet2 then the RefEdit control needs to display 'Sheet2'!$A$1 ... not $A$1. </P>
<P mce_keep="true">One way of solving this problem is to make the RefEdit control&nbsp;always include sheet names in range addresses but this makes for&nbsp;long, messy addresses and&nbsp;Excel's reference edit boxes don't behave like that ... so neither should our RefEdit control. </P>
<P mce_keep="true">Thus, we only want to include sheet names in the addresses if we have to. If we don't have to then we should omit them.</P>
<P mce_keep="true">And that's why we sink the <FONT face="Courier New">SheetActivate </FONT>event. </P>
<P mce_keep="true">Here's the code that runs when the <FONT face="Courier New">SheetActivate </FONT>event and the&nbsp;<FONT face="Courier New">SheetSelectionChange</FONT> event&nbsp;fire: </P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">void</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> ExcelEvent_SheetSelectionChange(<SPAN style="COLOR: blue">object</SPAN> Sh, Excel.<SPAN style="COLOR: #2b91af">Range</SPAN> Target) {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN><SPAN style="COLOR: blue">string</SPAN> address = <SPAN style="COLOR: #a31515">""</SPAN>;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN><SPAN style="COLOR: blue">try</SPAN> {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>Excel.<SPAN style="COLOR: #2b91af">Worksheet</SPAN> sheet = (Excel.<SPAN style="COLOR: #2b91af">Worksheet</SPAN>)Sh;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: blue">if</SPAN> (_state.UseSheetAddress) {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">foreach</SPAN> (<SPAN style="COLOR: #2b91af">Object</SPAN> obj <SPAN style="COLOR: blue">in</SPAN> Target.Areas) {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">if</SPAN> (address.Length &gt; 0)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>address += <SPAN style="COLOR: #a31515">","</SPAN>;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN>Excel.<SPAN style="COLOR: #2b91af">Range</SPAN> rng = (Excel.<SPAN style="COLOR: #2b91af">Range</SPAN>)obj;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN>address += <SPAN style="COLOR: #a31515">"'"</SPAN> + rng.Worksheet.Name + <SPAN style="COLOR: #a31515">"'!"</SPAN>;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN>address += <SPAN style="COLOR: #2b91af">ExcelUtility</SPAN>.RangeInvoke&lt;<SPAN style="COLOR: blue">string</SPAN>&gt;(rng, <SPAN style="COLOR: #a31515">"Address"</SPAN>, <SPAN style="COLOR: blue">true</SPAN>, <SPAN style="COLOR: blue">true</SPAN>, Excel.<SPAN style="COLOR: #2b91af">XlReferenceStyle</SPAN>.xlA1);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: blue">else</SPAN> {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN>address += <SPAN style="COLOR: #2b91af">ExcelUtility</SPAN>.RangeInvoke&lt;<SPAN style="COLOR: blue">string</SPAN>&gt;(Target, <SPAN style="COLOR: #a31515">"Address"</SPAN>, <SPAN style="COLOR: blue">true</SPAN>, <SPAN style="COLOR: blue">true</SPAN>, Excel.<SPAN style="COLOR: #2b91af">XlReferenceStyle</SPAN>.xlA1);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN>_addressTxt.Text = address;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;</SPAN><SPAN style="COLOR: blue">catch</SPAN> (<SPAN style="COLOR: #2b91af">Exception</SPAN>) {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;</SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">}</SPAN>&nbsp;</P><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">void</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> ExcelEvent_SheetActivate(<SPAN style="COLOR: blue">object</SPAN> Sh) {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Excel.<SPAN style="COLOR: #2b91af">Worksheet</SPAN> sheet = (Excel.<SPAN style="COLOR: #2b91af">Worksheet</SPAN>)Sh;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN><SPAN style="COLOR: blue">if</SPAN> (!_state.UseSheetAddress)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>_state.UseSheetAddress = sheet.Name != _state.Sheet.Name;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">}</SPAN></P><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"></SPAN></P></SPAN></SPAN>
<P mce_keep="true">
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"></SPAN></P>As the user selects ranges Excel fires the <FONT face="Courier New">SheetSelectionChange</FONT> event. If the&nbsp;range that was selected is not on the sheet that was active when the RefEdit control was displayed we append the sheet name to the address, otherwise we don't. 
<P mce_keep="true"><STRONG>Using the RefEdit Control on a WinForm</STRONG></P>
<P mce_keep="true">The RefEdit control is a normal .NET UserControl so it appears in the Visual Studio toolbox like every other UserControl. You can drag and drop it onto a WinForm as required. </P>
<P mce_keep="true">However, when we display a window which contains a RefEdit control&nbsp;we have to&nbsp;do so&nbsp;in a certain, non-standard way in order for the RefEdit control to function properly. </P>
<P mce_keep="true">In order for the user to be able to select ranges in Excel while the window containing a RefEdit control is visible, the window must be non-modal. To display a non-modal window we normally invoke&nbsp;its <SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">Show()</SPAN>method. We still do that, but we have to invoke the version of <SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">Show()</SPAN>that takes a native window handle as a parameter. This handle is the handle of the native window which is the parent window of the non-modal window. When we display a non-modal window in Excel we need to make sure that we tell the non-modal window that Excel is its parent window. This ensures that the window behaves properly (for example, when the user alt-tabs to another application).&nbsp;&nbsp;</P>
<P mce_keep="true">The overloaded <SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">Show()</SPAN> method we use actually takes an <SPAN style="COLOR: #2b91af"><FONT face="Courier New">IWin32Window</FONT></SPAN>. <SPAN style="COLOR: #2b91af"><FONT face="Courier New">IWin32Window</FONT></SPAN> interface is defined in the .NET FCL and is used to wrap a native HWND.&nbsp;We have to create a class that implements the <SPAN style="COLOR: #2b91af"><FONT face="Courier New">IWin32Window</FONT></SPAN> interface and supply an instance of that class to the <SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">Show()</SPAN> method of the window containing a RefEdit control.</P>
<P mce_keep="true">The following code defines a class, <SPAN style="COLOR: #2b91af"><FONT face="Courier New">NativeWindowWrapper</FONT></SPAN>, that implements the <SPAN style="COLOR: #2b91af"><FONT face="Courier New">IWin32Window</FONT></SPAN> interface.&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">class</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: #2b91af">NativeWindowWrapper</SPAN> : <SPAN style="COLOR: #2b91af">IWin32Window</SPAN> {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN><SPAN style="COLOR: #2b91af">IntPtr</SPAN> _handle;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN><SPAN style="COLOR: blue">public</SPAN> NativeWindowWrapper(<SPAN style="COLOR: blue">int</SPAN> Hwnd) {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>_handle = <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">IntPtr</SPAN>(Hwnd);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN><SPAN style="COLOR: blue">public</SPAN> <SPAN style="COLOR: #2b91af">IntPtr</SPAN> Handle {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">get</SPAN> { <SPAN style="COLOR: blue">return</SPAN> _handle; }<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">}</SPAN></P>
<P mce_keep="true">We use this class when we display a window&nbsp;containing a RefEdit control. We pass an instance of this class into the Show method&nbsp;of the window containing the RefEdit control.&nbsp;</P>
<P mce_keep="true">In the following example we are using the <SPAN style="COLOR: #2b91af"><FONT face="Courier New">ColumnAnalysisDisplayWindow</FONT></SPAN> discussed in&nbsp;a <A class="" href="http://blogs.msdn.com/gabhan_berry/archive/2008/03/10/analysing-column-data-using-c-and-drawing-custom-bar-charts.aspx" mce_href="http://blogs.msdn.com/gabhan_berry/archive/2008/03/10/analysing-column-data-using-c-and-drawing-custom-bar-charts.aspx">previous blog&nbsp;post</A> ... but the idea is obvious: replace <SPAN style="COLOR: #2b91af"><FONT face="Courier New">ColumnAnalysisDisplayWindow</FONT></SPAN> with your own window.</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="COLOR: blue">protected</SPAN> <SPAN style="COLOR: blue">void</SPAN> DisplayStatsWindow() {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN><SPAN style="COLOR: #2b91af">ColumnAnalysisDisplayWindow</SPAN> window = <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">ColumnAnalysisDisplayWindow</SPAN>();<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p></o:p></SPAN></P><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN>window.Show(<SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">NativeWindowWrapper</SPAN>(_connection.ExcelApp.Hwnd));<o:p></o:p></SPAN> 
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">}</SPAN></P>
<P mce_keep="true"><STRONG>Summary</STRONG></P>
<P mce_keep="true">In this post we've talked&nbsp;about how to code a RefEdit .NET control that mimics the behaviour of Excel's inbuilt reference edit box. I'm not claiming this is absolutely the best implementation we could produce, but I think it's not bad and is fairly easy to do. </P>
<P mce_keep="true">There are (no doubt) bugs in the code and situations where the control doesn't work that well. As I encounter and fix these prolbems I'll post updated versions of the control. If you encounter a problem, feel free to drop me a line and tell me about it.</P>
<P mce_keep="true">The following points summarise this post:</P>
<UL>
<LI>
<DIV mce_keep="true">The RefEdit control mimcs Excel's reference edit controls by enabling&nbsp;the user to visually select ranges;</DIV></LI>
<UL>
<LI>
<DIV mce_keep="true">The control does not behave <EM>exactly</EM> like&nbsp;Excel's reference edit boxes but is fairly close;&nbsp;</DIV></LI></UL>
<LI>
<DIV mce_keep="true">The control is&nbsp;implemented as a&nbsp;.NET UserControl;</DIV></LI>
<LI>
<DIV mce_keep="true">The control requires:</DIV></LI>
<UL>
<LI>
<DIV mce_keep="true">A reference to the Excel 2007 PIA;</DIV></LI>
<LI>
<DIV mce_keep="true">The ExcelUtility class defined in my ExcelExtensions COM addin (see below);</DIV></LI></UL>
<LI>
<DIV mce_keep="true">Windows that contain an instance of the RefEdit control have to be displayed non-modally;</DIV></LI>
<UL>
<LI>
<DIV mce_keep="true">And Excel's HWND must be specified as the native owner of the containing window;</DIV></LI></UL></UL>
<P mce_keep="true"><STRONG>Downloading the source code</STRONG></P>
<P mce_keep="true">I have updated my ExcelExtensions addin to include the RefEdit control. </P>
<P mce_keep="true">All the source code is included in the ExcelExtensions download <A class="" href="http://code.msdn.microsoft.com/ExcelExtensions" mce_href="http://code.msdn.microsoft.com/ExcelExtensions">found here</A>. </P>
<P mce_keep="true">There are two downloads on the <STRONG>Downloads</STRONG> tab. These are: </P>
<UL>
<LI>
<DIV mce_keep="true">ExcelExtensionsSrc.zip</DIV></LI>
<LI>
<DIV mce_keep="true">ExcelExtensions.msi</DIV></LI></UL>
<P mce_keep="true">ExcelExtensionsSrc.zip contains the complete source code and ExcelExtensions.msi is the set up program. </P>
<P mce_keep="true">&nbsp;</P>
<P mce_keep="true">&nbsp;</P>
<P mce_keep="true">&nbsp;</P>
<P mce_keep="true">&nbsp;</P>
<P mce_keep="true">&nbsp;</P>
<P mce_keep="true">&nbsp;</P>
<P mce_keep="true">&nbsp;</P>]]></content:encoded>
    </item>
    <item>
      <title>Upcoming Topics</title>
      <link>https://docs.microsoft.com/archive/blogs/gabhan_berry/upcoming-topics</link>
      <pubDate>Tue, 27 May 2008 20:39:00 GMT</pubDate>
      <dc:creator><![CDATA[Gabhan Berry]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/gabhan_berry/2008/05/27/upcoming-topics/</guid>
      <description><![CDATA[I know that posts have been few and far between (ok ..non-existant) this month. I hope this isn't...]]></description>
      <content:encoded><![CDATA[<P>I know that posts have been few and far between (ok ..non-existant) this month. I hope this isn't something&nbsp;that will be repeated ...&nbsp;this month has just been <STRONG>crazy</STRONG> busy with deadlines&nbsp;(I'm sure you know that feeling...)</P>
<P>Anyway,&nbsp;I have a healthy backlog of&nbsp;topics that I want to&nbsp;blog about over the summer:</P>
<UL>
<LI>Multi-threaded UDFs in Excel 2007;</LI>
<LI>RefEdit control for COM addins&nbsp;written in managed code;</LI>
<LI>C API enhancements in Excel 2007;</LI>
<LI>PivotTable stuff</LI>
<UL>
<LI>I get <STRONG>lots</STRONG> of questions about PivotTable programming so there is plenty I want to talk about here;</LI></UL>
<LI>Ribbon customisation;</LI></UL>
<P>I'm at TechEd next week and am hoping that the&nbsp;flight time there will give me time to get started on some of these posts. </P>
<P>Feel free to drop me a line if&nbsp;there is&nbsp;something you&nbsp;would like&nbsp;covered.&nbsp;</P>]]></content:encoded>
    </item>
    <item>
      <title>Busy Times ... and a Note about my TechEd Sessions</title>
      <link>https://docs.microsoft.com/archive/blogs/gabhan_berry/busy-times-and-a-note-about-my-teched-sessions</link>
      <pubDate>Fri, 25 Apr 2008 19:04:00 GMT</pubDate>
      <dc:creator><![CDATA[Gabhan Berry]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/gabhan_berry/2008/04/25/busy-times-and-a-note-about-my-teched-sessions/</guid>
      <description><![CDATA[Busy times&nbsp;at Microsoft has meant that I've been slower than usual in posting recently. My...]]></description>
      <content:encoded><![CDATA[<P>Busy times&nbsp;at Microsoft has meant that I've been slower than usual in posting recently. My current expectation is to&nbsp;be over my heavy workload by about mid-June. I'll continue to post in the meantime; but at a slower pace.</P>
<P>TechEd is being held in Orlando in June this year. There are a lot of very interesting sessions being held and the key note will be done by Bill Gates. If you can make it, I'd recommend coming along. You can find out more about TechEd <A class="" href="http://www.microsoft.com/events/teched2008/default.mspx" mce_href="http://www.microsoft.com/events/teched2008/default.mspx">here</A>.</P>
<P>I have two speaking slots at this year's show: one in the Developer track and one in the IT Professional track.</P>
<P>In the Developer session&nbsp;I'll be talking about the improvements we made to XLLs in Excel 2007. In the IT Professional session I'll be talking about the Business Intelligence capabilities of Excel 2007 and Excel Services.</P>
<P mce_keep="true">&nbsp;</P>
<P mce_keep="true">&nbsp;</P>]]></content:encoded>
    </item>
    <item>
      <title>Writing Custom Excel Worksheet Functions in C#</title>
      <link>https://docs.microsoft.com/archive/blogs/gabhan_berry/writing-custom-excel-worksheet-functions-in-c</link>
      <pubDate>Mon, 07 Apr 2008 07:00:00 GMT</pubDate>
      <dc:creator><![CDATA[Gabhan Berry]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/gabhan_berry/2008/04/07/writing-custom-excel-worksheet-functions-in-c/</guid>
      <description><![CDATA[Writing our own, custom worksheet functions is a great way to extend Excel. Before Excel 2002 we...]]></description>
      <content:encoded><![CDATA[<P>Writing our own, custom worksheet functions is a great way to extend Excel. Before Excel 2002 we developed custom functions by either writing them&nbsp;using VBA inside an XLA&nbsp;or&nbsp;by using C/C++ inside an XLL. Excel 2002 introduced a new type of addin called an <EM>automation addin</EM>. An automation addin enables&nbsp;Excel to&nbsp;call functions on COM objects&nbsp;from cells on a worksheet. In other words,&nbsp;it enabled us to call COM functions just as if they were&nbsp;normal, built-in Excel functions.</P>
<P>This opened up the world of custom worksheet functions to COM developers. So, now we can now write&nbsp;custom functions in any COM language; including C#.</P>
<P>In this article we'll&nbsp;write a custom worksheet function in C# called UNIQUEVALUES.&nbsp;</P>
<P mce_keep="true"><STRONG>What&nbsp;will our&nbsp;Custom Function Do?</STRONG></P>
<P mce_keep="true">The UNIQUEVALUES function will return an array of the unique values in a specified range. For example, if the range A1:A5 contains the values Red, Yellow, Red, Blue, Yellow, then {=UNIQUEVALUES(A1:A5)} will return the array {Red, Yellow, Blue}. </P>
<P mce_keep="true">The function will also be able to cope with a mixture of numerical data and text data. So, if A1:A5 contains the values: Red, 1, 1, Red then&nbsp;{=UNIQUEVALUES(A1:A5)} will return the array {Red, 1}.</P>
<P mce_keep="true">Because UNIQUEVALUES is a worksheet function that references the source data, whenever the values in the source range change the function automatically extracts the new unique values from the range. So we know that the values returned from the function are always up-to-date (once the worksheet has calculated).</P>
<P mce_keep="true">Notice that the UNIQUEVALUES function returns an array. This means that we <EM>array enter</EM> the funtion by pressing Ctrl-Shift-Enter rather than just Enter when we type the function into the worksheet. </P>
<P mce_keep="true"><STRONG>Coding the UNIQUEVALUES Function</STRONG>&nbsp;</P>
<P mce_keep="true">There are two tasks we need to do in order to make a C# class's methods be callable as worksheet functions: </P>
<UL>
<LI>
<DIV mce_keep="true">Define our worksheet functions&nbsp;in&nbsp;the way Excel expects;</DIV></LI>
<LI>
<DIV mce_keep="true">Add the Programmable key to the registry for our class's CLSID;</DIV></LI></UL>
<P mce_keep="true">The first&nbsp;task seems intriguing: <EM>Define our worksheet functions&nbsp;in&nbsp;the way Excel expects</EM>. What does that mean?</P>
<P mce_keep="true">Well, this is where we have to remember that Excel is speaking to our automation addin via COM. So&nbsp;Excel expects to be able to use COM techniques&nbsp;to discover and access our functions. Once we know what these COM techniques are, we can make sure that our C# class interoperates with COM in the appropriate way. </P>
<P mce_keep="true">When an automation addin is loaded, Excel needs to discover which functions that addin supports. For example, our automation addin&nbsp;will have a UNIQUEVALUES function that we want to use in worksheets. How does Excel discover&nbsp;that our addin&nbsp;supports that function and how does it discover&nbsp;information about the parameters of the function? </P>
<P mce_keep="true">Excel discovers this information using a COM interface called ITypeInfo. </P>
<P mce_keep="true">ITypeInfo&nbsp;is&nbsp;an interface that is&nbsp;used to access metadata about a&nbsp;COM type. It is similar <EM>in concept</EM> to .NET reflection, but is very different in implemenation.</P>
<P mce_keep="true">Excel uses ITypeInfo to discover the names and the parameter details of the functions&nbsp;exposed by our automation addin. This is how Excel knows&nbsp;to call our addin whenever it&nbsp;comes across&nbsp;a&nbsp;call to UNIQUEVALUES.&nbsp;&nbsp;&nbsp;</P>
<P mce_keep="true">But Excel will only query the default interface of our COM class. So we need to make sure that the functions we want to expose as worksheet functions are defined on the default interface of our class. </P>
<P mce_keep="true">There are two ways of doing this in C#:</P>
<UL>
<LI>
<DIV mce_keep="true">By specifying the <SPAN style="FONT-SIZE: 10pt; COLOR: #2b91af; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">ClassInterfaceType</SPAN><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">.AutoDual</SPAN> attribute on our C# class;</DIV></LI>
<LI>
<DIV mce_keep="true">By defining our worksheet functions on a dedicated interface and using the <SPAN style="FONT-SIZE: 10pt; COLOR: #2b91af; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">ComDefaultInterface </SPAN>attribute to make that interface the default interface of our class;</DIV></LI></UL>
<P mce_keep="true">To understand what the <SPAN style="FONT-SIZE: 10pt; COLOR: #2b91af; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">ClassInterfaceType</SPAN><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">.AutoDual </SPAN>attribute value does to our COM interface, we need to understand a little more about COM interfaces.&nbsp;</P>
<P mce_keep="true">In COM,&nbsp;the IDispatch interface enables clients to dynamically call your functions at runtime. Instead of the compiler checking that functions exist and that the parameters types match at compile-time, IDispatch enables clients to do so&nbsp;at runtime. This enables&nbsp;components&nbsp;to be extremely loosely coupled. This type of function call is termed: <EM>late-bound</EM>. </P>
<P mce_keep="true">Support of IDispatch is optional. COM classes do not <EM>have</EM> to support it. However, by default, C# classes support IDispatch. In fact (unless we specify otherwise)&nbsp;the default interface created for a C# class is a dispatch interface.&nbsp;</P>
<P mce_keep="true">Why do we need to care about this? Well, since the default interface is a dispatch interface&nbsp;our worksheet functions are not <EM>explicitly</EM> defined on the interface and are therefore not discoverable by Excel.&nbsp;So, even though our C# class may have a public function named UNIQUEVALUES Excel would not be able to call it.</P>
<P mce_keep="true">A <EM>dual</EM> interface allows a dispatch interface to explicitly define custom functions in addition to those already defined&nbsp;by IDispatch.&nbsp;The <SPAN style="FONT-SIZE: 10pt; COLOR: #2b91af; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">ClassInterfaceType</SPAN><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">.AutoDual </SPAN>attribute turns our COM interface into a dual interface. This effectively takes all public functions defined on our class&nbsp;(including those inherited from base classes) and explicitly defines them on our COM interface which in turn&nbsp;makes them&nbsp;discoverable&nbsp;by Excel.</P>
<P mce_keep="true">Thus, an easy way of getting our C# class to define Excel worksheet functions is to simply mark the class with&nbsp;<SPAN style="FONT-SIZE: 10pt; COLOR: #2b91af; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">ClassInterfaceType</SPAN><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">.AutoDual. </SPAN></P>
<P mce_keep="true">However, there is a downside to doing this. All public functions on our class are then available as worksheet functions, including those inherited from System.Object. This isn't ideal. It would be better if we could have more control over what gets exposed to Excel and make sure that only the functions that are intended to be worksheet functions are actually discoverable by Excel. This is the reason I prefer not to use the <SPAN style="FONT-SIZE: 10pt; COLOR: #2b91af; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">ClassInterfaceType</SPAN><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">.AutoDual </SPAN>attribute.</P>
<P mce_keep="true">(There is also a problem with COM versioning when using <SPAN style="FONT-SIZE: 10pt; COLOR: #2b91af; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">ClassInterfaceType</SPAN><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">.AutoDual </SPAN>- but we won't cover that here).</P>
<P mce_keep="true">Instead, I prefer to define the worksheet functions on a dedicated interface and have my C# class implement that interface. </P>
<P mce_keep="true">So, let's define our worksheet function interface. We'll call it <FONT face="Courier New"><SPAN style="COLOR: #2b91af">IFunctions</SPAN></FONT>. </P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">public</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: blue">interface</SPAN> <SPAN style="COLOR: #2b91af">IFunctions</SPAN> {<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="COLOR: blue"><FONT color=#000000>&nbsp; </FONT>object</SPAN>[,] UNIQUEVALUES(Excel.<SPAN style="COLOR: #2b91af">Range</SPAN> TargetRange);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">}</SPAN></P>
<P mce_keep="true">Notice that the UNIQUEVALUES function returns a 2-d array of objects. This array will contain the unique values extracted from the specified <FONT face="Courier New">Excel.<SPAN style="COLOR: #2b91af">Range</SPAN></FONT> object. </P>
<P mce_keep="true">Next, we define our functions class and have it implement the <FONT face="Courier New"><SPAN style="COLOR: #2b91af">IFunctions </SPAN></FONT>interface. We also use the default attribute to specify that we want the IFunctions interface to be the class's default COM interface. </P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">[<SPAN style="COLOR: #2b91af">ComDefaultInterface</SPAN>(<SPAN style="COLOR: blue">typeof</SPAN>(<SPAN style="COLOR: #2b91af">IFunctions</SPAN>))]<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="COLOR: blue">public</SPAN> <SPAN style="COLOR: blue">class</SPAN> <SPAN style="COLOR: #2b91af">Functions</SPAN> : <SPAN style="COLOR: #2b91af">IFunctions</SPAN> {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="COLOR: blue">&nbsp; public</SPAN> <SPAN style="COLOR: blue">object</SPAN>[,] UNIQUEVALUES(Excel.<SPAN style="COLOR: #2b91af">Range</SPAN> TargetRange) {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="COLOR: blue">&nbsp;&nbsp;&nbsp; object</SPAN>[,] values = TargetRange.get_Value(System.Reflection.<SPAN style="COLOR: #2b91af">Missing</SPAN>.Value) <SPAN style="COLOR: blue">as</SPAN> <SPAN style="COLOR: blue">object</SPAN>[,];<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #2b91af">List</SPAN>&lt;<SPAN style="COLOR: blue">object</SPAN>&gt; unqVals = <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">List</SPAN>&lt;<SPAN style="COLOR: blue">object</SPAN>&gt;();<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: blue">foreach</SPAN> (<SPAN style="COLOR: blue">object</SPAN> obj <SPAN style="COLOR: blue">in</SPAN> values) {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">if</SPAN> (!unqVals.Contains(obj))<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>unqVals.Add(obj);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: blue">object</SPAN>[,] resVals = <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: blue">object</SPAN>[unqVals.Count, 1];<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: blue">for</SPAN> (<SPAN style="COLOR: blue">int</SPAN> idx = 0; idx &lt; resVals.Length; ++idx)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>resVals[idx, 0] = unqVals[idx];<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: blue">return</SPAN> resVals;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">}</SPAN></P>
<P mce_keep="true">
<P mce_keep="true">The algorithm for extracting the unique values is as follows (I confess that this may not be the optimal algorithm). </P>
<P mce_keep="true">We extract the values in the <FONT face="Courier New"><SPAN style="COLOR: #2b91af">Range</SPAN> </FONT>object into a 2-d array of <FONT face="Courier New" color=#0000ff>object</FONT>. Because the array contains <FONT face="Courier New" color=#0000ff>object</FONT>, we can cope with both textual data and numerical data. Next, we iterate over the array and store each value that hasn't already occurred in a <FONT face="Courier New"><SPAN style="COLOR: #2b91af">List</SPAN>&lt;<SPAN style="COLOR: blue">object</SPAN>&gt;</FONT>. We then allocate a new 2-d array of <FONT face="Courier New" color=#0000ff>object </FONT>and copy the unique values from the <FONT face="Courier New"><SPAN style="COLOR: #2b91af">List</SPAN>&lt;<SPAN style="COLOR: blue">object</SPAN>&gt; </FONT>into the new array. Finally, we return the new array to Excel.</P>
<P mce_keep="true">
<P mce_keep="true">When we return an array from a UDF Excel handles serialising the array into the worksheet in the appropriate way (as long as the user array-entered the cell formula). This means that the dimensions of the array matter. The first dimension of the array is mapped to columns. So if we return an <FONT face="Courier New"><SPAN style="COLOR: blue">object</SPAN>[3]</FONT> then Excel interprets this as <EM>"three columns containing one row each"</EM> and places the three values adjacent to each other (i.e., each one in a different column). If we return an <FONT face="Courier New"><SPAN style="COLOR: blue">object</SPAN>[1,3]</FONT>then Excel interprets this as <EM>"one column containing three rows"</EM> and places each value <EM>beneath</EM> each other (i.e., each one in a different row). </P>
<P mce_keep="true">So, it is very easy to return an array to Excel. All we have to do is set the correct array dimensions and let Excel handle copying the values from the to the appropriate cells in the worksheet. </P>
<P mce_keep="true">We'll talk more about how arrays&nbsp;are used in Excel worksheet functions later in the article.</P>
<P mce_keep="true">If we stop coding here, we'll find that Excel still can't use our class. There is one final thing we need to do: <EM>set the class's Programmable key in the registry</EM>.</P>
<P mce_keep="true">The Programmable key indicates that a COM class exposes type information for its default interface.&nbsp;The key is specified&nbsp;under&nbsp;the COM class's HKEY_CLASSES_ROOT\CLSID\{Guid} key (where {Guid} is the guid of the COM class).</P>
<P mce_keep="true">There is a convenient way to set this registry key from C#. There&nbsp;exist two attributes called <SPAN style="FONT-SIZE: 10pt; COLOR: #2b91af; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">ComRegisterFunctionAttribute </SPAN>and <SPAN style="FONT-SIZE: 10pt; COLOR: #2b91af; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">ComUnregisterFunctionAttribute</SPAN>. These attributes are used to mark which functions COM should call&nbsp;during the COM registration/unregistration process. So all we need to do is write a function that inserts the Programmable key during registration and removes it during unregistration.</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">[<SPAN style="COLOR: #2b91af">ComRegisterFunctionAttribute</SPAN>]<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="COLOR: blue">public</SPAN> <SPAN style="COLOR: blue">static</SPAN> <SPAN style="COLOR: blue">void</SPAN> RegisterFunction(<SPAN style="COLOR: #2b91af">Type</SPAN> type) {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="COLOR: #2b91af">&nbsp; Registry</SPAN>.ClassesRoot.CreateSubKey(GetSubKeyName(type));<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">[<SPAN style="COLOR: #2b91af">ComUnregisterFunctionAttribute</SPAN>]<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="COLOR: blue">public</SPAN> <SPAN style="COLOR: blue">static</SPAN> <SPAN style="COLOR: blue">void</SPAN> UnregisterFunction(<SPAN style="COLOR: #2b91af">Type</SPAN> type) {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="COLOR: #2b91af"><FONT color=#000000>&nbsp; </FONT>Registry</SPAN>.ClassesRoot.DeleteSubKey(GetSubKeyName(type), <SPAN style="COLOR: blue">false</SPAN>);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="COLOR: blue">private</SPAN> <SPAN style="COLOR: blue">static</SPAN> <SPAN style="COLOR: blue">string</SPAN> GetSubKeyName(<SPAN style="COLOR: #2b91af">Type</SPAN> type) {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN><SPAN style="COLOR: blue">string</SPAN> s = <SPAN style="COLOR: #a31515">@"CLSID\{"</SPAN> + type.GUID.ToString().ToUpper() + <SPAN style="COLOR: #a31515">@"}\Programmable"</SPAN>;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN><SPAN style="COLOR: blue">return</SPAN> s;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">}</SPAN></P>
<P mce_keep="true">(Notice that the registration functions are <FONT face="Courier New"><SPAN style="COLOR: blue">static</SPAN></FONT>).</P>
<P mce_keep="true">
<P mce_keep="true">Now we've finished coding the <SPAN style="COLOR: #2b91af"><FONT face="Courier New">Functions</FONT></SPAN> class and the UNIQUEVALUES function. We can now use from an Excel worksheet just like any other custom worksheet function.</P>
<P mce_keep="true"><STRONG>Using UNIQUEVALUES in a Worksheet</STRONG></P>
<P mce_keep="true">The first to do is to install our <SPAN style="COLOR: #2b91af"><FONT face="Courier New">Functions</FONT></SPAN> class as an automation addin. This enables Excel to call our custom UDFs. </P>
<P mce_keep="true">To install as an automation addin, we bring up the usual&nbsp;Addin Manager via&nbsp;Excel Options-&gt;Add-Ins-&gt;Manage Excel Add-ins. From this dialogue we click the Automation button and select ExcelExtensions.Functions from the list.</P>
<P mce_keep="true"><IMG src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/08/36/20/70/CSharpWorksheetFuncs_img0.png" original-url="http://blogs.msdn.com/photos/gabhan_berry/images/8362070/original.aspx" mce_src="http://blogs.msdn.com/photos/gabhan_berry/images/8362070/original.aspx"></P>
<P mce_keep="true">To use the function, we highlight the cells which we want the unique values written into, type the =UNIQUEVALUES( ... ) where ... is the range from which we want to extract the values and then press <EM>ctrl-shift-enter</EM>. </P>
<P mce_keep="true">Note that we must press ctrl-shift-enter to tell Excel that this function returns an array. Excel automatically serialises the values returned from the function into the array of highlighted cells. If the number of values returned from the function is less than the number of cells we highlighted, Excel places #N/A in each of the extra cells. </P>
<P mce_keep="true">Consider the following example.</P>
<P mce_keep="true"><IMG src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/08/36/58/76/CSharpWorksheetFuncs_img1.png" original-url="http://blogs.msdn.com/photos/gabhan_berry/images/8365876/original.aspx" mce_src="http://blogs.msdn.com/photos/gabhan_berry/images/8365876/original.aspx"></P>
<P mce_keep="true">Notice that there are only four unique values in the range D3:D9. However, because we highlighted 7 cells (i.e., E3:E9) Excel has placed #N/A in each of the 3 additional cells.</P>
<P mce_keep="true">If we highlight too few cells (say, 2 cells) then Excel will place the first to values returned from the function in those cells.</P>
<P mce_keep="true">This is normal array function behaviour in Excel. Notice that we didn't have to write anything in our code to do this; Excel does this for us automatically. </P>
<P mce_keep="true">If the data in D3:D9 changes, Excel fires UNIQUEVALUES and our function recalculates the new unique values. </P>
<P mce_keep="true">In the following screenshot, we change the first value from Red to Yellow and the unique values automatically update:</P>
<P mce_keep="true"><IMG src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/08/36/59/18/CSharpWorksheetFuncs_img2.png" original-url="http://blogs.msdn.com/photos/gabhan_berry/images/8365918/original.aspx" mce_src="http://blogs.msdn.com/photos/gabhan_berry/images/8365918/original.aspx"></P>
<P mce_keep="true">The UNIQUEVALUES function can also be used with Excel's native functions that work with arrays. Two examples of such&nbsp;functions are COUNT and INDEX. </P>
<P mce_keep="true">Both COUNT and INDEX can&nbsp;accept arrays as parameters. So we can use these functions&nbsp;on the array returned by UNIQUEVALUES.</P>
<P mce_keep="true">The formula =COUNT(UNIQUEVALUES(D3:D9)) returns the number of values in the array. The formula =INDEX(UNIQUEVALUES(D3:D9), 1, 1) returns the first value in the array; and =INDEX(UNIQUEVALUES(D3:D9), <STRONG>2</STRONG>, 1) returns the second; and so on ... (remember that UNIQUEVALUES returns an array one column wide).</P>
<P mce_keep="true"><STRONG>Summary</STRONG></P>
<UL>
<LI>
<DIV mce_keep="true">The functions that we want to expose as custom worksheet functions need to be defined on our class's default interface;</DIV></LI>
<UL>
<LI>
<DIV mce_keep="true">Defining our custom worksheet functions on a dedicated interface and using <SPAN style="COLOR: #2b91af"><FONT face="Courier New">ComDefaultInterface </FONT></SPAN>to make that interface the default is one way of doing this;</DIV></LI></UL>
<LI>
<DIV mce_keep="true">Our COM class must define the Programmable registry key under its CLSID key;</DIV></LI>
<LI>
<DIV mce_keep="true">We can return multiple values from a custom UDF by returning them in an array;</DIV></LI>
<LI>
<DIV mce_keep="true">Excel handles the serialisation of the array into the worksheet;</DIV></LI></UL>
<P mce_keep="true"><STRONG>Download</STRONG></P>
<P mce_keep="true">I have added the UNIQUEVALUES function into my ExcelExtensions add-in. This can be downloaded from the MSDN Code Gallery by clicking <A class="" href="http://code.msdn.microsoft.com/ExcelExtensions" mce_href="http://code.msdn.microsoft.com/ExcelExtensions">here</A>.</P>
<P mce_keep="true">There are two downloads which can be downloaded from the Releases tab: ExcelExtensionsSrc.zip and ExcelExtensions.msi. </P>
<P mce_keep="true">ExcelExtensionsSrc.zip contains the complete source code and ExcelExtensions.msi is the set up program. </P>
<P mce_keep="true">Over time, I'll be adding additional feature to the addin.</P>
<P mce_keep="true">&nbsp;</P>
<P mce_keep="true">&nbsp;</P>
<P mce_keep="true">&nbsp;</P>
<P mce_keep="true">&nbsp;</P>
<P mce_keep="true">&nbsp;</P>
<P mce_keep="true">&nbsp;</P>]]></content:encoded>
    </item>
    <item>
      <title>Using Statistics in Business Intelligence</title>
      <link>https://docs.microsoft.com/archive/blogs/gabhan_berry/using-statistics-in-business-intelligence</link>
      <pubDate>Fri, 04 Apr 2008 12:27:00 GMT</pubDate>
      <dc:creator><![CDATA[Gabhan Berry]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/gabhan_berry/2008/04/04/using-statistics-in-business-intelligence/</guid>
      <description><![CDATA[One other area I work in is Business Intelligence. These days BI is an important technology to an...]]></description>
      <content:encoded><![CDATA[<P>One other area I work in is Business Intelligence. These days BI is an important technology to an increasing number of companies. There are many great BI tools out there, one of which is (of course)&nbsp;Excel. </P>
<P>One of the things that&nbsp;makes Excel such a useful BI tool is its richness of functionality. It has so many existing features that can be used in the BI area - one of which is its statistical analysis functions.</P>
<P>I posted an article over on the Excel team blog illustrating one way&nbsp;we can use Excel's&nbsp;statistical functions to extract useful information from business data. </P>
<P>If you're interested in statistical analysis or BI in Excel, you may find the article interesting. Click <A class="" href="http://blogs.msdn.com/excel/archive/2008/04/02/statistics-and-business-data-detecting-unexpected-values.aspx" mce_href="http://blogs.msdn.com/excel/archive/2008/04/02/statistics-and-business-data-detecting-unexpected-values.aspx">here</A> to view the article.</P>]]></content:encoded>
    </item>
    <item>
      <title>OOXML wins ISO Vote</title>
      <link>https://docs.microsoft.com/archive/blogs/gabhan_berry/ooxml-wins-iso-vote</link>
      <pubDate>Tue, 01 Apr 2008 12:49:00 GMT</pubDate>
      <dc:creator><![CDATA[Gabhan Berry]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/gabhan_berry/2008/04/01/ooxml-wins-iso-vote/</guid>
      <description><![CDATA[Office Open XML (OOXML), the XML file formats used by Office 2007, today won the ISO vote which...]]></description>
      <content:encoded><![CDATA[<P>Office Open XML (OOXML), the XML file formats used by Office 2007, today won the ISO vote which paves the way for it becoming an ISO standard.</P>
<P>This is <EM>very</EM> exciting news indeed for Excel programmability.&nbsp;Coupled with the new Open XML SDK that&nbsp;we announced last week,&nbsp;programming against&nbsp;Excel's native file format has never been as&nbsp;open.&nbsp;I think this opens up quite a lot of exciting programmability scenarios, some of which I'll cover on this blog over time.</P>
<P>More information can be found at the following links:</P>
<UL>
<LI><A class="" href="http://www.microsoft.com/presspass/press/2008/apr08/04-01OpenXMLVotePR.mspx" mce_href="http://www.microsoft.com/presspass/press/2008/apr08/04-01OpenXMLVotePR.mspx ">Microsoft's press release</A>;</LI>
<LI><A class="" href="http://www.iso.org/iso/pressrelease.htm?refid=Ref1123" mce_href="http://www.iso.org/iso/pressrelease.htm?refid=Ref1123">ISO's press release</A>;</LI>
<LI><A class="" href="http://www.ecma-international.org/news/TC45_current_work/ISO_and_IEC_approve_Office_Open_XML.htm" mce_href="http://www.ecma-international.org/news/TC45_current_work/ISO_and_IEC_approve_Office_Open_XML.htm ">ECMA's statement</A>;</LI></UL>
<P mce_keep="true">&nbsp;</P>]]></content:encoded>
    </item>
    <item>
      <title>Open XML SDK Roadmap Announced</title>
      <link>https://docs.microsoft.com/archive/blogs/gabhan_berry/open-xml-sdk-roadmap-announced</link>
      <pubDate>Thu, 20 Mar 2008 00:06:00 GMT</pubDate>
      <dc:creator><![CDATA[Gabhan Berry]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/gabhan_berry/2008/03/20/open-xml-sdk-roadmap-announced/</guid>
      <description><![CDATA[The new XML file formats in Office 2007 open the door to lots of exciting&nbsp;programmability...]]></description>
      <content:encoded><![CDATA[<P>The new XML file formats in Office 2007 open the door to lots of exciting&nbsp;programmability solutions. For example, the files can be created and consumed&nbsp;without automating Excel.exe. This&nbsp;really helps server-side solutions. It also&nbsp;aids&nbsp;batch processing of Excel files and cross-platform business solutions that exchange Office files.</P>
<P>To enable easy access to the files, we provided the .NET Packaging API.&nbsp;&nbsp;But this is a low-level API which does not have any application awareness. For example, it doesn't understand Excel ranges or tables; it deals with the raw XML elements and relations.</P>
<P>But we are working on improving this situation. </P>
<P>The Open XML SDK is a .NET library which will provide a higher-level API for the Open XML files. It will have application awareness and will greatly benefit developer productivity. </P>
<P>Version 1.0 will be released in May 2008. This will be&nbsp;an augmentation of&nbsp;last year's CTP. However,&nbsp;there won't really be anything in v1.0 for Excel developers. The Excel support will come later this year.</P>
<P><IMG src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/08/32/69/37/timeline.png" original-url="http://blogs.msdn.com/photos/gabhan_berry/images/8326937/original.aspx" mce_src="http://blogs.msdn.com/photos/gabhan_berry/images/8326937/original.aspx"></P>
<P>Here are some links which contain more information on the SDK:</P>
<UL>
<LI><A class="" href="http://openxmldeveloper.org/archive/2008/03/13/OpenXMLSDK.aspx" mce_href="http://openxmldeveloper.org/archive/2008/03/13/OpenXMLSDK.aspx">OpenXMLDeveloper.org</A></LI>
<LI><A class="" href="http://msdn2.microsoft.com/en-us/library/bb448854.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/bb448854.aspx">Download the Open XML SDK</A></LI></UL>
<P mce_keep="true">&nbsp;</P>
<P mce_keep="true">&nbsp;</P>
<P mce_keep="true">&nbsp;</P>
<P mce_keep="true">&nbsp;</P>]]></content:encoded>
    </item>
    <item>
      <title>Analysing Column Data using C# (... with a little Custom Drawing thrown in for fun)</title>
      <link>https://docs.microsoft.com/archive/blogs/gabhan_berry/analysing-column-data-using-c-with-a-little-custom-drawing-thrown-in-for-fun</link>
      <pubDate>Mon, 10 Mar 2008 18:35:00 GMT</pubDate>
      <dc:creator><![CDATA[Gabhan Berry]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/gabhan_berry/2008/03/10/analysing-column-data-using-c-with-a-little-custom-drawing-thrown-in-for-fun/</guid>
      <description><![CDATA[When presented with&nbsp;a table of data in Excel, sometimes it is useful to be able to learn some...]]></description>
      <content:encoded><![CDATA[<P>When presented with&nbsp;a table of data in Excel, sometimes it is useful to be able to learn some quick facts about it.&nbsp;One quick fact which is often useful is the distribution of&nbsp;values within a particular column. For example, if we have a column called&nbsp;Region it is useful to&nbsp; know which regions occur more often than others. Perhaps 50% of the column is&nbsp;North America, 30% Europe and 19% Pacific.&nbsp;Excel has all the tools we need to do this but&nbsp;we just need to tie them together in a way that suits us.</P>
<P>Additionally, it would be nice if we displayed this information graphically; perhaps using some form of simple histogram to compliment the numerical values and provide a tiny bit of visualisation.</P>
<P>In this post,&nbsp;we'll&nbsp;write a C# addin that does&nbsp;such a thing.</P>
<P>Here's a screenshot of the&nbsp;addin we'll build. </P>
<P><IMG src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/08/13/92/79/ColumnAnalysis_img1.png" original-url="http://blogs.msdn.com/photos/gabhan_berry/images/8139279/original.aspx" mce_src="http://blogs.msdn.com/photos/gabhan_berry/images/8139279/original.aspx"></P>
<P mce_keep="true"><STRONG>Getting Started</STRONG></P>
<P mce_keep="true">We won't cover the basics of getting up-and-running with C# Excel addins in this post. I have already covered that briefly <A class="" href="http://blogs.msdn.com/gabhan_berry/archive/2008/01/30/excel-and-managed-code-how-does-that-work.aspx" mce_href="http://blogs.msdn.com/gabhan_berry/archive/2008/01/30/excel-and-managed-code-how-does-that-work.aspx">here</A>. Instead, we'll fast forward to the stage where we've already got our basic addin project and now we're ready to add the fun bits. </P>
<P mce_keep="true">Note: This addin uses the&nbsp;<FONT face="Courier New"><SPAN style="COLOR: #2b91af">ExcelUtility</SPAN> </FONT>class I published previously in <A class="" href="http://blogs.msdn.com/gabhan_berry/archive/2008/02/06/excel-s-optional-parameters-and-missing-value-a-c-workaround.aspx" mce_href="http://blogs.msdn.com/gabhan_berry/archive/2008/02/06/excel-s-optional-parameters-and-missing-value-a-c-workaround.aspx">this post</A>.</P>
<P mce_keep="true"><STRONG>Designing the Custom Ribbon</STRONG></P>
<P mce_keep="true">We'll add a custom tab to the Ribbon called "Excel Extensions". On this tab, we'll place a button titled "Analyse Column" inside a group titled "Table Extensions". </P>
<P mce_keep="true">Here's our Ribbon XML:</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&lt;?</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: #a31515; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">xml</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> </SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">version</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">=</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">"<SPAN style="COLOR: blue">1.0</SPAN>"<SPAN style="COLOR: blue"> </SPAN><SPAN style="COLOR: red">encoding</SPAN><SPAN style="COLOR: blue">=</SPAN>"<SPAN style="COLOR: blue">utf-8</SPAN>"<SPAN style="COLOR: blue"> ?&gt;<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&lt;</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: #a31515; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">customUI</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> </SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">onLoad</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">=</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">"<SPAN style="COLOR: blue">OnRibbonLoad</SPAN>"<SPAN style="COLOR: blue"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN></SPAN><SPAN style="COLOR: red">xmlns</SPAN><SPAN style="COLOR: blue">=</SPAN>"<SPAN style="COLOR: blue">http://schemas.microsoft.com/office/2006/01/customui</SPAN>"<SPAN style="COLOR: blue">&gt;<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&nbsp; &lt;</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: #a31515; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">ribbon</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&nbsp;&nbsp;&nbsp; &lt;</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: #a31515; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">tabs</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: #a31515; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">tab</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> </SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">id</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">=</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">"<SPAN style="COLOR: blue">ExcelExtensionsTab</SPAN>"<SPAN style="COLOR: blue"> </SPAN><SPAN style="COLOR: red">label</SPAN><SPAN style="COLOR: blue">=</SPAN>"<SPAN style="COLOR: blue">Excel Extensions</SPAN>"<SPAN style="COLOR: blue">&gt;<SPAN style="mso-tab-count: 3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: #a31515; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">group</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> </SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">id</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">=</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">"<SPAN style="COLOR: blue">TableCommandsGroup</SPAN>"<SPAN style="COLOR: blue"> </SPAN><SPAN style="COLOR: red">label</SPAN><SPAN style="COLOR: blue">=</SPAN>"<SPAN style="COLOR: blue">Table Extensions</SPAN>"<SPAN style="COLOR: blue">&gt;<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: #a31515; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">button</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> </SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">id</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">=</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">"<SPAN style="COLOR: blue">ColumnAnalysis</SPAN>"<SPAN style="COLOR: blue"> </SPAN><SPAN style="COLOR: red">onAction</SPAN><SPAN style="COLOR: blue">=</SPAN>"<SPAN style="COLOR: blue">OnColumnAnalysisClicked</SPAN>" </SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">label</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">=</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">"<SPAN style="COLOR: blue">Analyse Column</SPAN>"<SPAN style="COLOR: blue"> </SPAN><SPAN style="COLOR: red">getImage</SPAN><SPAN style="COLOR: blue">=</SPAN>"<SPAN style="COLOR: blue">GetColumnAnalysisButtonImage</SPAN>" </SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">getEnabled</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">=</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">"<SPAN style="COLOR: blue">GetColumnAnalysisEnabled</SPAN>"<SPAN style="COLOR: blue"> <o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&nbsp;supertip</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">=</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">"<SPAN style="COLOR: blue">Analyse a table column and view statistical details about the values in the column.</SPAN>"<SPAN style="COLOR: blue"> <o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&nbsp;size</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">=</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">"<SPAN style="COLOR: blue">large</SPAN>"<SPAN style="COLOR: blue">/&gt;<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: #a31515; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">group</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: #a31515; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">tab</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&nbsp;&nbsp;&nbsp; &lt;/</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: #a31515; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">tabs</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&nbsp; &lt;/</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: #a31515; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">ribbon</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&lt;/</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: #a31515; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">customUI</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: blue; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&gt;</SPAN></P>
<P mce_keep="true">
<P>We'll store this&nbsp;in an XML file as part of our solution. Then we'll link the XML file into our assembly at compile time as an&nbsp;embedded resource i.e., the XML is compiled into our assembly. This means that we don't have to distribute the XML file with our addin and&nbsp;we use the .NET <SPAN style="COLOR: #2b91af"><FONT face="Courier New">Assembly </FONT></SPAN>class to&nbsp;access the&nbsp;XML resource at runtime.&nbsp;</P>
<P>Here's our implemention of the IRibbonExtensibility.GetCustomUI method:</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">public</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: blue">string</SPAN> GetCustomUI(<SPAN style="COLOR: blue">string</SPAN> RibbonID) {<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="COLOR: #2b91af">&nbsp; Assembly</SPAN> assembly = <SPAN style="COLOR: #2b91af">Assembly</SPAN>.GetExecutingAssembly();<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="COLOR: blue">&nbsp; using</SPAN> (System.IO.<SPAN style="COLOR: #2b91af">Stream</SPAN> stream = assembly.GetManifestResourceStream&nbsp;(<SPAN style="COLOR: #a31515">"ExcelExtensions.ExcelExtensionsRibbon.xml"</SPAN>)) {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">return</SPAN> <SPAN style="COLOR: blue">new</SPAN> System.IO.<SPAN style="COLOR: #2b91af">StreamReader</SPAN>(stream).ReadToEnd();<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">}</SPAN></P>
<P>It would be nice if&nbsp;our&nbsp;Analyse Column button was enabled only when&nbsp;the user has clicked inside a table. To&nbsp;do this, we need to interact with the Ribbon API a little more.&nbsp;&nbsp;</P>
<P>In our XML, we specify a <SPAN style="FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">getEnabled</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">=</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">"<SPAN style="COLOR: blue">GetColumnAnalysisEnabled</SPAN>"</SPAN>&nbsp;attribute on the button. This tells&nbsp;the Ribbon that whenever it needs to draw our button it should first call the <FONT face="Courier New">GetColumnAnalysisEnabled </FONT>method. This method will return a&nbsp;true/false value indicating whether the button&nbsp;should be&nbsp;displayed as enabled or not. </P>
<P>The Ribbon will call <FONT face="Courier New">GetColumnAnalysisEnabled </FONT>when it first&nbsp;displays&nbsp;our custom tab. But it won't call it&nbsp;again. So, how do we toggle the state of our button dynamically at runtime? </P>
<P>When our addin is first loaded, we are given a reference to the main Ribbon instance.&nbsp;This is an object of type <SPAN style="FONT-SIZE: 10pt; COLOR: #2b91af; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">IRibbonUI</SPAN>. One of the methods&nbsp;on this interface&nbsp;is <SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">Invalidate</SPAN>.&nbsp;When we call this method, the Ribbon&nbsp;invokes all of our <SPAN style="FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">getEnabled </SPAN>functions. It is&nbsp;our responsibility&nbsp;to make sure that these functions return the correct true/false value.</P>
<P>This seems a little weird to begin with. It is a side affect of the declarative nature of the Ribbon API; that is, we&nbsp;are not given pointers or references to the Ribbon controls at runtime so we need some callback approach to enable us to&nbsp;change the controls' appearances&nbsp;at runtime.&nbsp;</P>
<P>The Ribbon doesn't know anything about our application logic so it doesn't know when we want to enable/disable buttons or dynamically change the Ribbon. To avoid constantly calling into all addins,&nbsp;the Ribbon&nbsp;declares methods by which all addins can call into it and force&nbsp;it to redraw part or all of&nbsp;their custom Ribbon tabs. </P>
<P>Because we want to enable/disable our button based on the cell that the user selects, we want the Ribbon to redraw our button every time the sheet selection changes. When the user selects a cell, we want to force the Ribbon to invoke our <FONT face="Courier New">GetColumnAnalysisEnabled </FONT><FONT color=#000000>method. In this method we will determine whether or not the user clicked inside a table&nbsp;and return true/false appropriately. </FONT></P>
<P>First, let's sink the <FONT face="Courier New">SheetSelectionChange </FONT>event and invalidate the Ribbon whenever <FONT face="Courier New">SheetSelectionChange </FONT>fires.</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">protected</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: blue">void</SPAN> SinkExcelEvents() {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&nbsp; _application.SheetSelectionChange += <SPAN style="COLOR: blue">new</SPAN> Excel.<SPAN style="COLOR: #2b91af">AppEvents_SheetSelectionChangeEventHandler</SPAN>(_application_SheetSelectionChange);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;</SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes"></SPAN></SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="COLOR: blue">void</SPAN> _application_SheetSelectionChange(<SPAN style="COLOR: blue">object</SPAN> Sh, Excel.<SPAN style="COLOR: #2b91af">Range</SPAN> Target) {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&nbsp; _ribbonUI.Invalidate();<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">}</SPAN></P>
<P>Next, we implement&nbsp;the <FONT face="Courier New">GetColumnAnalysisEnabled </FONT><FONT color=#000000>method. This method returns true if&nbsp;the selected cell is inside a table and false otherwise. </FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">public</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: blue">bool</SPAN> GetColumnAnalysisEnabled(<SPAN style="COLOR: #2b91af">IRibbonControl</SPAN> Control) {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="COLOR: blue"><FONT color=#000000>&nbsp; </FONT>return</SPAN> <SPAN style="COLOR: #2b91af">ColumnAnalysisExtension</SPAN>.EnableUI(<SPAN style="COLOR: blue">this</SPAN>);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">}</SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">public</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: blue">static</SPAN> <SPAN style="COLOR: blue">bool</SPAN> EnableUI(<SPAN style="COLOR: #2b91af">Connect</SPAN> Connection) {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&nbsp; Excel.<SPAN style="COLOR: #2b91af">ListObjects</SPAN> listObjects = ((Excel.<SPAN style="COLOR: #2b91af">Worksheet</SPAN>)Connection.ExcelApp.ActiveSheet).ListObjects;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&nbsp; Excel.<SPAN style="COLOR: #2b91af">Range</SPAN> activeCell = Connection.ExcelApp.ActiveCell; </SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="COLOR: blue"><FONT color=#000000>&nbsp; </FONT>foreach</SPAN> (Excel.<SPAN style="COLOR: #2b91af">ListObject</SPAN> listObj <SPAN style="COLOR: blue">in</SPAN> listObjects) {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="COLOR: blue">&nbsp;&nbsp;&nbsp; if</SPAN> (listObj.SourceType == Excel.<SPAN style="COLOR: #2b91af">XlListObjectSourceType</SPAN>.xlSrcRange) {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="COLOR: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; List</SPAN>&lt;<SPAN style="COLOR: blue">object</SPAN>&gt; param = <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">List</SPAN>&lt;<SPAN style="COLOR: blue">object</SPAN>&gt;();<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Excel.<SPAN style="COLOR: #2b91af">Range</SPAN> intersection = <SPAN style="COLOR: #2b91af">ExcelUtility</SPAN>.ApplicationInvoke&lt;Excel.<SPAN style="COLOR: #2b91af">Range</SPAN>&gt;(Connection.ExcelApp, <SPAN style="COLOR: #a31515">"Intersect"</SPAN>, activeCell, listObj.Range);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: blue">if</SPAN> (intersection != <SPAN style="COLOR: blue">null</SPAN>){<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">return</SPAN> <SPAN style="COLOR: blue">true</SPAN>;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;</SPAN><SPAN style="COLOR: blue">return</SPAN> <SPAN style="COLOR: blue">false</SPAN>;</SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"></SPAN><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">}</SPAN></P>
<P>The <FONT face="Courier New">GetColumnAnalysisEnabled </FONT>method is defined on our <SPAN style="FONT-SIZE: 10pt; COLOR: #2b91af; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">Connect </SPAN>class (the class that implements <SPAN style="FONT-SIZE: 10pt; COLOR: #2b91af; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">IDTExtensibility2</SPAN> and <SPAN style="FONT-SIZE: 10pt; COLOR: #2b91af; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">IRibbonExtensibility</SPAN>). But we've seperated the&nbsp;implementation of the column analysis extension into a different class called <SPAN style="COLOR: #2b91af"><FONT face="Courier New">ColumnAnalysisExtension</FONT></SPAN>. </P>
<P>So, the Ribbon invokes <SPAN style="FONT-SIZE: 10pt; COLOR: #2b91af; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">Connect</SPAN>.<FONT face="Courier New">GetColumnAnalysisEnabled </FONT>which in turn invokes <SPAN style="FONT-SIZE: 10pt; COLOR: #2b91af; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="COLOR: #2b91af"><FONT face="Courier New">ColumnAnalysisExtension</FONT></SPAN></SPAN>.<FONT face="Courier New">EnableUI</FONT>.&nbsp;&nbsp;</P>
<P>We determine whether&nbsp;or not the active&nbsp;cell is in a table by looping through all the tables in the book and&nbsp;calculating the intersection of each table's range&nbsp;with the active cell. If the intersection is non null then the active cell is in the table's range; otherwise, it's not.&nbsp;</P>
<P>So, when the user clicks inside a table our button is enabled and when they click outside a table the button is disabled.</P>
<P mce_keep="true"><IMG src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/08/14/02/49/ColumnAnalysis_img2.png" original-url="http://blogs.msdn.com/photos/gabhan_berry/images/8140249/original.aspx" mce_src="http://blogs.msdn.com/photos/gabhan_berry/images/8140249/original.aspx"></P>
<P mce_keep="true"><STRONG>Calculating the Distribution of Values in a Column</STRONG></P>
<P mce_keep="true">There's no point writing our own code to count the occurances of values in an Excel range when we can just get Excel to do it. So, we'll make use of the existing COUNTA and COUNTIF worksheet functions to perform the actual calculations. </P>
<P mce_keep="true">For each value in the column we'll gather two values: the number of occurances and the percentage of occurances. We'll store this data in a&nbsp;<SPAN style="FONT-SIZE: 10pt; COLOR: #2b91af; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">Dictionary</SPAN><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&lt;<SPAN style="COLOR: blue">string</SPAN>, <SPAN style="COLOR: #2b91af">List</SPAN>&lt;<SPAN style="COLOR: blue">double</SPAN>&gt;&gt;</SPAN>&nbsp;object.</P>
<P mce_keep="true">Therefore, we need to write a method that does the following: </P>
<OL>
<LI>
<DIV mce_keep="true">Count the number of rows in&nbsp;the column by evaluating =COUNTA(&lt;column range&gt;);&nbsp;</DIV></LI>
<LI>
<DIV mce_keep="true">Determine all the unique values in the column;</DIV></LI>
<LI>
<DIV mce_keep="true">For each unique value: </DIV></LI>
<OL>
<LI>
<DIV mce_keep="true">evaluate =COUNTIF(&lt;column range&gt;, &lt;value&gt;) to calculate the number of occurances of the value in the column;</DIV></LI>
<LI>
<DIV mce_keep="true">Store the number of occurances and the percentage&nbsp;of occurances&nbsp;in our dictionary object;</DIV></LI></OL></OL>
<P mce_keep="true">Here's the code:</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">protected</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: blue">void</SPAN> CalculateStats() {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&nbsp;&nbsp;</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="COLOR: blue">string</SPAN> eval = <SPAN style="COLOR: #a31515">"=COUNTA("</SPAN> + <SPAN style="COLOR: #2b91af">ExcelUtility</SPAN>.RangeInvoke&lt;<SPAN style="COLOR: blue">string</SPAN>&gt;(_columnRange, <SPAN style="COLOR: #a31515">"Address"</SPAN>, <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">null</SPAN>, <SPAN style="COLOR: blue">null</SPAN>, Excel.<SPAN style="COLOR: #2b91af">XlReferenceStyle</SPAN>.xlA1) + <SPAN style="COLOR: #a31515">")"</SPAN>;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;</SPAN><SPAN style="COLOR: blue">double</SPAN> totalCount = (<SPAN style="COLOR: blue">double</SPAN>)_connection.ExcelApp.Evaluate(eval);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;</SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp; _stats = <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">Dictionary</SPAN>&lt;<SPAN style="COLOR: blue">string</SPAN>, <SPAN style="COLOR: #2b91af">List</SPAN>&lt;<SPAN style="COLOR: blue">double</SPAN>&gt;&gt;();<o:p></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes"></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN><SPAN style="COLOR: blue">foreach</SPAN> (Excel.<SPAN style="COLOR: #2b91af">Range</SPAN> cell <SPAN style="COLOR: blue">in</SPAN> _columnRange.Cells) {</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue"></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&nbsp;&nbsp;&nbsp; try</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><FONT color=#000000> {<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></SPAN><SPAN style="COLOR: blue">string</SPAN><FONT color=#000000> val = cell.Value2.ToString();<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><FONT color=#000000><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN></FONT><SPAN style="COLOR: blue">if</SPAN><FONT color=#000000> (!_stats.ContainsKey(val)) {<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><FONT color=#000000><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>_stats.Add(val, </FONT><SPAN style="COLOR: blue">new</SPAN><FONT color=#000000> </FONT><SPAN style="COLOR: #2b91af">List</SPAN><FONT color=#000000>&lt;</FONT><SPAN style="COLOR: blue">double</SPAN><FONT color=#000000>&gt;());<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><FONT color=#000000><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN>}<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><FONT color=#000000><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN>}<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;</FONT></SPAN><SPAN style="COLOR: blue">catch</SPAN><FONT color=#000000> (</FONT><SPAN style="COLOR: #2b91af">Exception</SPAN><FONT color=#000000>) {<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><FONT color=#000000><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN>}</FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"></SPAN></SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;</SPAN>}</SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p></o:p></SPAN>&nbsp;</P></SPAN></SPAN>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN><SPAN style="COLOR: blue">foreach</SPAN> (<SPAN style="COLOR: #2b91af">KeyValuePair</SPAN>&lt;<SPAN style="COLOR: blue">string</SPAN>, <SPAN style="COLOR: #2b91af">List</SPAN>&lt;<SPAN style="COLOR: blue">double</SPAN>&gt;&gt; entry <SPAN style="COLOR: blue">in</SPAN> _stats) {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>eval = <SPAN style="COLOR: #a31515">"=COUNTIF("</SPAN> + <SPAN style="COLOR: #2b91af">ExcelUtility</SPAN>.RangeInvoke&lt;<SPAN style="COLOR: blue">string</SPAN>&gt;(_columnRange, <SPAN style="COLOR: #a31515">"Address"</SPAN>, <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">null</SPAN>, <SPAN style="COLOR: blue">null</SPAN>, Excel.<SPAN style="COLOR: #2b91af">XlReferenceStyle</SPAN>.xlA1) + <SPAN style="COLOR: #a31515">", \""</SPAN> + entry.Key + <SPAN style="COLOR: #a31515">"\")"</SPAN>;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: blue">double</SPAN> count = (<SPAN style="COLOR: blue">double</SPAN>)_connection.ExcelApp.Evaluate(eval);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN>entry.Value.Add(count);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN>entry.Value.Add(count / totalCount);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;</SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">}</SPAN></P>
<P mce_keep="true">
<P mce_keep="true"><STRONG>Displaying the Data using a DataGridView Control and Custom Drawn Cells</STRONG></P>
<P mce_keep="true">The next stage is to display the data to the user. For this, we'll write a WinForm which contains a DataGridView control. In the DataGridView we'll list each value along with the value's statistics. However, inserting rows of data into a DataGridView control isn't that interesting so we'll not discuss that here. Instead we'll concentrate on the nicer bit: using custom drawn DataGridView cells. </P>
<P mce_keep="true">One of the big advantages of using .NET to write Excel addins is that we have access to all the .NET framework class library (FCL). This brings about some exciting possibilities for Excel addins, one of which is using WinForms to build good looking and modern user interfaces.</P>
<P mce_keep="true">The DataGridView control has a really nice feature which makes it very easy for us to perform custom drawing for cells in&nbsp;the control. It allows us to define a custom column class which the DataGridView will&nbsp;use as if it was an inbuilt column class.&nbsp; </P>
<P mce_keep="true">Then we define our custom&nbsp;cell class. This is the class which defines the cells in our custom column.&nbsp;The&nbsp;cell class&nbsp;extends one of the inbuilt DataGridViewXXXCell classes (such as <FONT face="Courier New"><SPAN style="COLOR: #2b91af">DataGridViewTextBoxCell</SPAN>)</FONT>.&nbsp;All we have to do is&nbsp;override the Paint method of the base class and perform the custom drawing that we want to do. </P>
<P mce_keep="true">Custom drawing doesn't get much easier than this!&nbsp;</P>
<P mce_keep="true">Let's remind ourselves what we want to draw. </P>
<P mce_keep="true"><IMG src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/08/13/92/79/ColumnAnalysis_img1.png" original-url="http://blogs.msdn.com/photos/gabhan_berry/images/8139279/original.aspx" mce_src="http://blogs.msdn.com/photos/gabhan_berry/images/8139279/original.aspx"></P>
<P mce_keep="true">We want to draw a rectangle whose size represents the percentage value of the item. So, North America has a value of 50.8% so we want the rectangle to fill (horizontally) 50.8% of the cell. In other words, we want something similar to conditional formatting in Excel, but in a DataGridView control.&nbsp;</P>
<P mce_keep="true">Notice that we also want to display the value itself as a number and we still want the cell borders and alternating background colours to be drawn. </P>
<P mce_keep="true">The first step, is to define our custom column class. We'll call this <FONT face="Courier New"><SPAN style="COLOR: #2b91af">DataGridViewBarChartColumn</SPAN> </FONT>and its definition is as follows:</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">public</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: blue">class</SPAN> <SPAN style="COLOR: #2b91af">DataGridViewBarChartColumn</SPAN> : <SPAN style="COLOR: #2b91af">DataGridViewColumn</SPAN> {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">public</SPAN> DataGridViewBarChartColumn() {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">this</SPAN>.CellTemplate = <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">DataGridViewBarChartCell</SPAN>();<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">this</SPAN>.ReadOnly = <SPAN style="COLOR: blue">true</SPAN>;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>}</SPAN></P>
<P mce_keep="true">
<P mce_keep="true">The <FONT face="Courier New">CellTemplate </FONT>property specifies the class which defines the cells that occur in the column. In this case its <FONT face="Courier New"><SPAN style="COLOR: #2b91af">DataGridViewBarChartCell</SPAN></FONT>. This is our custom cell class and it is in this class that we do the custom drawing.&nbsp;&nbsp;</P>
<P mce_keep="true">The <SPAN style="COLOR: #2b91af"><FONT face="Courier New">DataGridViewBarChartCell </FONT></SPAN>definition is as follows. </P>
<P mce_keep="true">
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">class</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: #2b91af">DataGridViewBarChartCell</SPAN> : <SPAN style="COLOR: #2b91af">DataGridViewTextBoxCell</SPAN> {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">protected</SPAN> <SPAN style="COLOR: #2b91af">Color</SPAN> _barColor = <SPAN style="COLOR: #2b91af">Color</SPAN>.FromArgb(50, 0, 0, 0);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">protected</SPAN> <SPAN style="COLOR: blue">override</SPAN> <SPAN style="COLOR: blue">void</SPAN> Paint(<SPAN style="COLOR: #2b91af">Graphics</SPAN> graphics,<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: #2b91af">Rectangle</SPAN> clipBounds,<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: #2b91af">Rectangle</SPAN> cellBounds,<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">int</SPAN> rowIndex,<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: #2b91af">DataGridViewElementStates</SPAN> elementState,<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">object</SPAN> value,<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">object</SPAN> formattedValue,<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: blue">string</SPAN> errorText,<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: #2b91af">DataGridViewCellStyle</SPAN> cellStyle,<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: #2b91af">DataGridViewAdvancedBorderStyle</SPAN> advancedBorderStyle,<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: #2b91af">DataGridViewPaintParts</SPAN> paintParts) {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">base</SPAN>.Paint(graphics,<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>clipBounds,<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;</SPAN>cellBounds,<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>rowIndex,<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>elementState,<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>value,<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>formattedValue,<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>errorText,<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>cellStyle,<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>advancedBorderStyle,<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>paintParts);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">float</SPAN> val = 0.0F;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">try</SPAN> {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>val = <SPAN style="COLOR: #2b91af">Convert</SPAN>.ToSingle(value);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>val /= 100.0F;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">catch</SPAN> (<SPAN style="COLOR: #2b91af">InvalidCastException</SPAN> InvalidCastEx) {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">return</SPAN>;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: #2b91af">RectangleF</SPAN> barRect = cellBounds;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>barRect.Width *= val;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">using</SPAN> (<SPAN style="COLOR: #2b91af">SolidBrush</SPAN> brush = <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">SolidBrush</SPAN>(_barColor)) {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>graphics.FillRectangle(brush, barRect);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>}</SPAN></P>
<P mce_keep="true"><SPAN style="COLOR: #2b91af"><FONT face="Courier New">DataGridViewBarChartCell </FONT></SPAN>is a subclass&nbsp;of the inbuilt&nbsp;<FONT face="Courier New"><SPAN style="COLOR: #2b91af">DataGridViewTextBoxCell</SPAN>&nbsp;</FONT>class. Extending the inbuilt class means that all we have to do is override the paint behaviour of the <FONT face="Courier New"><SPAN style="COLOR: #2b91af">DataGridViewTextBoxCell</SPAN> </FONT>class and let the <FONT face="Courier New"><SPAN style="COLOR: #2b91af">DataGridViewTextBoxCell</SPAN> </FONT>class handle everything else.</P>
<P mce_keep="true">In the Paint method you can see that we are automatically provided with a Graphics object and a bounding rectangle in which to paint. All we need to do is calculate the width of the rectangle and then call <FONT face="Courier New">Graphics.FillRectangle</FONT>. </P>
<P mce_keep="true">
<P mce_keep="true">Finally, notice that the first thing we do in the Paint method is call <FONT face="Courier New"><SPAN style="COLOR: blue">base</SPAN>.Paint</FONT>. This means we don't have to care about drawing the cell's value or the background or the borders because the base class will do that for us. All we do is then draw on top of what the base class draws. If we didn't call <FONT face="Courier New"><SPAN style="COLOR: blue">base</SPAN>.Paint </FONT>then the cells would only contain the filled rectangle and nothing else. </P>
<P mce_keep="true"><STRONG>Hooking it all Together</STRONG></P>
<P mce_keep="true">To make the DataGridView control use our custom column class, we set the type of the appropriate column in the DataGridView control to <FONT face="Courier New"><SPAN style="COLOR: #2b91af">DataGridViewBarChartColumn</SPAN></FONT>.&nbsp;&nbsp;</P>
<P mce_keep="true">The final stage is to display our window when the user clicks the button in the Ribbon. Looking back at our Ribbon XML we defined the onAction callback for the button as <FONT face="Courier New"><SPAN style="COLOR: red">onAction</SPAN><SPAN style="COLOR: blue">=</SPAN>"<SPAN style="COLOR: blue">OnColumnAnalysisClicked</SPAN>"</FONT>. This means that the Ribbon will call the <FONT size=2><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">OnColumnAnalysisClicked </SPAN></FONT>method on our addin when the user clicks the button, so, all we need to do is define the <FONT face="Courier New">OnColumnAnalysisClicked </FONT>method and make it public.</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">public</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: blue">void</SPAN> OnColumnAnalysisClicked(<SPAN style="COLOR: #2b91af">IRibbonControl</SPAN> Control) {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="COLOR: #2b91af"><FONT color=#000000>&nbsp; </FONT>ColumnAnalysisExtension</SPAN> ext = <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">ColumnAnalysisExtension</SPAN>(<SPAN style="COLOR: blue">this</SPAN>);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">}</SPAN></P>
<P mce_keep="true">
<P mce_keep="true">In this method we are creating a new <SPAN style="COLOR: #2b91af"><FONT face="Courier New">ColumnAnalysisExtension </FONT></SPAN>object. The constructor of this class simply calls our <FONT face="Courier New">CalculateStats </FONT>method and then displays the window containing the DataGridView control (it does one or two other things too).</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">public</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> ColumnAnalysisExtension(<SPAN style="COLOR: #2b91af">Connect</SPAN> Connection) {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&nbsp; _connection = Connection;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;</SPAN>BuildColumnRange();<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;</SPAN><SPAN style="COLOR: blue">if</SPAN> (_columnRange != <SPAN style="COLOR: blue">null</SPAN>) {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>CalculateStats();<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>DisplayStatsWindow();<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;</SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">}</SPAN></P>
<P mce_keep="true">
<P mce_keep="true"><STRONG>Summary</STRONG></P>
<UL>
<LI>
<DIV mce_keep="true">Use the IRibbonUI.Invalidate method to force the Ribbon to redraw your custom Ribbon controls when you need their state to change;</DIV></LI>
<LI>
<DIV mce_keep="true">To avoid having to ship your Ribbon XML, compile it as an embedded resource into your assembly;</DIV></LI>
<LI>
<DIV mce_keep="true">Using .NET to write user interfaces offers exciting UI possibilities, such as easily-to-code custom drawn&nbsp;controls;</DIV></LI>
<LI>
<DIV mce_keep="true">To custom draw cells in the DataGridView control, subclass the DataGridViewColumn class and one of the inbuilt DataGridViewXXXCell classes;</DIV></LI>
<LI>
<DIV mce_keep="true">The code for the custom drawing should override the Paint method of the custom cell's base class;</DIV></LI></UL>
<P mce_keep="true"><STRONG>Download the Excel Extensions Addin</STRONG></P>
<P mce_keep="true">The Excel Extensions addin and full source code can be downloaded from the MSDN Code Gallery <A class="" href="http://code.msdn.microsoft.com/ExcelExtensions" mce_href="http://code.msdn.microsoft.com/ExcelExtensions">here</A>. </P>
<P mce_keep="true">There are two downloads which can be downloaded from the Releases tab. There are: ExcelExtensionsSrc.zip and ExcelExtensions.msi. </P>
<P mce_keep="true">ExcelExtensionsSrc.zip contains the complete source code and ExcelExtensions.msi is the set up program. </P>
<P mce_keep="true">Over time, I'll be adding additional feature to the addin.</P>
<P mce_keep="true">&nbsp;</P>
<P mce_keep="true">&nbsp;</P>
<P mce_keep="true">&nbsp;</P>
<P mce_keep="true">&nbsp;</P>
<P mce_keep="true">&nbsp;</P>
<P mce_keep="true">&nbsp;</P>]]></content:encoded>
    </item>
    <item>
      <title>Using Custom Functions in Dynamic Ranges</title>
      <link>https://docs.microsoft.com/archive/blogs/gabhan_berry/using-custom-functions-in-dynamic-ranges</link>
      <pubDate>Tue, 19 Feb 2008 12:33:00 GMT</pubDate>
      <dc:creator><![CDATA[Gabhan Berry]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/gabhan_berry/2008/02/19/using-custom-functions-in-dynamic-ranges/</guid>
      <description><![CDATA[Named ranges are great. They enable us to assign a name to a collection of cells and use that name...]]></description>
      <content:encoded><![CDATA[<P>Named ranges are great. They enable us to assign a name to a collection of cells and use that name in code and worksheet functions. However, named ranges tend to be static. That is, they tend to represent a fixed set of cells, such as, A1:E100. But sometimes it would be useful to have the range's set of cells be dynamic. Instead of the range representing a fixed set of cells, we would like to dynamically determine which cells are included in the range based on some custom logic. </P>
<P>Excel already lets us do this. It allows us to embed functions inside the definition of a named range and have the functions determine on-the-fly which cells are included in the range. But it's not just native Excel functions (like IF, OFFSET, etc.) that we can embed into a named range defintion. We can also embed our own, custom functions; we can write a custom function that uses our own application logic to dynamically determine at calculation time which cells are in a named range.</P>
<P>This gives us the capability to <EM>point</EM> the named range at different cells&nbsp;without actually changing the named range's definition. </P>
<P>In this posting, I'll talk about dynamic ranges&nbsp;and&nbsp;embedding custom functions into named range defintions.</P>
<P mce_keep="true"><STRONG>Getting Started with Dynamic Ranges</STRONG></P>
<P mce_keep="true">Let's take a simple example. Let's say that we have a column of numbers in the range 'Example 1'!$C$1:$C$100 and we want to find the cells in this range that are higher than some value, X = 34,999.</P>
<P mce_keep="true">Using dynamic ranges, we can create a named range (called, say, Range 1) with a definition of:</P>
<P mce_keep="true">=GreaterThan('Example 1'!$C$1:$C$100, 34999)</P>
<P mce_keep="true"><IMG src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/07/80/12/59/DynamicRanges_img0.png" original-url="http://blogs.msdn.com/photos/gabhan_berry/images/7801259/original.aspx" mce_src="http://blogs.msdn.com/photos/gabhan_berry/images/7801259/original.aspx"></P>
<P mce_keep="true">We also define a UDF called GreaterThan as:</P>
<P mce_keep="true">Public Function GreaterThan(Rng As Range, Limit As Long) As Range<BR>&nbsp;&nbsp;&nbsp; Dim Cell As Range<BR>&nbsp;&nbsp;&nbsp; Dim ResultRange As Range<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; For Each Cell In Rng<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If Cell.Value2 &gt; Limit Then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If ResultRange Is Nothing Then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set ResultRange = Cell<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set ResultRange = Application.Union(ResultRange, Cell)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<BR>&nbsp;&nbsp;&nbsp; Next<BR>&nbsp;&nbsp;&nbsp; Set GreaterThan = ResultRange<BR>End Function</P>
<P mce_keep="true">The UDF iterates over the source range testing&nbsp;each cell. If the cell meets our criteria (in this case its value being greater than 34999)&nbsp;then it is added into a temporary range object. Once the code has iterated over all the cells, the temporary range is returned to the caller. This range represents the set of cells the named range now refers to.</P>
<P mce_keep="true">(Note: we're not coping with any runtime errors in this code ... which we should do in a proper version).</P>
<P mce_keep="true">We can use the named range just like any other named range and Excel will call our UDF as and when it needs to. So, we could calculate the average of our range by using normal worksheet functions, such as:</P>
<P mce_keep="true">=AVERAGE(Range1)</P>
<P mce_keep="true"><IMG src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/07/80/14/30/DynamicRanges_img1.png" original-url="http://blogs.msdn.com/photos/gabhan_berry/images/7801430/original.aspx" mce_src="http://blogs.msdn.com/photos/gabhan_berry/images/7801430/original.aspx">&nbsp;</P>
<P mce_keep="true">We can visually confirm the range that our UDF returned using the Name Manager. If we open up the Name Manager and select Range 1 and then click inside the 'Refers to:' textbox, Excel invokes our UDF and highlights the cells on the worksheet that our UDF returned. </P>
<P mce_keep="true"><IMG src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/07/80/54/04/DynamicRanges_img4.png" original-url="http://blogs.msdn.com/photos/gabhan_berry/images/7805404/original.aspx" mce_src="http://blogs.msdn.com/photos/gabhan_berry/images/7805404/original.aspx"></P>
<P mce_keep="true"><STRONG>Recalculating the Range&nbsp;when Data Changes</STRONG>&nbsp;</P>
<P mce_keep="true">Excel knows that our dynamic range depends on $C$1:$C$100.&nbsp;This is&nbsp;because we pass $C$1:$C$100 to the UDF call in the named range definition.</P>
<P mce_keep="true">This enables Excel to properly calculate our named range during worksheet calculations. &nbsp;&nbsp;</P>
<P mce_keep="true">In our example, the cell $E$3 (which contains the formula =AVERAGE(Range1)) depends on Range 1 and Range 1 depends on $C$1:$C$100. Therefore, if $C$1:$C$100 changes, Excel knows that it needs to recalculate Range 1 in order to recalculate $E$3. </P>
<P mce_keep="true">So, if we change the value of $C$1 from 40000 to 20000 Excel knows that it needs to recalculate $E$3 and, hence, invokes our UDF.&nbsp;Our UDF determines which cells Range 1 refers to and deems that&nbsp;$C$1 is no longer part of Range 1 (because its value is no longer&nbsp;&gt; 34999); thus the value of $E$3 changes, as shown below.</P>
<P mce_keep="true"><IMG style="WIDTH: 299px; HEIGHT: 331px" height=331 src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/07/80/47/05/DynamicRanges_img2.png" original-url="http://blogs.msdn.com/photos/gabhan_berry/images/7804705/original.aspx" width=299 mce_src="http://blogs.msdn.com/photos/gabhan_berry/images/7804705/original.aspx"></P>
<P mce_keep="true">We can also confirm&nbsp;that Range1 is comprised of different cells&nbsp;by inspecting&nbsp;Range1's address in the VBA Immediate window (this also illustrates that we can use the dynamic named range in VBA just like we can use a 'normal' named range).</P>
<P mce_keep="true"><IMG src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/07/80/48/21/DynamicRanges_img3.png" original-url="http://blogs.msdn.com/photos/gabhan_berry/images/7804821/original.aspx" mce_src="http://blogs.msdn.com/photos/gabhan_berry/images/7804821/original.aspx"></P>
<P mce_keep="true"><STRONG>A Note about Interacting with the Calc Tree</STRONG></P>
<P mce_keep="true">If our UDF didn't declare its source data range as a parameter then Excel would not be able to call it at the appropriate times during recalculation. </P>
<P mce_keep="true">For example, if our UDF simply iterated through $C$1:$C$100 without declaring this range as a parameter then, when $C$1:$C$100 is changed, $E$3 wouldn't be recalculated. </P>
<P mce_keep="true">This is because Excel would not know that $E$3 depends on $C$1:$C$100 because that dependency&nbsp;would be&nbsp;hidden inside the UDF.</P>
<P mce_keep="true">There may circumstances when we don't care about this. But it is worth remembering that Excel needs to know about any worksheet dependencies our UDF has in order for our dynamic range to interact with the calculation tree properly.</P>
<P mce_keep="true"><STRONG>Making More Use of Dynamic Ranges</STRONG></P>
<P mce_keep="true">Our GreaterThan UDF is about as basic as could be, but, it illustrates the technique. It doesn't, however, take much imagination to see how this technique could be used in more sophisticated ways. </P>
<P mce_keep="true">For example, inside our UDF we could make use of external data, such as our company's databases, and use the logic and/or data stored in them to determine the contents of the dynamic range in Excel. </P>
<P mce_keep="true">One example could be that we have a list of dates in our worksheet and we want to highlight the dates on which somebody downloaded a trial version of our product from our web site. This would&nbsp;make use&nbsp;of&nbsp;the same technique described here, except, in the loop in our UDF, we would make a call to the database to determine whether that cell should or should not be included in the range. </P>
<P mce_keep="true">In other words, using UDFs in dynamic ranges enables us to incorporate external data and external&nbsp;components into our range definitions.</P>
<P mce_keep="true">Another use is: search. We could write a fairly sophisticated custom, search feature that is built on top of dynamic ranges and UDFs.</P>
<P mce_keep="true"><STRONG>Concerns About Execution Speed</STRONG></P>
<P mce_keep="true">I'm afraid I've left the bad news until the end... </P>
<P mce_keep="true">Implementing dynamic ranges exactly as I have described here could hand you&nbsp;serious performance issues. </P>
<P mce_keep="true">To illustrate this, let's change the definition of Range 1 from:</P>
<P mce_keep="true">&nbsp;=GreaterThan('Example 1'!<STRONG>$C$1:$C$100</STRONG>, 34999) </P>
<P mce_keep="true">to:</P>
<P mce_keep="true">&nbsp;=GreaterThan('Example 1'!<STRONG>$C:$C</STRONG>, 34999).</P>
<P mce_keep="true">Now, we are checking for cells greater than 34999 in the <EM>entire</EM> C column. In Excel 2007, that's 1,048,576 cells. On my PC (which is a 2.66 GHz&nbsp;dual core with 3GB RAM - not that dual core helps here anyway), calculating the range <EM>once</EM> takes 3 seconds. If we referenced Range 1 in only a handful of places in our workbook or VBA, our workbook would become fairly unusable fairly quickly.</P>
<P mce_keep="true">It would be interesting to see if implementing our UDF in an XLL would improve the speed substantially -&nbsp;(something I'll look into).&nbsp;</P>
<P mce_keep="true">Also, if our UDF did fancier things (like calling into an external database) then you can just imagine what would happen when we're iterating over 1,048,576 cells ....&nbsp;&nbsp;</P>
<P mce_keep="true">So, it's worth stating that we should exercise some common sense and some judgement when using dynamic ranges. However, if we restrict our usage to <EM>suitably sized</EM> source ranges, dynamic ranges are a useful tool indeed (where <EM>suitably sized</EM> is something we each have to define for ourselves).</P>
<P mce_keep="true"><STRONG>Summary</STRONG></P>
<UL>
<LI>
<DIV mce_keep="true">Named range definitions&nbsp;can contain&nbsp;native and custom functions;</DIV></LI>
<LI>
<DIV mce_keep="true">A custom function used&nbsp;as a named range definition should return the range that it determines the named range represents;</DIV></LI>
<LI>
<DIV mce_keep="true">To interact properly with Excel's calculation tree, all worksheet dependencies (such as the source range) should be included as parameters to the custom function;</DIV></LI>
<LI>
<DIV mce_keep="true">Dynamic ranges can be used to build some fairly useful and sophisticated extensions;</DIV></LI>
<LI>
<DIV mce_keep="true">Speed when iterating over large ranges is not good and we should employ careful judgment when using dynamic ranges on potentially large source ranges;</DIV></LI></UL>
<P mce_keep="true">&nbsp;</P>
<P mce_keep="true">You can download the workbook I used for this post in the list of attachments below.</P>
<P mce_keep="true">&nbsp;</P>
<P mce_keep="true">&nbsp;</P>
<P mce_keep="true">&nbsp;</P>
<P mce_keep="true">&nbsp;</P>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/07/79/81/80/Dynamic%20Ranges.xlsm" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-07-79-81-80/Dynamic-Ranges.xlsm">Dynamic Ranges.xlsm</a></p>]]></content:encoded>
    </item>
  </channel>
</rss>