View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
Peter T Peter T is offline
external usenet poster
 
Posts: 5,600
Default how to get the value object back from 'customproperties'?

There must be many ways to uniquely identify your buttons, eg

' normal module in Excel
Sub abc() ' make some unique buttons
Dim i As Long
Dim ole As OLEObject
ActiveSheet.OLEObjects.Delete
For i = 1 To 3
Set ole = ActiveSheet.OLEObjects.Add("Forms.CommandButton.1" , _
Left:=20, Top:=30 + (i - 1) * 40, Width:=150, Height:=27)
ole.Name = "NA_AB" & Right("00" & i, 3)
Next
End Sub


Following is in VB6, but could be in any other VBA app, eg Word (with a
reference to Excel), or adapted for C#

' Form code
Private mCls2 As Class2
Private Sub Form_Load() ' in VBA use Userform's Initialize event
Set mCls2 = New Class2
Set mCls2.xlApp = GetObject(, "excel.application")
mCls2.Init
End Sub

Private Sub Form_Terminate()
Set mCls2 = Nothing
End Sub
''''''''''''''''''''''
' Class1
Public WithEvents btn As MSForms.CommandButton
Public ws As Excel.Worksheet
Public idx As Long

Private Sub btn_Click()
ws.Cells(idx, 5) = ws.Cells(idx, 5) + 1
End Sub
'''''''''''''''''''''''
'' Class2
Public WithEvents xlApp As Excel.Application
Private mCol As Collection
Private mSH As Object

Public Sub Init()
xlApp_SheetActivate xlApp.ActiveSheet
End Sub


Private Sub xlApp_SheetActivate(ByVal Sh As Object)
Dim i As Long
Dim ws As Worksheet
Dim ole As Excel.OLEObject
Dim c As Class1
Set mCol = Nothing
If TypeName(Sh) = "Worksheet" Then
For Each ole In Sh.OLEObjects
If Left$(ole.Name, 5) = "NA_AB" Then
If mCol Is Nothing Then
Set mCol = New Collection
Set mSH = Sh
End If
Set c = New Class1
Set c.btn = ole.object
c.idx = Val(Right$(ole.Name, 3))
Set c.ws = Sh
mCol.Add c, ole.Name
End If
Next
End If
End Sub

Private Sub xlApp_SheetDeactivate(ByVal Sh As Object)
If Not mSH Is Nothing Then
If Sh Is mSH Then
Set mCol = Nothing
Set mSH = Nothing
End If
End If
End Sub


This is bare bones, obviously would require a lot more and probably wouldn't
use GetObject. Even so this simple code will pick up any of "your" buttons
and trap their events.

Regards,
Peter T




"NA_AB" wrote in message
...

hey thank you guys for replying :)

peter


Since am creating buttons this way,
btn = sht.Shapes.AddOLEObject("Forms.CommandButton.1",.. ..);

I'll have to look for a way of identifying each button uniquely. However
once I close my excel and reopen, I should still be able to identify them
uniquely so that I can assign their respective catch events. Is there a
way
to do this?!

Also, there are more than one button at a time and I am going to have to
add
and delete them dynamically. Once I click a button, it refreshes a certain
area of the sheet and loads the result set in that area. This area keeps
changing at different times even for a single button. So, how am I going
to
attach the events? There are just too many things to be remembered like
the
area dimensions etc.

Regards,
na_ab