View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Jake Marx[_3_] Jake Marx[_3_] is offline
external usenet poster
 
Posts: 860
Default Capture Excel Events from other application

Hi George,

As Rob said, this is definitely possible.

Create a class module named "CXLWBSink" and add the following code to it:

Public WithEvents xlWB As Excel.Workbook

Private Sub xlWB_SheetChange(ByVal Sh As Object, _
ByVal Target As Excel.Range)
Target.Cells.Interior.ColorIndex = 3
End Sub

Now, create a standard module and add the following code to it:

Dim CxlwbSink As CxlwbSink

Sub StartDemo()
Dim xlApp As Excel.Application

Set CxlwbSink = New CxlwbSink

Set xlApp = New Excel.Application

Set CxlwbSink.xlWB = xlApp.Workbooks.Open("C:\test.xls")
xlApp.UserControl = True
xlApp.Visible = True

Set xlApp = Nothing
End Sub

Sub EndDemo()
Dim xlApp As Excel.Application

On Error Resume Next
If Not CxlwbSink.xlWB Is Nothing Then
Set xlApp = CxlwbSink.xlWB.Application
CxlwbSink.xlWB.Close False
xlApp.Quit
Set xlApp = Nothing
End If
Set CxlwbSink = Nothing
End Sub


Now, if you run StartDemo, you should get a new Excel app with test.xls open
(assuming that's a valid file path). If you change any cell in that
workbook, the cell fill color should change. When you're done, run EndDemo
to kill the instance of Excel.

This is totally off the cuff, so I'm sure you'll need to add some error
handling and tighten the code up, but I just wanted to show you that it does
work.

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]


George wrote:
Who know how to capture excel event for example 'Workbook_BeforeClose'
from other application for example MSWord. Using WithEvents in MSWord
class doesn't work, so I guess it impossible to do that.