G
Guest
i got this program that will fetch the data in the excel spreadsheet, it was
working before then i make some adjustment and it now give me an error of
"Object reference not set to an instance of an object error" i can't seem to
see what's wrong with my code so i'm hoping some fresh eyes can spot what's
wrong or what i'm missing. the following is the code, this line strFile =
CStr(xlSheet.Range("I" + CStr(Row)).Value.ToString + "\" + strfilename) gave
me the object reference not set error. it suppose to fetch the data from
spreadsheet and tell it what directory to copy the file from the I column of
the spreadsheet contain the directory on a server.
any help is much appericated
thank
Dim o As OpenFileDialog
Dim xlApp As Microsoft.Office.Interop.Excel.Application
Dim xlWB As Microsoft.Office.Interop.Excel.Workbook
Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim strName As String
Dim strAddress As String
Dim strType As String
Dim strFile As String
Dim strEnc As String
Dim strSubnet As String
Dim strTag As String
Dim strEventP As String
Dim strEventYN As String
Dim Row As Integer = 2
arlsName = New ArrayList
arlsAddress = New ArrayList
arlsType = New ArrayList
arlsFile = New ArrayList
arlsEnc = New ArrayList
arlsSubnet = New ArrayList
arlsTag = New ArrayList
arlsEventP = New ArrayList
arlsEventYN = New ArrayList
o = New OpenFileDialog
o.Title = "Open Excel spreadsheet"
o.InitialDirectory = "C:\"
o.FilterIndex = 2
o.RestoreDirectory = True
o.Filter = "All Files (*.xls)|*.xls"
If (o.ShowDialog = Windows.Forms.DialogResult.OK) Then
lblDisplay.Text = o.FileName
xlApp = CType(CreateObject("Excel.Application"),
Microsoft.Office.Interop.Excel.Application)
xlApp.Visible = False
xlApp.Workbooks.Open(o.FileName)
xlWB = xlApp.ActiveWorkbook
xlSheet = CType(xlWB.Sheets(2),
Microsoft.Office.Interop.Excel.Worksheet)
Dim strfilename As String = "logicaldevice.xml"
Try
Do
strName = CStr(xlSheet.Range("A" + CStr(Row)).Value)
strAddress = CStr(xlSheet.Range("C" + CStr(Row)).Value)
strType = CStr(xlSheet.Range("F" + CStr(Row)).Value)
strFile = CStr(xlSheet.Range("I" +
CStr(Row)).Value.ToString + "\" + strfilename)
strEnc = CStr(xlSheet.Range("E" + CStr(Row)).Value)
strSubnet = CStr(xlSheet.Range("D" + CStr(Row)).Value)
strTag = CStr(xlSheet.Range("H" + CStr(Row)).Value)
strEventP = CStr(xlSheet.Range("L" + CStr(Row)).Value)
strEventYN = CStr(xlSheet.Range("K" + CStr(Row)).Value)
If strName IsNot Nothing Then
arlsName.Add(strName)
arlsAddress.Add(strAddress)
arlsType.Add(strType)
arlsFile.Add(strFile)
arlsEnc.Add(strEnc)
arlsSubnet.Add(strSubnet)
arlsTag.Add(strTag)
arlsEventP.Add(strEventP)
arlsEventYN.Add(strEventYN)
End If
Row += 1
Loop Until strName = ""
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
xlWB.Close()
xlApp.Quit()
ElseIf (o.ShowDialog = Windows.Forms.DialogResult.Cancel) Then
MsgBox("You did not select any xls file, the program will now
close")
Application.Exit()
End If
working before then i make some adjustment and it now give me an error of
"Object reference not set to an instance of an object error" i can't seem to
see what's wrong with my code so i'm hoping some fresh eyes can spot what's
wrong or what i'm missing. the following is the code, this line strFile =
CStr(xlSheet.Range("I" + CStr(Row)).Value.ToString + "\" + strfilename) gave
me the object reference not set error. it suppose to fetch the data from
spreadsheet and tell it what directory to copy the file from the I column of
the spreadsheet contain the directory on a server.
any help is much appericated
thank
Dim o As OpenFileDialog
Dim xlApp As Microsoft.Office.Interop.Excel.Application
Dim xlWB As Microsoft.Office.Interop.Excel.Workbook
Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim strName As String
Dim strAddress As String
Dim strType As String
Dim strFile As String
Dim strEnc As String
Dim strSubnet As String
Dim strTag As String
Dim strEventP As String
Dim strEventYN As String
Dim Row As Integer = 2
arlsName = New ArrayList
arlsAddress = New ArrayList
arlsType = New ArrayList
arlsFile = New ArrayList
arlsEnc = New ArrayList
arlsSubnet = New ArrayList
arlsTag = New ArrayList
arlsEventP = New ArrayList
arlsEventYN = New ArrayList
o = New OpenFileDialog
o.Title = "Open Excel spreadsheet"
o.InitialDirectory = "C:\"
o.FilterIndex = 2
o.RestoreDirectory = True
o.Filter = "All Files (*.xls)|*.xls"
If (o.ShowDialog = Windows.Forms.DialogResult.OK) Then
lblDisplay.Text = o.FileName
xlApp = CType(CreateObject("Excel.Application"),
Microsoft.Office.Interop.Excel.Application)
xlApp.Visible = False
xlApp.Workbooks.Open(o.FileName)
xlWB = xlApp.ActiveWorkbook
xlSheet = CType(xlWB.Sheets(2),
Microsoft.Office.Interop.Excel.Worksheet)
Dim strfilename As String = "logicaldevice.xml"
Try
Do
strName = CStr(xlSheet.Range("A" + CStr(Row)).Value)
strAddress = CStr(xlSheet.Range("C" + CStr(Row)).Value)
strType = CStr(xlSheet.Range("F" + CStr(Row)).Value)
strFile = CStr(xlSheet.Range("I" +
CStr(Row)).Value.ToString + "\" + strfilename)
strEnc = CStr(xlSheet.Range("E" + CStr(Row)).Value)
strSubnet = CStr(xlSheet.Range("D" + CStr(Row)).Value)
strTag = CStr(xlSheet.Range("H" + CStr(Row)).Value)
strEventP = CStr(xlSheet.Range("L" + CStr(Row)).Value)
strEventYN = CStr(xlSheet.Range("K" + CStr(Row)).Value)
If strName IsNot Nothing Then
arlsName.Add(strName)
arlsAddress.Add(strAddress)
arlsType.Add(strType)
arlsFile.Add(strFile)
arlsEnc.Add(strEnc)
arlsSubnet.Add(strSubnet)
arlsTag.Add(strTag)
arlsEventP.Add(strEventP)
arlsEventYN.Add(strEventYN)
End If
Row += 1
Loop Until strName = ""
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
xlWB.Close()
xlApp.Quit()
ElseIf (o.ShowDialog = Windows.Forms.DialogResult.Cancel) Then
MsgBox("You did not select any xls file, the program will now
close")
Application.Exit()
End If