Thanks Hefrried, that works great. Only one thing left to do :
How can I reference my object Label1 (created as Public WithEvents in
form1.vb) within the event handler xlSheet_Change ? When I try to reference
it, I get a message saying it is not declared. Obviously my scoping of the
variable must be out somehow.
e.g.
Module Module1
'================================================= =================
'Demonstrates Using WithEvents for Event Handling
'================================================= =================
Private xlApp As Excel.Application
Private xlBook As Excel.Workbook
Private WithEvents xlSheet As Excel.Worksheet
Public Sub LoadExcel()
'Start Excel and create a new workbook.
xlApp = CreateObject("Excel.Application")
xlBook = xlApp.Workbooks.Add()
xlBook.Windows(1).Caption = "Uses WithEvents"
'Get references to the worksheet.
xlSheet = xlBook.Worksheets.Item(1)
xlSheet.Activate()
'Make Excel visible and give the user control.
xlApp.Visible = True
xlApp.UserControl = True
End Sub
Private Sub xlSheet_Change(ByVal Target As Excel.Range) Handles
xlSheet.Change
label1.text = xlSheet.Cells(1, 1)
<---------------------------------------------------------------------------
---------------- This is where I get the message 'Label1 is not declared'
End Sub
End Module
Thanks,
Gordon.
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
* "Gordon" <gh*********@btconnect.com> scripsit: OK, let's put it another way - how can I set up an event handler for
when my cell in Excel changes ? Here's the code I have so far :
Maybe this article helps:
Microsoft Knowledge Base Article - 302814
HOW TO: Handle Events for Excel by Using Visual Basic .NET
<http://support.microsoft.com/default.aspx?scid=kb;en-us;302814>
--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>