ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   events across vba projects (https://www.excelbanter.com/excel-programming/358448-events-across-vba-projects.html)

Eric Fingerhut[_3_]

events across vba projects
 
Is this possible?

I have a core library (vba project, "LibA") that I want to be ignorant of
any consumers. If I set up a separate project ("LibB") that has a reference
set to "LibA", there are times when I may like to make a piece of
information available from LiibB to LibA.

I thought the natural solution would be to set up an event that broadcasts
the need for information in LibA, with and event handler in LibB to provide
it. Doing so compiles, but doesn't seem to work.

TIA,
Eric

Chip Pearson

events across vba projects
 
Are you talking about receiving standard Excel events (e.g.,
Change, SelectionChange) from another workbook? If so, you need
to use Application level events. See
http://www.cpearson.com/excel/AppEvent.htm for details and
example code.

Or are you talking about custom, user defined events, declared as
Event and triggered by RaiseEvent?

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"Eric Fingerhut" wrote
in message
...
Is this possible?

I have a core library (vba project, "LibA") that I want to be
ignorant of
any consumers. If I set up a separate project ("LibB") that has
a reference
set to "LibA", there are times when I may like to make a piece
of
information available from LiibB to LibA.

I thought the natural solution would be to set up an event that
broadcasts
the need for information in LibA, with and event handler in
LibB to provide
it. Doing so compiles, but doesn't seem to work.

TIA,
Eric




Eric Fingerhut[_3_]

events across vba projects
 
Thanks for the reply. I'm talking about Custom events, and I must apologize
for accidentally posting this twice. Sample code below:

//////////////////////////////////////////////////////////
Class CMsgEx is a class in LibA with the following custom event raised in a
sample method below:
/////////////////////////////////////////////////////////

' post that we are looking for an application title
Public Event applicationTitleRequest(MsgEx As CMsgEx)

Public Function displayInformation(ByVal aMsg As String) As VbMsgBoxResult
RaiseEvent applicationTitleRequest(Me)
icon = Information
Caption = aMsg
Title = aTitle
displayInformation = show
End Function

/////////////////////////////////////////////////
Class CAppFacade is in LibB, and LibB has a refernce to Lib A
////////////////////////////////////////////////

Public WithEvents m_oMsgEx As LibA.CMsgEx

Private Sub m_oMsgEx_applicationTitleRequest(MsgEx As LibA.CMsgEx)
MsgEx.Title = Me.applicationName
End Sub


"Chip Pearson" wrote:

Are you talking about receiving standard Excel events (e.g.,
Change, SelectionChange) from another workbook? If so, you need
to use Application level events. See
http://www.cpearson.com/excel/AppEvent.htm for details and
example code.

Or are you talking about custom, user defined events, declared as
Event and triggered by RaiseEvent?

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"Eric Fingerhut" wrote
in message
...
Is this possible?

I have a core library (vba project, "LibA") that I want to be
ignorant of
any consumers. If I set up a separate project ("LibB") that has
a reference
set to "LibA", there are times when I may like to make a piece
of
information available from LiibB to LibA.

I thought the natural solution would be to set up an event that
broadcasts
the need for information in LibA, with and event handler in
LibB to provide
it. Doing so compiles, but doesn't seem to work.

TIA,
Eric





Tim Williams

events across vba projects
 
Eric,
(not sure which of your two threads we're in, so posting to both...)

The code below worked for me (at least it did what I expected, but I realize
these two are not always the same thing).
To simplify I created two simple classes in the same project, clsA and clsB.
clsB contains an instance of clsA.

I had to cheat a bit and expose the clsA instance of clsB via a property so
I could trigger the clsA event....

Cheers
Tim


'#### test code
Sub Tester()

Dim oB As clsB
Set oB = New clsB
oB.SubClassA.DisplayConsumerInformation

End Sub

'############### clsA code
Option Explicit

Private m_title As String

Public Event appTitleRequest(ByRef MsgEx As clsA)

Public Sub DisplayConsumerInformation()
Me.Title=""
RaiseEvent appTitleRequest(Me)
MsgBox Me.Title
End Sub

Property Let Title(sTitle As String)
m_title = sTitle
End Property

Property Get Title() As String
Title = m_title
End Property

'##################### clsB code
Option Explicit

Private m_title As String

Public Event appTitleRequest(ByRef MsgEx As clsA)

Public Sub DisplayConsumerInformation()
RaiseEvent appTitleRequest(Me)
MsgBox Me.Title
End Sub

Property Let Title(sTitle As String)
m_title = sTitle
End Property

Property Get Title() As String
Title = m_title
End Property



"Eric Fingerhut" wrote in message
...
Thanks for the reply. I'm talking about Custom events, and I must
apologize
for accidentally posting this twice. Sample code below:

//////////////////////////////////////////////////////////
Class CMsgEx is a class in LibA with the following custom event raised in
a
sample method below:
/////////////////////////////////////////////////////////

' post that we are looking for an application title
Public Event applicationTitleRequest(MsgEx As CMsgEx)

Public Function displayInformation(ByVal aMsg As String) As VbMsgBoxResult
RaiseEvent applicationTitleRequest(Me)
icon = Information
Caption = aMsg
Title = aTitle
displayInformation = show
End Function

/////////////////////////////////////////////////
Class CAppFacade is in LibB, and LibB has a refernce to Lib A
////////////////////////////////////////////////

Public WithEvents m_oMsgEx As LibA.CMsgEx

Private Sub m_oMsgEx_applicationTitleRequest(MsgEx As LibA.CMsgEx)
MsgEx.Title = Me.applicationName
End Sub


"Chip Pearson" wrote:

Are you talking about receiving standard Excel events (e.g.,
Change, SelectionChange) from another workbook? If so, you need
to use Application level events. See
http://www.cpearson.com/excel/AppEvent.htm for details and
example code.

Or are you talking about custom, user defined events, declared as
Event and triggered by RaiseEvent?

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"Eric Fingerhut" wrote
in message
...
Is this possible?

I have a core library (vba project, "LibA") that I want to be
ignorant of
any consumers. If I set up a separate project ("LibB") that has
a reference
set to "LibA", there are times when I may like to make a piece
of
information available from LiibB to LibA.

I thought the natural solution would be to set up an event that
broadcasts
the need for information in LibA, with and event handler in
LibB to provide
it. Doing so compiles, but doesn't seem to work.

TIA,
Eric







Tim Williams[_4_]

events across vba projects
 
Sorry - paste error. The clsB code is wrong. Will repost when I get
home...

Tim


Tim Williams

events across vba projects
 
OK - this is what I used...

'#### test code
Sub Tester()

Dim oB As clsB
Set oB = New clsB
oB.SubClassA.DisplayConsumerInformation

End Sub

'############### clsA code
Option Explicit

Private m_title As String

Public Event appTitleRequest(ByRef MsgEx As clsA)

Public Sub DisplayConsumerInformation()
Me.Title=""
RaiseEvent appTitleRequest(Me)
MsgBox Me.Title
End Sub

Property Let Title(sTitle As String)
m_title = sTitle
End Property

Property Get Title() As String
Title = m_title
End Property

'##################### clsB code
Public WithEvents m_oMsgEx As clsA

Private Sub Class_Initialize()
Set m_oMsgEx = New clsA
End Sub

Private Sub m_oMsgEx_AppTitleRequest(MsgEx As clsA)
MsgEx.Title = Me.AppName
End Sub

Property Get AppName() As String
AppName = "Some App Name"
End Property

Property Get SubClassA() As clsA
Set SubClassA = m_oMsgEx
End Property



"Tim Williams" wrote in message
oups.com...
Sorry - paste error. The clsB code is wrong. Will repost when I get
home...

Tim




Eric Fingerhut[_3_]

events across vba projects
 
Thanks Tim; your code looks perfect, but doesn't work across when the classes
are in two vba projects. When the following below code in classB, which is in
efBasicAppFrameworkLibrary that holds a reference to efBasicVbaLibrary, it
gets a runtime error on New efBasicVbaLibrary.CMsgEx:

Private Sub Class_Initialize()
Set m_oMsgEx = New efBasicVbaLibrary.CMsgEx
End Sub

Thanks,
Eric

"Tim Williams" wrote:

OK - this is what I used...

'#### test code
Sub Tester()

Dim oB As clsB
Set oB = New clsB
oB.SubClassA.DisplayConsumerInformation

End Sub

'############### clsA code
Option Explicit

Private m_title As String

Public Event appTitleRequest(ByRef MsgEx As clsA)

Public Sub DisplayConsumerInformation()
Me.Title=""
RaiseEvent appTitleRequest(Me)
MsgBox Me.Title
End Sub

Property Let Title(sTitle As String)
m_title = sTitle
End Property

Property Get Title() As String
Title = m_title
End Property

'##################### clsB code
Public WithEvents m_oMsgEx As clsA

Private Sub Class_Initialize()
Set m_oMsgEx = New clsA
End Sub

Private Sub m_oMsgEx_AppTitleRequest(MsgEx As clsA)
MsgEx.Title = Me.AppName
End Sub

Property Get AppName() As String
AppName = "Some App Name"
End Property

Property Get SubClassA() As clsA
Set SubClassA = m_oMsgEx
End Property



"Tim Williams" wrote in message
oups.com...
Sorry - paste error. The clsB code is wrong. Will repost when I get
home...

Tim






All times are GMT +1. The time now is 07:26 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com