title: vb.net & office: Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass'
body: <p>I'm developing a vb.net application and it should open and handle a excel file.
  The problem is that in my pc (and the same could happen in other pc where it could be installed) I have Visio version 2013 and Office (excel, word, etc..) version 2010. Integrating excel in vb is so simple because more or less automatic, done in few steps.. but in the same time so stupid: calling interop.Excel functions, it addresses to the newest version of office intalled (2013), but I don't have excel for that version!
  I searched a lot on the web about this issue, and every where I found the solution of deleting a registry key, well, it works fine, but every time I use Visio, it seems the registry key is recreated again.</p>
  
  <p>My questions are:</p>
  
  <ul>
  <li><p>can I select which version of excel I would like to link my application?
  I can understand that fixing the version I should handle any office versions
  manually.. but maybe I prefer doing this..</p></li>
  <li><p>can I copy a dll in the application folder to be sure that the app uses that 
  version? I tried with Microsoft.Office.Interop.Excel.dll and Office.dll but still the issue</p></li>
  </ul>
  
  <p>I don't want to follow the way of deleting the registry key programmatically, is not safe in my opinion..
  I can't believe that the integration between office and .net is so stupid that is not able to use the right version of excel installed!! 
  ;-P</p>
  
  <p>Code:</p>
  
  <pre><code>        Dim fileTest As String = FilePath
  
      Dim APP As New Application
      MsgBox(APP.Version)
  
      Dim oSheet As Excel.Worksheet = Nothing
      Dim workbook As Excel.Workbook = Nothing
      Dim IndexFound As Integer = 0
      Dim StartRow As Integer = 10
      Dim DateSelected As String = ""
      Dim resultOK As Boolean = False
  
      Try
  
          workbook = APP.Workbooks.Open(fileTest)
  
          oSheet = workbook.Worksheets("Activities")
  
          APP.DisplayAlerts = False
  
          oSheet.Range("A2").Value = "test"
          oSheet.Range("B3").Value = "Ciao"
  
      Catch ex As Exception
          MsgBox("Error: " + ex.Message)
  
          If Not resultOK Then
              If Not workbook Is Nothing Then
                  workbook.Close()
                  While System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook) &gt; 0
                  End While
                  workbook = Nothing
              End If
              oSheet = Nothing
              workbook = Nothing
              APP.Quit()
              System.Runtime.InteropServices.Marshal.ReleaseComObject(APP)
              APP = Nothing
              GC.Collect()
              GC.WaitForPendingFinalizers()
          End If
      End Try
  
  End Sub
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/HB2Hc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HB2Hc.png" alt="Reference excel interop"></a>
  <a href="https://i.stack.imgur.com/6hfyX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6hfyX.png" alt="Reference Office core"></a></p>
  
  <p>Thanks
  Andrea</p>
  
  <p><a href="https://i.stack.imgur.com/OSzrC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OSzrC.png" alt="enter image description here"></a></p>