Solved

Using VB.net  2010 to read/write to existing or new Excel 2007 worksheet

Posted on 2011-09-23
3
629 Views
Last Modified: 2013-11-26
All,
I'm about ready to go back to VB6.

I need to develop a program that simply reads and writes to an excel 2007 file. I can do it in VB6 and to Excel 2003, but not 2007. I'm using tne Microsoft Excel 12.0 library.

I'm getting "Interop type '_ExcelApplication Class' cannot be embedded. Use the applicable interface instead." (Line 10)

Although far from being an "expert", I can handle VB6 fairly well. VB.net is kicking my "#@#".
Thanks for the help,

EBSI (W. Bailey)


_
Imports Microsoft.Office.Interop

Public Class Form1
    'http://www.visual-basic-tutorials.com/WriteToExcel2007.php
    Dim APP As Excel.Application
    Dim worksheet As Excel.Worksheet
    Dim workbook As Excel.Workbook

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        APP = New Excel._ExcelApplicationClass
        workbook = APP.Workbooks.Open("C:\MBS_Jobs_Weld\jvtest\Test.xls")
        worksheet = workbook.Worksheets("sheet1")

    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Myfile As String
        Myfile = Dir("C:\MBS_Jobs_Weld\jvtest\Test.xls")
        Stop
        TextBox1.Text = worksheet.Cells(1, 1).Value
        TextBox2.Text = worksheet.Cells(1, 2).Value
    End Sub

    Private Sub Form1_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        workbook.Save()
        workbook.Close()
        APP.Quit()
    End Sub



End Class

Open in new window

0
Comment
Question by:EBSI
  • 2
3 Comments
 

Author Comment

by:EBSI
Comment Utility
All,

I got the program to compile, but now get this error.

Thank you,

EBSI
ScreenShot.jpg
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
Comment Utility
1) I would use the ACE driver, if you aren't, in the connection string.

2) You can use OLE DB to read the Excel

3) You can also use Open XML library to read Excel.

If you are saying that you want to go back to VB6, then I would say that you are in desparate need of some small successes, to bolster your confidence in something completely new, because we would hate to see you go over the one thing that .NET has a problem with (COM interop).
0
 

Author Closing Comment

by:EBSI
Comment Utility
Although not specific, it did help.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

728 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now