title: Early Binding of a C# COM library in VBA
body: <p>Although this is a long question the coding and testing part should be really easy to reproduce. </p>
  
  <p>I have created <strong>two</strong> separate <code>Class Libraries</code> in <code>C#</code> and I think I am running into a name collision problem caused by existing registry keys from my previous projects and trials.</p>
  
  <p>Here are my two classes:</p>
  
  <pre><code>using System;
  using System.Runtime.InteropServices;
  
  namespace Test
  {
  
      [InterfaceType(ComInterfaceType.InterfaceIsDual),
      Guid("ED5D264B-1D80-4A5D-9C14-8297D90B7037")]
      public interface ITest
      {
          // body
      }
  
      [ClassInterface(ClassInterfaceType.None)]
      [Guid("8B261B92-8EC5-4CDC-A551-67DEB42137FF")]
      [ProgId("Test.TestClass")]
      public class TestClass : ITest
      {
          // body
      }
  } 
  </code></pre>
  
  <p>and</p>
  
  <pre><code>using System;
  using System.Runtime.InteropServices;
  using ADODB;
  
  namespace Test
  {
  
      [InterfaceType(ComInterfaceType.InterfaceIsDual),
      Guid("ED5D264B-1D80-4A5D-9C14-8297D90B7037")]
      public interface IConnection
      {
          // body
      }
  
      [ClassInterface(ClassInterfaceType.None)]
      [Guid("8B261B92-8EC5-4CDC-A551-67DEB42137FF")]
      [ProgId("Test.Connection")]
      public class Connection : IConnection
      {
          // body
      }
  }
  </code></pre>
  
  <p>I have <a href="http://www.codeproject.com/Articles/3511/Exposing-NET-Components-to-COM" rel="nofollow noreferrer">Exposed .Net Components to COM</a> like this: <br>
  In order to access the assemblies from Excel I have added the <em>ADODB references</em> to the assembly, ticked <em>make assembly COM visible</em> and <em>register for com interop</em>. Also, I've added references to each <code>*.tlb</code> file(<em>2 files for two projects</em>) so I can access them using an <a href="http://msdn.microsoft.com/en-us/library/0tcf61s1(v=vs.80).aspx" rel="nofollow noreferrer">early binding</a> and use VBA <a href="http://en.wikipedia.org/wiki/Intelli-sense" rel="nofollow noreferrer">Intellisense</a>.</p>
  
  <p>I have followed the same procedure on another machine and I can use early binding using the <code>Connection</code> as class.</p>
  
  <p>I am thinking there are some old registry keys I haven't deleted on my original machine which will not allow me to use <code>Connection</code> as the class name in VBE. I've manually scanned my registry and deleted everything I could think of related to my project.</p>
  
  <p>I have also deleted the project entirely and used a 3rd party software to scan registry for missing <code>dll</code>s however that didn't help:/</p>
  
  <p>Removed all previously registered GUIDs and applied new ones each time I created a new Project (<em>just in case</em>)</p>
  
  <p>Created new projects using different namespaces and class names (<code>using ADODB;</code>) I haven't been able to use early binding yet like this <code>Test.Connection</code> therefore I am assuming I have a <a href="http://en.wikipedia.org/wiki/Name_collision" rel="nofollow noreferrer"><em>name collision</em></a> problem. I am suspecting the name class <code>Connection</code> to be causing it although I am not 100% sure. </p>
  
  <p>The <code>Test.TestClass</code> namespace in VBA:</p>
  
  <p>I can declare and use instances of the <code>TestClass</code> type in two ways using early binding:</p>
  
  <pre><code>Dim x as Test.TestClass
  Dim x as TestClass
  </code></pre>
  
  <p>Now going into VBE Object Explorer <kbd>F2</kbd> the <code>TestClass</code> is properly displayed in comparison to other libraries and general idea of using COMs.</p>
  
  <p><img src="https://i.stack.imgur.com/PiJyK.png" alt="TestClass Object Explorer"></p>
  
  <p>However, when I want to use the <code>Test.Connection</code> library I am unable to use early binding following the same pattern as <code>TestClass</code> because the generated <code>*.tlb</code> file automatically changes(<em>renames</em>) the <code>ProgId's</code>. So, instead I have to bind it like this</p>
  
  <pre><code>Dim x As Test.Test_Connection
  Dim x As Test_Connection
  </code></pre>
  
  <p>and the <code>Object Explorer</code> displays the names using <code>_</code> (underscores) and not <code>.</code> (dots), which is easy to explain why this happens - keep reading :)</p>
  
  <p><img src="https://i.stack.imgur.com/g6xeC.png" alt="not able to use early binding"></p>
  
  <p>As it stands I am sure it is not the VBE environment that changes the names to avoid collisions. It is the VS' <code>*.tlb</code> generator. </p>
  
  <p>I went to the assembly folder and opened both <code>*.tlb</code> files in <code>Notepad++</code>. I can clearly see that the <code>*.tlb</code> for the <code>Test.Connection</code> library already includes the names with the <code>_</code>s unlike the <code>Test.TestClass</code> which has <code>.</code>s<br></p>
  
  <p>I have tried to manually edit the <code>*.tlb</code> file but as its a mixed binary file it takes some effect but also causes Excel to stop responding in some weird ways so I have to avoid this method. <br></p>
  
  <p>I think I have explained well what the problem is and where it comes from. Now my question is: <br> Are there any attributes to use in C# code to tell the <code>*.tlb</code> generator not to override my <code>ProdId</code>s? <br> Are there any alternative ways of manipulating <code>*.tlb</code> files?<br> Is this issue a <code>name collision</code> and is it avoidable without changing the name of <code>Connection</code> class? </p>
  
  <p>I'm sorry for such long question but I have been digging and digging for almost a week now and I still cant solve this. </p>
  
  <p>Note: In VBA ( or VBE Object Explorer ) using IntelliSense <kbd>ctrl</kbd>+<kbd>space</kbd> it does <strong>not</strong> seem that either <code>Connection</code> or <code>Recordset</code> have been used. Since they are not already reserved in the VBE environment I recon it has to do with my library itself.</p>
  
  <p>As a reference to why this issue has been raised here, please see <a href="https://stackoverflow.com/questions/17569233/vba-equivalent-to-c-sharp-using-or-vb-net-imports-creating-aliases">VBA equivalent to C# using or VB.NET imports creating aliases</a><br>
  Thank you very much for your time!</p>