Use Class in VB.NET to ReadWrite a cell in Excel

  • Thread starter Thread starter JayWangTPE
  • Start date Start date
J

JayWangTPE

Guest
First, another code opens a Excel file. Next, the following code Read/Write a cell in the already-open Excel. But one exception occurs. How to fix it? Thanks!


System.NullReferenceException: 'Object reference not set to an instance of an object.'

at refloc.wks = xlWorkBook.ActiveSheet




Dim refloc As Miscreflocation08142019

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

'xlWorkBook = xlApp.ActiveWorkbook
xlWorkBook.ActiveSheet.Range("C1").Value = "TEXT"
refloc.wks = xlWorkBook.ActiveSheet
TextBox2.Text = refloc.Firstdataloc
End Sub


Class Module--

Imports Microsoft.Office.Interop
Public Class Miscreflocation08142019
Dim xlApp As Excel.Application
Dim xlWorkBooks As Excel.Workbooks = Nothing
Dim xlWorkBook As Excel.Workbook = Nothing
Public wks As Excel.Worksheet = Nothing
Dim data As String
Public Property Firstdataloc() As Integer
Get
data = wks.Range("C1").Value
Return data
End Get
Set(ByVal value As Integer)
wks.Range("C1").Value = value
End Set
End Property

End Class

Continue reading...
 
Back
Top