Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Propblem setting OnAction property at runtime

Hi,

I am using Excel 2000.

I create command buttons at runtime like this:
Code snippet

For iIndx = 2 To 24 Step 4
If Cells(14, iIndx).Value < "" Then
iBtn = iBtn + 1
Set xBtnShape = Distribution.Shapes.AddOLEObject( _
Left:=Range(Cells(1, iIndx).Address).Left, _
Top:=Range(Cells(1, iIndx).Address).Top, _
Width:=150, _
Height:=24, _
ClassType:="Forms.CommandButton.1")

With Distribution.OLEObjects(Distribution.OLEObjects.Co unt).Object
.Caption = "Update Distribution Class " & iBtn
End With
end if

next
<<<< END CODE <<<<<

so, this works fine, I get a new button in the right column for each column that has a value in the cell checked by the If... statement.

It then changes the caption fine.

My problem is that I cannot set the OnAction property. Using this code I want to point all my buttons at one global procedure that will check the Application.Caller value and do what is required.

Unfortunately it always gives an Application or Object defined error...

anyone had this before?

thanks

Philip
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Propblem setting OnAction property at runtime

You are adding commandbuttons which don't support the onaction property.
Their code is placed into events. If you want to have one event that
handles all the buttons, you would need to use the technique shown by John
Walkenbach:


http://j-walk.com/ss/excel/tips/tip44.htm
Handle Multiple UserForm Buttons With One Subroutine


Application.Caller is not supported either.

If you want to add buttons form the forms toolbar which do support onaction
and application caller:

Option Base 0

Sub AddButtons()
Dim btn As Button, Varr1
Dim cell as Range
Dim i as Long
Application.ScreenUpdating = False
' to remove previously added buttons -
ActiveSheet.Buttons.Delete
varr1 = Array("Date", "Amount", "Cus Num", _
"Other1", "Other2")

i = 0
For Each Cell In Range("A5:A9")
Set btn = ActiveSheet.Buttons.Add( _
Left:=Cell.Left, _
Top:=Cell.Top, _
Width:=Cell.Width, _
Height:=Cell.Height)
btn.OnAction = "Macro1"
btn.Caption = varr1(i)
btn.Name = varr1(i)
i = i + 1
Next

Application.ScreenUpdating = True
End Sub
Sub Macro1()
MsgBox Application.Caller
End Sub

as an example.

--
Regards,
Tom Ogilvy


Philip wrote in message
...
Hi,

I am using Excel 2000.

I create command buttons at runtime like this:
Code snippet

For iIndx = 2 To 24 Step 4
If Cells(14, iIndx).Value < "" Then
iBtn = iBtn + 1
Set xBtnShape = Distribution.Shapes.AddOLEObject( _
Left:=Range(Cells(1, iIndx).Address).Left, _
Top:=Range(Cells(1, iIndx).Address).Top, _
Width:=150, _
Height:=24, _
ClassType:="Forms.CommandButton.1")

With

Distribution.OLEObjects(Distribution.OLEObjects.Co unt).Object
.Caption = "Update Distribution Class " & iBtn
End With
end if

next
<<<< END CODE <<<<<

so, this works fine, I get a new button in the right column for each

column that has a value in the cell checked by the If... statement.

It then changes the caption fine.

My problem is that I cannot set the OnAction property. Using this code I

want to point all my buttons at one global procedure that will check the
Application.Caller value and do what is required.

Unfortunately it always gives an Application or Object defined error...

anyone had this before?

thanks

Philip



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Propblem setting OnAction property at runtime

Thanks Tom, the second solution is what I wanted...

cheer

Phili

----- Tom Ogilvy wrote: ----

You are adding commandbuttons which don't support the onaction property
Their code is placed into events. If you want to have one event tha
handles all the buttons, you would need to use the technique shown by Joh
Walkenbach


http://j-walk.com/ss/excel/tips/tip44.ht
Handle Multiple UserForm Buttons With One Subroutin


Application.Caller is not supported either

If you want to add buttons form the forms toolbar which do support onactio
and application caller

Option Base

Sub AddButtons(
Dim btn As Button, Varr
Dim cell as Rang
Dim i as Lon
Application.ScreenUpdating = Fals
' to remove previously added buttons
ActiveSheet.Buttons.Delet
varr1 = Array("Date", "Amount", "Cus Num",
"Other1", "Other2"

i =
For Each Cell In Range("A5:A9"
Set btn = ActiveSheet.Buttons.Add(
Left:=Cell.Left,
Top:=Cell.Top,
Width:=Cell.Width,
Height:=Cell.Height
btn.OnAction = "Macro1
btn.Caption = varr1(i
btn.Name = varr1(i
i = i +
Nex

Application.ScreenUpdating = Tru
End Su
Sub Macro1(
MsgBox Application.Calle
End Su

as an example

-
Regards
Tom Ogilv


Philip wrote in messag
..
Hi
I am using Excel 2000
I create command buttons at runtime like this
Code snippet For iIndx = 2 To 24 Step

If Cells(14, iIndx).Value < "" The
iBtn = iBtn +
Set xBtnShape = Distribution.Shapes.AddOLEObject(
Left:=Range(Cells(1, iIndx).Address).Left,
Top:=Range(Cells(1, iIndx).Address).Top,
Width:=150,
Height:=24,
ClassType:="Forms.CommandButton.1"
Wit

Distribution.OLEObjects(Distribution.OLEObjects.Co unt).Objec
.Caption = "Update Distribution Class " & iBt
End Wit
end i
nex

<<<< END CODE <<<<< so, this works fine, I get a new button in the right column for eac

column that has a value in the cell checked by the If... statement
It then changes the caption fine
My problem is that I cannot set the OnAction property. Using this code

want to point all my buttons at one global procedure that will check th
Application.Caller value and do what is required
Unfortunately it always gives an Application or Object defined error..
anyone had this before
thank
Phili




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing several parameters to OnAction property. LABKHAND Excel Discussion (Misc queries) 1 April 24th 09 08:02 PM
DrawingObjects/AutoShapes' .OnAction property in DialogSheets BizMark Excel Discussion (Misc queries) 0 October 12th 05 12:40 PM
Capturing Shape Name/Text from OnAction property William Bartusek Excel Discussion (Misc queries) 3 April 12th 05 06:38 PM
unable to set the OnAction Property of the Text Box Russell Harris Excel Programming 1 December 10th 03 12:52 AM
Find OnAction property Kemosabe Excel Programming 1 November 21st 03 03:34 PM


All times are GMT +1. The time now is 03:47 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"