I'm creating a method that will automatically create an Excel file. The appearance of the Excel file (format) will depend on the given VB.NET script. Now I'm having a problem with regards to referencing the assembly Microsoft.Office.Interop.Excel.dll. Here's my sample code below:
Imports System.CodeDom.Compiler
Imports Microsoft.Office.Interop
Public Class ExcelScript
Public Sub ExecuteExcelScript(VBCode As String, ds As DataSet)
Dim provOptions As New Dictionary(Of String, String)
provOptions.Add("CompilerVersion", "v4.0")
Dim vbProvider As New VBCodeProvider(provOptions)
Dim vbParameter As New CompilerParameters
Dim compResults As CompilerResults = Nothing
VBCode = "Imports System" & vbNewLine &
"Imports System.Data" & vbNewLine &
"Imports System.Data.SqlClient" & vbNewLine &
"Imports Microsoft.Office.Interop" & vbNewLine &
"Public Class GenerateExcel" & vbNewLine &
"Public Sub GenerateExcelFromScript(ds As DataSet)" & vbNewLine &
VBCode & vbNewLine &
"End Sub" & vbNewLine &
"End Class"
vbParameter.ReferencedAssemblies.Add("System.dll")
vbParameter.ReferencedAssemblies.Add("System.Data.dll")
vbParameter.ReferencedAssemblies.Add("System.Xml.dll")
vbParameter.ReferencedAssemblies.Add("Microsoft.Office.Interop.Excel.dll")
vbParameter.GenerateExecutable = False
vbParameter.GenerateInMemory = True
vbParameter.OutputAssembly = "ExcelGenerator"
compResults = vbProvider.CompileAssemblyFromSource(vbParameter, VBCode)
End Sub
End Class
The code above does not work and returns an error that the file Microsoft.Office.Interop.Excel.dll cannot be found. But it will work if I specify the absolute path of the dll like this:
vbParameter.ReferencedAssemblies.Add("C:\Program Files (x86)\Microsoft Visual Studio 14.0\Visual Studio Tools for Office\PIA\Office15\Microsoft.Office.Interop.Excel.dll")
I cannot deploy the one with the absolute path to our end-users since they might have different versions of Microsoft Office installed and the location of their Excel interop assembly might be different as well. Is there any other way to reference this assembly or obtain the full path of this assembly to be referenced when deployed to another PC?