Using VSTO to expand a C# array (1D or 2D) into Excel following a UDF callHi all,<br/> <br/> I have some C# functions that I have exposed to Excel so that they can be called from Excel cells (UDFs). Some of these functions return arrays or even matrices. I have two questions in fact:<br/> <br/> 1- If I am to get the instance of the running Excel workbook, how can I get the cell from which the UDF call was made?<br/> 2- Is there some built-in functionality in VSTO to expand array results automatically in the cell where the UDF call was made?<br/> <br/> Thanks!<br/> <br/> Best Regards,<br/> Fayssal© 2009 Microsoft Corporation. All rights reserved.Thu, 09 Jul 2009 06:09:29 Zc575930c-3fd2-458d-8f85-e039e50c5c64http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/c575930c-3fd2-458d-8f85-e039e50c5c64#c575930c-3fd2-458d-8f85-e039e50c5c64http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/c575930c-3fd2-458d-8f85-e039e50c5c64#c575930c-3fd2-458d-8f85-e039e50c5c64Fayssal El Moufatichhttp://social.msdn.microsoft.com/Profile/en-US/?user=Fayssal%20El%20MoufatichUsing VSTO to expand a C# array (1D or 2D) into Excel following a UDF callHi all,<br/> <br/> I have some C# functions that I have exposed to Excel so that they can be called from Excel cells (UDFs). Some of these functions return arrays or even matrices. I have two questions in fact:<br/> <br/> 1- If I am to get the instance of the running Excel workbook, how can I get the cell from which the UDF call was made?<br/> 2- Is there some built-in functionality in VSTO to expand array results automatically in the cell where the UDF call was made?<br/> <br/> Thanks!<br/> <br/> Best Regards,<br/> FayssalFri, 03 Jul 2009 13:45:29 Z2009-07-04T00:51:20Zhttp://social.msdn.microsoft.com/Forums/en-US/vsto/thread/c575930c-3fd2-458d-8f85-e039e50c5c64#cdc1c5f2-89a0-4a44-9351-b2b70d6cdc10http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/c575930c-3fd2-458d-8f85-e039e50c5c64#cdc1c5f2-89a0-4a44-9351-b2b70d6cdc10Cindy Meisterhttp://social.msdn.microsoft.com/Profile/en-US/?user=Cindy%20MeisterUsing VSTO to expand a C# array (1D or 2D) into Excel following a UDF callHi Fayssal<br/><br/>VSTO has absolutely nothing to do with UDF. It does not support UDF in any way. You'll find alternate resources in the forum's <a href="http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/063a23a6-1595-4c83-a25f-6c94658c4649">Please Read First</a> message where you can ask questions not related to VSTO.<br/><br/>FWIW, if your custom function returns an array and you want the array written to the selected set of cells, the user must press a certain keyboard combination (rather than Enter) to confirm entering an array. I <em>think</em> the combination is Ctrl+Enter (or maybe it's Shift+Enter - one of those, anyway). This will write the forumula with { braces } which indicates it's an array. And Excel will then take care of populating the cell selection with the array.<br/><br/>There was a discussion about this in the forum, some time back. If you search UDF and array you should turn it up and find the details.<hr class="sig">Cindy Meister, VSTO/Word MVPSat, 04 Jul 2009 10:18:13 Z2009-07-04T10:18:13Zhttp://social.msdn.microsoft.com/Forums/en-US/vsto/thread/c575930c-3fd2-458d-8f85-e039e50c5c64#9fe4291d-0bb8-4b7b-bc96-202c75f24b42http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/c575930c-3fd2-458d-8f85-e039e50c5c64#9fe4291d-0bb8-4b7b-bc96-202c75f24b42Fayssal El Moufatichhttp://social.msdn.microsoft.com/Profile/en-US/?user=Fayssal%20El%20MoufatichUsing VSTO to expand a C# array (1D or 2D) into Excel following a UDF call<p>Thanks Cindy! Indeed, it seems this cannot be achieved using VSTO. More details could be found in this thread:<br/><br/><a href="http://www.xtremedotnettalk.com/showthread.php?t=96044">http://www.xtremedotnettalk.com/showthread.php?t=96044</a><br/><br/>BTW, the command to expand the results of a UDF is: <strong>Ctrl-Shift-Enter.</strong><br/>Do you have any idea why VSTO does not want to support UDFs for Document-level add-ins?<br/><br/>Anyways, I was trying to get access to the Excel instance from which the UDF call was initiated, and I ran into the dreadful COMException when trying to modify some cells from the active sheet. If somebody runs into this problem, this might be helpful:<br/><br/><span style="font-size:x-small;color:#008000"><a href="http://support.microsoft.com/kb/316125/EN-US/">http://support.microsoft.com/kb/316125/EN-US/</a></span><span style="font-size:x-small"></span> <p><span style="font-size:x-small;color:#008000"><a href="http://support.microsoft.com/kb/316126">http://support.microsoft.com/kb/316126</a><span style="font-size:x-small;color:#008000"> <p> </p> </span></span></p> <p>Best Regards,<br/>Fayssal</p> </p>Mon, 06 Jul 2009 10:22:53 Z2009-07-06T10:24:19Z