K
Karen Hagerman
I posted earlier and it hasn't shown up yet, so sorry if this ends up being a repost.
I am trying to use WithEvents to enable the following actions.
From an Access form, Excel is opened to a pre-formatted Excel file.
The User then types in some values in the Excel sheet.
Once the Excel sheet/app is closed and saved, I want Access to completely close the Excel instance.
I have tried to understand and implement the WithEvents code described in a few places in MSDN. To see if I was successful, I have included a msgbox in the appropriate code but I never see the message box so I do not believe I have the code correct. My code is:
In a class labeled XLEvents
Option Compare Database
Private WithEvents xlApp As Excel.Application
Private Sub Class_Initialize()
Set xlApp = Excel.Application
End Sub
Private Sub xlApp_WindowResize(ByVal Wb As Excel.Workbook, ByVal Wn As Excel.Window)
MsgBox ("in the class")
End Sub
In a module labeled basXLEvents
Option Compare Database
Global gXLEvents As clsXlEvents
Sub InitXLEvents()
Set gXLEvents = New clsXlEvents
End Sub
In my Access form module I have
Private Sub Command5_Click()
On Error GoTo Err_Command5_Click
Dim xlApp As Excel.Application 'Variables for Excel Object
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Const xlFile = "F:\Documents and Settings\Karen Hagerman\......"
Const xlSheetName = "Sheet1"
(OTHER CODE WHICH SETS UP VARIABLES TO PUT IN THE EXCEL SHEET)
'Create the Excel Object
Set xlApp = CreateObject("Excel.Application")
With xlApp
.Visible = True
Set xlBook = .Workbooks.Open(xlFile)
On Error Resume Next
Set xlSheet = xlBook.Worksheets(xlSheetName)
(MORE CODE THAT MANIPULATES THE EXCEL SHEET)
Exit_Command5_Click:
Exit Sub
Err_Command5_Click:
MsgBox Err.Description
Resume Exit_Command5_Click
End Sub
Private Sub xlApp_WindowDeactivate()
MsgBox ("in the form")
End Sub
Private Sub xlApp_WindowResize()
MsgBox ("intheform")
End Sub
If my code was working, I'd expect to see the message boxes when I resize or close the Excel window, neither happens. Any suggestions on what I'm doing wrong would certainly help. There must be a few real experts with WithEvents in this group.
Karen
I am trying to use WithEvents to enable the following actions.
From an Access form, Excel is opened to a pre-formatted Excel file.
The User then types in some values in the Excel sheet.
Once the Excel sheet/app is closed and saved, I want Access to completely close the Excel instance.
I have tried to understand and implement the WithEvents code described in a few places in MSDN. To see if I was successful, I have included a msgbox in the appropriate code but I never see the message box so I do not believe I have the code correct. My code is:
In a class labeled XLEvents
Option Compare Database
Private WithEvents xlApp As Excel.Application
Private Sub Class_Initialize()
Set xlApp = Excel.Application
End Sub
Private Sub xlApp_WindowResize(ByVal Wb As Excel.Workbook, ByVal Wn As Excel.Window)
MsgBox ("in the class")
End Sub
In a module labeled basXLEvents
Option Compare Database
Global gXLEvents As clsXlEvents
Sub InitXLEvents()
Set gXLEvents = New clsXlEvents
End Sub
In my Access form module I have
Private Sub Command5_Click()
On Error GoTo Err_Command5_Click
Dim xlApp As Excel.Application 'Variables for Excel Object
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Const xlFile = "F:\Documents and Settings\Karen Hagerman\......"
Const xlSheetName = "Sheet1"
(OTHER CODE WHICH SETS UP VARIABLES TO PUT IN THE EXCEL SHEET)
'Create the Excel Object
Set xlApp = CreateObject("Excel.Application")
With xlApp
.Visible = True
Set xlBook = .Workbooks.Open(xlFile)
On Error Resume Next
Set xlSheet = xlBook.Worksheets(xlSheetName)
(MORE CODE THAT MANIPULATES THE EXCEL SHEET)
Exit_Command5_Click:
Exit Sub
Err_Command5_Click:
MsgBox Err.Description
Resume Exit_Command5_Click
End Sub
Private Sub xlApp_WindowDeactivate()
MsgBox ("in the form")
End Sub
Private Sub xlApp_WindowResize()
MsgBox ("intheform")
End Sub
If my code was working, I'd expect to see the message boxes when I resize or close the Excel window, neither happens. Any suggestions on what I'm doing wrong would certainly help. There must be a few real experts with WithEvents in this group.
Karen