Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

VB
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
            'the problem so far must be about the culture info of the system so i define to new parameters to deal with culture info.
            Dim newCulture As System.Globalization.CultureInfo  'To save new Culture info
            Dim OldCulture As System.Globalization.CultureInfo  'To hold the old Culture info

            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()
                'xlObject.AlertBeforeOverwriting = False
                'xlObject.DisplayAlerts = False

                ''This Adds a new woorkbook, you could open the workbook from file also
                xlWB = xlObject.Workbooks.Add()
                ' I customized this row...
                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."
                    'showmessage(a_sErrorMessage)
                ElseIf ex.ErrorCode = -2146827284 Then
                    a_sErrorMessage = "Error in export: Excel allows only 65,536 maximum rows in a sheet."
                    'showmessage(a_sErrorMessage)
                Else
                    a_sErrorMessage = (("Error in export: " & ex.Message) + Environment.NewLine & " Error: ") + ex.ErrorCode
                    'showmessage(a_sErrorMessage)
                End If
            Catch ex As Exception
                a_sErrorMessage = (("Error in export: " & ex.Message) + Environment.NewLine & " Error: ")
                'showmessage(a_sErrorMessage)
            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
                ' force final cleanup!
                GC.Collect()
                GC.WaitForPendingFinalizers()
            End Try
        Catch ex As Exception
            a_sErrorMessage = "Error in export: " & ex.Message
            'showmessage(a_sErrorMessage)
        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.
Posted
Updated 17-May-16 18:14pm
Comments
m.r.m.40 17-May-16 5:02am    
The problem was with adding extra references to the project, i removed all office dependencies and only added the excel reference and used the interop.Excel so it got solved.
Patrice T 17-May-16 19:01pm    
as you solved the question,
Put the comment in a solution ans accept the solution to close the question.
Use Accept answer to close the question.
m.r.m.40 18-May-16 0:13am    
OK!

1 solution

The problem was with adding extra references to the project, i removed all office dependencies and only added the excel reference and used the interop.Excel so it got solved.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900