PC Review


Reply
Thread Tools Rate Thread

Comparing Object References

 
 
=?Utf-8?B?SU5UUDU2?=
Guest
Posts: n/a
 
      24th Jul 2007
Is there a way in VBA to compare Object References?

Consider the following

Option Explicit
Dim WBArray(1 To 1) As Workbook

Public Sub assign()
Set WBArray(1) = ActiveWorkbook
End Sub

Public Sub test()
If WBArray(1) = ActiveWorkbook Then
MsgBox "Yes"
Else
MsgBox "No"
End If
End Sub

Functionally, I want to make an AddIn with a class module that captures
Application events, but I only want to execute the code for those events on
workbooks that the user has told me are part of my application. So, I was
considering having the user tell me which workbooks are "mine" and in the
event handlers, the first thing I do is check to see if the event causing
workbook is in the list. I was hoping to not have to manage names or other
user definable properties.

Above, in test(), I functionally want to compare the object reference in the
array against the object reference for ActiveWorkbook to see if they refer to
the same object.

Is this something I can do in VBA? (Excel 2003) I could capture the
workbook.name property, and track changes to the name, but I was hoping to
avoid that.

Bob

 
Reply With Quote
 
 
 
 
=?Utf-8?B?SU5UUDU2?=
Guest
Posts: n/a
 
      24th Jul 2007
I forgot to include, the IF statement in Test() bombs with

Run-time error '91':

Object variable or With block variable not set.

Bob


"INTP56" wrote:

> Is there a way in VBA to compare Object References?
>
> Consider the following
>
> Option Explicit
> Dim WBArray(1 To 1) As Workbook
>
> Public Sub assign()
> Set WBArray(1) = ActiveWorkbook
> End Sub
>
> Public Sub test()
> If WBArray(1) = ActiveWorkbook Then
> MsgBox "Yes"
> Else
> MsgBox "No"
> End If
> End Sub
>
> Functionally, I want to make an AddIn with a class module that captures
> Application events, but I only want to execute the code for those events on
> workbooks that the user has told me are part of my application. So, I was
> considering having the user tell me which workbooks are "mine" and in the
> event handlers, the first thing I do is check to see if the event causing
> workbook is in the list. I was hoping to not have to manage names or other
> user definable properties.
>
> Above, in test(), I functionally want to compare the object reference in the
> array against the object reference for ActiveWorkbook to see if they refer to
> the same object.
>
> Is this something I can do in VBA? (Excel 2003) I could capture the
> workbook.name property, and track changes to the name, but I was hoping to
> avoid that.
>
> Bob
>

 
Reply With Quote
 
=?Utf-8?B?SU5UUDU2?=
Guest
Posts: n/a
 
      24th Jul 2007
Sorry, forget that last error message, different code ....

The error message is:

Run-time error '438':

Object doesn't support this property or method.

"INTP56" wrote:

> Is there a way in VBA to compare Object References?
>
> Consider the following
>
> Option Explicit
> Dim WBArray(1 To 1) As Workbook
>
> Public Sub assign()
> Set WBArray(1) = ActiveWorkbook
> End Sub
>
> Public Sub test()
> If WBArray(1) = ActiveWorkbook Then
> MsgBox "Yes"
> Else
> MsgBox "No"
> End If
> End Sub
>
> Functionally, I want to make an AddIn with a class module that captures
> Application events, but I only want to execute the code for those events on
> workbooks that the user has told me are part of my application. So, I was
> considering having the user tell me which workbooks are "mine" and in the
> event handlers, the first thing I do is check to see if the event causing
> workbook is in the list. I was hoping to not have to manage names or other
> user definable properties.
>
> Above, in test(), I functionally want to compare the object reference in the
> array against the object reference for ActiveWorkbook to see if they refer to
> the same object.
>
> Is this something I can do in VBA? (Excel 2003) I could capture the
> workbook.name property, and track changes to the name, but I was hoping to
> avoid that.
>
> Bob
>

 
Reply With Quote
 
Jim Cone
Guest
Posts: n/a
 
      24th Jul 2007
Bob,

Change...
If WBArray(1) = ActiveWorkbook Then
To...
If WBArray(1) is ActiveWorkbook Then
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware
(Excel Add-ins / Excel Programming)



"INTP56"
<(E-Mail Removed)>
wrote in message
Is there a way in VBA to compare Object References?
Consider the following
Option Explicit
Dim WBArray(1 To 1) As Workbook

Public Sub assign()
Set WBArray(1) = ActiveWorkbook
End Sub

Public Sub test()
If WBArray(1) = ActiveWorkbook Then '<<<<<
MsgBox "Yes"
Else
MsgBox "No"
End If
End Sub

Functionally, I want to make an AddIn with a class module that captures
Application events, but I only want to execute the code for those events on
workbooks that the user has told me are part of my application. So, I was
considering having the user tell me which workbooks are "mine" and in the
event handlers, the first thing I do is check to see if the event causing
workbook is in the list. I was hoping to not have to manage names or other
user definable properties.
Above, in test(), I functionally want to compare the object reference in the
array against the object reference for ActiveWorkbook to see if they refer to
the same object.
Is this something I can do in VBA? (Excel 2003) I could capture the
workbook.name property, and track changes to the name, but I was hoping to
avoid that.
Bob

 
Reply With Quote
 
=?Utf-8?B?SU5UUDU2?=
Guest
Posts: n/a
 
      24th Jul 2007
Thanks Jim, that's just what I wanted. Bob

"Jim Cone" wrote:

> Bob,
>
> Change...
> If WBArray(1) = ActiveWorkbook Then
> To...
> If WBArray(1) is ActiveWorkbook Then
> --
> Jim Cone
> San Francisco, USA
> http://www.realezsites.com/bus/primitivesoftware
> (Excel Add-ins / Excel Programming)


 
Reply With Quote
 
Matthew Pfluger
Guest
Posts: n/a
 
      28th Aug 2008
Thank you very much, Jim! This is also exactly what I was looking for. I
hope it works with other objects as well. I shall do more testing...

Thanks,
Matthew Pfluger

"Jim Cone" wrote:

> Bob,
>
> Change...
> If WBArray(1) = ActiveWorkbook Then
> To...
> If WBArray(1) is ActiveWorkbook Then
> --
> Jim Cone
> San Francisco, USA
> http://www.realezsites.com/bus/primitivesoftware
> (Excel Add-ins / Excel Programming)
>
>
>
> "INTP56"
> <(E-Mail Removed)>
> wrote in message
> Is there a way in VBA to compare Object References?
> Consider the following
> Option Explicit
> Dim WBArray(1 To 1) As Workbook
>
> Public Sub assign()
> Set WBArray(1) = ActiveWorkbook
> End Sub
>
> Public Sub test()
> If WBArray(1) = ActiveWorkbook Then '<<<<<
> MsgBox "Yes"
> Else
> MsgBox "No"
> End If
> End Sub
>
> Functionally, I want to make an AddIn with a class module that captures
> Application events, but I only want to execute the code for those events on
> workbooks that the user has told me are part of my application. So, I was
> considering having the user tell me which workbooks are "mine" and in the
> event handlers, the first thing I do is check to see if the event causing
> workbook is in the list. I was hoping to not have to manage names or other
> user definable properties.
> Above, in test(), I functionally want to compare the object reference in the
> array against the object reference for ActiveWorkbook to see if they refer to
> the same object.
> Is this something I can do in VBA? (Excel 2003) I could capture the
> workbook.name property, and track changes to the name, but I was hoping to
> avoid that.
> Bob
>
>

 
Reply With Quote
 
Jim Cone
Guest
Posts: n/a
 
      28th Aug 2008

Matthew,
You are welcome.
--
Jim Cone
Portland, Oregon USA



"Matthew Pfluger"
wrote in message
Thank you very much, Jim! This is also exactly what I was looking for. I
hope it works with other objects as well. I shall do more testing...
Thanks,
Matthew Pfluger



"Jim Cone" wrote:
> Bob,
> Change...
> If WBArray(1) = ActiveWorkbook Then
> To...
> If WBArray(1) is ActiveWorkbook Then
> --
> Jim Cone
> San Francisco, USA
> http://www.realezsites.com/bus/primitivesoftware
> (Excel Add-ins / Excel Programming)






> "INTP56"
> <(E-Mail Removed)>
> wrote in message
> Is there a way in VBA to compare Object References?
> Consider the following
> Option Explicit
> Dim WBArray(1 To 1) As Workbook
>
> Public Sub assign()
> Set WBArray(1) = ActiveWorkbook
> End Sub
>
> Public Sub test()
> If WBArray(1) = ActiveWorkbook Then '<<<<<
> MsgBox "Yes"
> Else
> MsgBox "No"
> End If
> End Sub
>
> Functionally, I want to make an AddIn with a class module that captures
> Application events, but I only want to execute the code for those events on
> workbooks that the user has told me are part of my application. So, I was
> considering having the user tell me which workbooks are "mine" and in the
> event handlers, the first thing I do is check to see if the event causing
> workbook is in the list. I was hoping to not have to manage names or other
> user definable properties.
> Above, in test(), I functionally want to compare the object reference in the
> array against the object reference for ActiveWorkbook to see if they refer to
> the same object.
> Is this something I can do in VBA? (Excel 2003) I could capture the
> workbook.name property, and track changes to the name, but I was hoping to
> avoid that.
> Bob

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Object Library invalid or contains references to object defintions John Microsoft Excel Programming 1 24th Jul 2009 11:00 AM
Re: Finding if an object references indirectly another object withreflection joproulx@hotmail.com Microsoft C# .NET 0 22nd Aug 2008 08:37 PM
Re: Finding if an object references indirectly another object withreflection raylopez99 Microsoft C# .NET 0 21st Aug 2008 05:24 PM
Re: Finding if an object references indirectly another object withreflection Leon Lambert Microsoft C# .NET 1 21st Aug 2008 02:38 PM
Find object references.. How do I find references to missing objec rocknroj Microsoft Frontpage 7 2nd Feb 2008 09:29 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:13 AM.