Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default How to find out if a button exists on a sheet

Hi all,
I use the following code to create a button and procedure at runtime:
With ActiveSheet
Set btn = .OLEObjects.Add(classtype:="Forms.CommandButton.1" ,
_
Left:=.Range("B3").Left, Top:=.Range("B5").Top, _
Width:=100, Height:=20)
End With
btn.Object.Caption = "Back to Forms"
btn.Name = "Button"
With Workbooks("ResultLib.xls").VBProject.VBComponents( ActiveSheet.CodeName).CodeModule
.InsertLines .CreateEventProc("Click", btn.Name) + 1, _
" Application.run ""Frontend1.xls!initialize"" "
End With

This works fine, however this code is called from the click of a
button and on the second call it creates the same procedure again
creating an ambigious name.
I have tried to use an error handler as follows

on error goto notfound
ActiveSheet.Shapes("Button").select
exit sub
notfound:
With ActiveSheet
Set btn = .OLEObjects.Add(classtype:="Forms.CommandButton.1" ,
_
Left:=.Range("B3").Left, Top:=.Range("B5").Top, _
Width:=100, Height:=20)
End With
btn.Object.Caption = "Back to Forms"
btn.Name = "Button"
With Workbooks("ResultLib.xls").VBProject.VBComponents( ActiveSheet.CodeName).CodeModule
.InsertLines .CreateEventProc("Click", btn.Name) + 1, _
" Application.run ""Frontend1.xls!initialize"" "
End With

However the error doesn't seem to be getting thrown and the code just
steps through ignoring the select statement.
Can anybody please shed some light on this.


Regards
Nigel
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default How to find out if a button exists on a sheet

Nigel,

Your code worked fine for me. Perhaps you could change that code so
that when the button is created your also write a flag value to a
customdocument property or to the registry, and use a reading of that
value to exit or continue your routine.

HTH,
Bernie
MS Excel MVP

"Nigel Brown" wrote in message
om...
Hi all,
I use the following code to create a button and procedure at

runtime:
With ActiveSheet
Set btn =

..OLEObjects.Add(classtype:="Forms.CommandButton.1 ",
_
Left:=.Range("B3").Left, Top:=.Range("B5").Top, _
Width:=100, Height:=20)
End With
btn.Object.Caption = "Back to Forms"
btn.Name = "Button"
With

Workbooks("ResultLib.xls").VBProject.VBComponents( ActiveSheet.CodeName
).CodeModule
.InsertLines .CreateEventProc("Click", btn.Name) + 1, _
" Application.run ""Frontend1.xls!initialize"" "
End With

This works fine, however this code is called from the click of a
button and on the second call it creates the same procedure again
creating an ambigious name.
I have tried to use an error handler as follows

on error goto notfound
ActiveSheet.Shapes("Button").select
exit sub
notfound:
With ActiveSheet
Set btn =

..OLEObjects.Add(classtype:="Forms.CommandButton.1 ",
_
Left:=.Range("B3").Left, Top:=.Range("B5").Top, _
Width:=100, Height:=20)
End With
btn.Object.Caption = "Back to Forms"
btn.Name = "Button"
With

Workbooks("ResultLib.xls").VBProject.VBComponents( ActiveSheet.CodeName
).CodeModule
.InsertLines .CreateEventProc("Click", btn.Name) + 1, _
" Application.run ""Frontend1.xls!initialize"" "
End With

However the error doesn't seem to be getting thrown and the code

just
steps through ignoring the select statement.
Can anybody please shed some light on this.


Regards
Nigel



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
Enable Option Button if File Exists E.Q. Excel Discussion (Misc queries) 2 December 28th 07 10:19 PM
Checking if Sheet Exists? [email protected] Excel Discussion (Misc queries) 5 September 1st 06 03:27 PM
check if the sheet/tag exists Alex Excel Worksheet Functions 2 March 14th 06 08:58 PM
Sheet name already exists eddie_zoom Excel Discussion (Misc queries) 1 March 11th 05 02:53 PM
How can I know if a sheet exists ? Ben.C Excel Programming 3 December 29th 03 09:36 AM


All times are GMT +1. The time now is 06:20 PM.

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

About Us

"It's about Microsoft Excel"