Click here to Skip to main content
15,894,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Imports Microsoft.Office.Interop.Excel
Imports System.Text
Imports System.Reflection
Imports System.Data
Imports Excel = Microsoft.Office.Interop.Excel

Public Class Form1
    Private Const connstring As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=C:\Test.xls;Extended Properties=""Excel 8.0;HDR=YES;"""
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Dim objexcel As Microsoft.Office.Interop.Excel.Application
  
        ' Dim wb As Microsoft.Office.Interop.Excel.Workbook
        ' Dim ws As Microsoft.Office.Interop.Excel.Worksheet
        '  Dim mMsgResponse As Integer
        Dim objExcel As New Excel.Application
        Dim objBook As Excel.Workbook = objExcel.Workbooks.Open("C:\Users\Server\Downloads\DONEsb.xls")
        Dim objSheet As Excel.Worksheet
        Dim bolFlag As Boolean = True
        Dim excelRow As Integer = 7
        Dim excelCol As Integer = 1
        Dim strCell1 As String
        Dim strCell2 As String
        Dim strCell3 As String
        Dim strCell4 As String
        Dim strCell5 As String
        Dim strCell6 As String
        Dim dialog As New OpenFileDialog()

        ' If DialogResult.OK = dialog.ShowDialog Then
        'TextBox1.Text = dialog.FileName
        ' objExcel = New Microsoft.Office.Interop.Excel.Application
        ' objBook = objExcel.Workbooks.Open(TextBox1.Text)


        If Convert.ToString(objSheet.Cells(excelRow, 1).value) = "" Then
            bolFlag = False
            strCell1 = CType(objSheet.Cells(excelRow, 1).value, String)
            strCell2 = CType(objSheet.Cells(excelRow, 2).value, String)
            strCell3 = CType(objSheet.Cells(excelRow, 3).value, String)
            strCell4 = CType(objSheet.Cells(excelRow, 4).value, String)
            strCell5 = CType(objSheet.Cells(excelRow, 5).value, String)
            strCell6 = CType(objSheet.Cells(excelRow, 6).value, String)

            excelRow += 1
            strCell1 = ""
            strCell2 = ""
            strCell3 = ""
            strCell4 = ""
            strCell5 = ""
            strCell6 = ""


            objExcel.Visible = True

            objBook.Activate()


            MsgBox("")

        End If
        ' End If


        '   Catch ex As COMException

        '   MessageBox.Show("Error accessing Excel: " + ex.ToString())

    End Sub



End Class
Posted
Updated 5-Jan-15 22:26pm
v2
Comments
ArunRajendra 6-Jan-15 4:23am    
In which part of the code are you getting the error?
Member 11316594 6-Jan-15 4:54am    
If Convert.ToString(objSheet.Cells(excelRow, 1).value) = "" Then
CHill60 6-Jan-15 4:27am    
objSheet has not been initialised
Member 11316594 6-Jan-15 4:55am    
how to initialiase...
CHill60 6-Jan-15 4:56am    
See solution 2 below

You have not set any value for objSheet. You can use your workbook object and use the WorkSheets in that object to assign a value to objWorkSheet.
 
Share this answer
 
Initialize your objSheet as below:
C#
Dim objSheet = objBook.Worksheets[1] as Microsoft.Office.Interop.Excel.Worksheet;
 
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