Search code examples
vb.netvisual-studiooffice-interop

Form not popping up upon running


Here I have my vb6 code that plays around with an excel file. (I took this code off some other website.) This code compiles and builds but when I hit f5 it does the form does not po up. If I remove all the code in the Class then it pops up.

How would I fix this?
Sorry I am new at vb6, but not coding in general.

Imports Excel = Microsoft.Office.Interop.Excel

Public Class Form1

    Dim xl As New Excel.Application
    Dim xlsheet As Excel.Worksheet
    Dim xlwbook As Excel.Workbook

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'the benifit of placing numbers in (row, col) is that you
        'can loop through different directions if required. I could
        'have used column names like "A1" 'etc. 

        'Text1.Text = xlsheet.Cells(2, 1) ' row 2 col 1
        'Text2.Text = xlsheet.Cells(2, 2) ' row 2 col 2 

        'don't forget to do this or you'll not be able to open
        'book1.xls again, untill you restart you pc.
        xl.ActiveWorkbook.Close(False, "f:\a.xls")
        xl.Quit()
    End Sub

    Private Sub Form_Load()
        xlwbook = xl.Workbooks.Open("f:\a.xls")
        xlsheet = xlwbook.Sheets.Item(1)
    End Sub

    Private Sub Form_Unload(Cancel As Integer)
        xlwbook = Nothing
        xl = Nothing
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        xlsheet.Cells(2, 1) = "adsfasdfasdf"
        xlsheet.Cells(2, 2) = "qwerqwer"
        xlwbook.Save()

        'don't forget to do this or you'll not be able to open
        'book1.xls again, untill you restart you pc.
        xl.ActiveWorkbook.Close(False, "f:\a.xls")
        xl.Quit()
    End Sub

End Class

Solution

  • Then it means that you are not adding reference to your project

    follow the link to code and further details..

    http://vb.net-informations.com/excel-2007/vb.net_excel_2007_open_file.htm