title: Register DLL with regasm from VBA with shell command
body: <p>Okay so I'm trying to package a DLL being used by my spreadsheet with the excel file. My spreadsheet will be copied to different PC's and used from different places and I'm trying to avoid having the user run regasm every time the worksheet (with the DLL) is copied to a new PC. I've resorted to doing a shell command to register the DLL with regasm on startup. Here is my code:</p>
  
  <pre><code>Private Sub Workbook_BeforeClose(Cancel As Boolean)
      Dim strWinCmd As String
      Dim retVal As Double
      strWinCmd = "cmd.exe %SystemRoot%\Microsoft.NET\Framework\v2.0.50727\regasm.exe /u /codebase /tlb """ &amp; Application.ActiveWorkbook.Path &amp; "\JART\JART.dll"""
      retVal = Shell(strWinCmd, vbHide)
  End Sub
  
  Private Sub Workbook_Open()
      Dim strWinCmd As String
      Dim retVal As Double
      strWinCmd = "cmd.exe /c %SystemRoot%\Microsoft.NET\Framework\v2.0.50727\regasm.exe /codebase /tlb """ &amp; Application.ActiveWorkbook.Path &amp; "\JART\JART.dll"""
      Call Shell(strWinCmd, vbNormalFocus)
  
      Call Button_Handlers.Sleep(1500)
  
      Call Button_Handlers.Initialize
  End Sub
  </code></pre>
  
  <p>For reference the Button_Handlers.Sleep just calls the system sleep method and Button_Handlers.Initialize does this:</p>
  
  <pre><code>Sub Initialize()
      'This JART.MainJobControl is the main class in the JART DLL
      Set JART_Instance = New JART.MainJobControl
      'Use JART_Instance
  End Sub
  </code></pre>
  
  <p>So basically I'm trying to register the DLL at start-up and un-register it on close. My problem is that when I open the spreadsheet on a new PC I get an error in Button_Handlers.Initialize. It tells me that I'm trying to use an undefined class (JART.MainJobControl), as if the DLL wasn't referenced. If I try to reopen the file everything works fine???</p>
  
  <p>The way I'm doing this is manually adding the reference to the DLL on a machine that already has it registered with regasm. I then save this excel file and transport it to a machine that hasn't had the DLL registered and try to open it and run it. I think that since the reference is already added to the excel file, all's the code has to do is register it with regasm. Does anyone know why this wouldn't work? Am I not sleeping long enough.</p>