VB.net Interop Type "ApplicationClass" cannot be embedded. Use the application interface insteads

Hi

I am getting the following error in the code below on the line marked XXXX

Interop Type "ApplicationClass" cannot be embedded. Use the application interface insteads


    Sub SQL_to_Excel()


        ' Create sql connection string

        'Dim conString As String = "Data Source =  XXXX ; Initial Catalog = XXXX; User Id = XXXX; Password = XXXX;"
        Dim conString As String = "Server=xx.xx.xx.xx;Database=xxx;Uid=xxxUser;Pwd=xxx"
        Dim sqlCon As New SqlConnection(conString)

        sqlCon.Open()



        Dim da As New SqlDataAdapter("select * from tblTest", sqlCon)

        Dim dtMainSQLData As New System.Data.DataTable()

        da.Fill(dtMainSQLData)

        Dim dcCollection As DataColumnCollection = dtMainSQLData.Columns

        ' Export Data into EXCEL Sheet

        Dim ExcelApp As New Microsoft.Office.Interop.Excel.ApplicationClass() 'XXXX

        ExcelApp.Application.Workbooks.Add(Type.Missing)

        ' ExcelApp.Cells.CopyFromRecordset(objRS);

        For i As Integer = 1 To dtMainSQLData.Rows.Count


            For j As Integer = 1 To dtMainSQLData.Columns.Count


                If i = 1 Then

                    ExcelApp.Cells(i, j) = dcCollection(j - 1).ToString()
                Else


                    ExcelApp.Cells(i, j) = dtMainSQLData.Rows(i - 1)(j - 1).ToString()

                End If

            Next
        Next

        ExcelApp.ActiveWorkbook.SaveCopyAs("C:\Users\Sachindra\Desktop\test.xls")

        ExcelApp.ActiveWorkbook.Saved = True

        ExcelApp.Quit()

        MsgBox("Data Exported Successfully into Excel File")


    End Sub
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?
 
YZlatConnect With a Mentor Commented:
first use the following import:
Imports Excel = Microsoft.Office.Interop.Excel

Open in new window



then try:

Sub SQL_to_Excel()


        ' Create sql connection string 

        'Dim conString As String = "Data Source =  XXXX ; Initial Catalog = XXXX; User Id = XXXX; Password = XXXX;"
        Dim conString As String = "Server=xx.xx.xx.xx;Database=xxx;Uid=xxx;Pwd=xxx"
        Dim sqlCon As New SqlConnection(conString)

        sqlCon.Open()



        Dim da As New SqlDataAdapter("select * from tblTest", sqlCon)

        Dim dtMainSQLData As New System.Data.DataTable()

        da.Fill(dtMainSQLData)

        Dim dcCollection As DataColumnCollection = dtMainSQLData.Columns

        ' Export Data into EXCEL Sheet 

        Dim ExcelApp As New Excel.Application 'XXXX

        ExcelApp.Application.Workbooks.Add(Type.Missing)

        ' ExcelApp.Cells.CopyFromRecordset(objRS);

        For i As Integer = 1 To dtMainSQLData.Rows.Count


            For j As Integer = 1 To dtMainSQLData.Columns.Count


                If i = 1 Then

                    ExcelApp.Cells(i, j) = dcCollection(j - 1).ToString()
                Else


                    ExcelApp.Cells(i, j) = dtMainSQLData.Rows(i - 1)(j - 1).ToString()

                End If

            Next
        Next

        ExcelApp.ActiveWorkbook.SaveCopyAs("C:\Users\Sachindra\Desktop\test.xls")

        ExcelApp.ActiveWorkbook.Saved = True

        ExcelApp.Quit()

        MsgBox("Data Exported Successfully into Excel File")


    End Sub

Open in new window

0
 
YZlatCommented:
try

Microsoft.Office.Interop.Excel.Application
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks very much. I need to blank out the login details
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks very much
0
 
YZlatCommented:
Glad I could help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.