?
Solved

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

Posted on 2012-09-04
5
Medium Priority
?
3,277 Views
Last Modified: 2012-09-04
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
0
Comment
Question by:Murray Brown
  • 3
  • 2
5 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 38363687
try

Microsoft.Office.Interop.Excel.Application
0
 
LVL 35

Accepted Solution

by:
YZlat earned 2000 total points
ID: 38363744
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
 

Author Closing Comment

by:Murray Brown
ID: 38364049
Thanks very much. I need to blank out the login details
0
 

Author Comment

by:Murray Brown
ID: 38365374
Thanks very much
0
 
LVL 35

Expert Comment

by:YZlat
ID: 38365429
Glad I could help
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month15 days, 15 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question