I'm using visual studio 2010 (VB.net technology) and office 2010 I'm trying to use excel functionalities in vb.net I face the error below:
MsoAppLanguageID is ambiguous in the namespace Microsoft.Office.Core.
I add two following references: Microsoft Excel 14.0 Object Library Microsoft
Office 14.0 Object Library
And import the following code:
Imports System.Runtime.InteropServices
Here is the following function code:
Public Function ExportToExcel(ByVal a_sFilename As String, ByVal a_sData As DataTable, ByVal a_sFileTitle As String, ByRef a_sErrorMessage As String) As Boolean
a_sErrorMessage = String.Empty
Dim bRetVal As Boolean = False
Dim dsDataSet As DataTable = Nothing
Try
dsDataSet = a_sData
Dim xlObject As Microsoft.Office.Interop.Excel.Application = Nothing
Dim xlWB As Microsoft.Office.Interop.Excel.Workbook = Nothing
Dim xlSh As Microsoft.Office.Interop.Excel.Worksheet = Nothing
Dim rg As Microsoft.Office.Interop.Excel.Range = Nothing
Dim newCulture As System.Globalization.CultureInfo
Dim OldCulture As System.Globalization.CultureInfo
OldCulture = System.Threading.Thread.CurrentThread.CurrentCulture
newCulture = New System.Globalization.CultureInfo( _
xlObject.LanguageSettings.LanguageID(Microsoft.Office.Core.MsoAppLanguageID.msoLanguageIDUI))
System.Threading.Thread.CurrentThread.CurrentCulture = newCulture
Try
xlObject = New Microsoft.Office.Interop.Excel.Application()
xlWB = xlObject.Workbooks.Add()
xlWB.SaveAs(a_sFilename, _
56, _
"123456", _
"654321", _
True, _
True, _
1, _
1, _
True, _
True)
xlSh = DirectCast(xlObject.ActiveWorkbook.ActiveSheet, Microsoft.Office.Interop.Excel.Worksheet)
For j As Integer = 0 To dsDataSet.Columns.Count - 1
xlSh.Cells(1, j + 1) = _
dsDataSet.Columns(j).ToString()
xlSh.Cells(1, j + 1).Font.Bold = True
Next
For i As Integer = 1 To dsDataSet.Rows.Count
For j As Integer = 0 To dsDataSet.Columns.Count - 1
xlSh.Cells(i + 1, j + 1) = _
dsDataSet.Rows(i - 1)(j).ToString()
Next
Next
xlSh.Columns.AutoFit()
If String.IsNullOrEmpty(a_sFileTitle) Then
xlObject.Caption = "untitled"
Else
xlObject.Caption = a_sFileTitle
End If
xlWB.Save()
bRetVal = True
Catch ex As System.Runtime.InteropServices.COMException
If ex.ErrorCode = -2147221164 Then
a_sErrorMessage = "Error in export: Please install Microsoft Office (Excel) to use the Export to Excel feature."
ElseIf ex.ErrorCode = -2146827284 Then
a_sErrorMessage = "Error in export: Excel allows only 65,536 maximum rows in a sheet."
Else
a_sErrorMessage = (("Error in export: " & ex.Message) + Environment.NewLine & " Error: ") + ex.ErrorCode
End If
Catch ex As Exception
a_sErrorMessage = (("Error in export: " & ex.Message) + Environment.NewLine & " Error: ")
Finally
Try
If xlWB IsNot Nothing Then
xlWB.Close(Nothing, Nothing, Nothing)
End If
xlObject.Workbooks.Close()
xlObject.Quit()
If rg IsNot Nothing Then
Marshal.ReleaseComObject(rg)
End If
If xlSh IsNot Nothing Then
Marshal.ReleaseComObject(xlSh)
End If
If xlWB IsNot Nothing Then
Marshal.ReleaseComObject(xlWB)
End If
If xlObject IsNot Nothing Then
Marshal.ReleaseComObject(xlObject)
End If
Catch
End Try
xlSh = Nothing
xlWB = Nothing
xlObject = Nothing
GC.Collect()
GC.WaitForPendingFinalizers()
End Try
Catch ex As Exception
a_sErrorMessage = "Error in export: " & ex.Message
End Try
Return bRetVal
End Function
Private Sub showmessage(ByVal a_sErrorMessage As String)
MyMessageBox.showMsg(MyMessageBox.typeMsag.ErrorMsag, "", a_sErrorMessage)
End Sub
I can't find a way to solve the problem, Any help or advise would be helpful,
Thanks,
What I have tried:
removing and adding the references, and re-importing, but the error still remains.