Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default programming button again

Hi,

I was trying to program a button with vba (excel) and got the following
advice:

You have to write the event in the sheet module using code (or have it
already there)

http://www.cpearson.com/excel/vbe.htm



It seemed to work, namely in excel 200. Unfortunately, that doesn't work in
excel 2003. The reason is the extra security option "Trust access to vb
project". Even if enabled, it still doesn't allow code to be written, while
it does in 2000, even without changing any option.

Is there no way to just say:
CommandButton1.OnClick="..."
instead of needing to write in the editor?

It's very suprising that such a simple thing as adding a button with a
function first of all, needs such complicated coding (writing in the editor)
and then also leads to security issues...

I pasted the code that I use below.

Anyway, any help is welcome!

Thanks, regards,
-Hendri.


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default programming button again

I pasted the code that I use below.

Sorry, it was missing. Here it is:

Private Sub Add_start_button(cl)
Dim VBEHwnd As Long
Dim StartLine As Long
Dim cmdBtn As CommandButton
On Error GoTo ErrH:
Application.VBE.MainWindow.Visible = False
VBEHwnd = FindWindow("wndclass_desked_gsk",
Application.VBE.MainWindow.Caption)
If VBEHwnd Then
LockWindowUpdate VBEHwnd
End If
Set cmdBtn =
ActiveSheet.OLEObjects.Add(ClassType:="Forms.Comma ndButton.1", _
Left:=Range(cl).Left + 1, Top:=Range(cl).Top + 1, Width:=130,
Height:=24).Object
cmdBtn.Caption = "Ga naar startpagina"
With ActiveWorkbook.VBProject.VBComponents(ActiveSheet. Name).CodeModule
StartLine = .CreateEventProc("Click", "CommandButton1") + 1
.InsertLines StartLine, "Sheets(""Start"").Activate"
End With
Application.VBE.MainWindow.Visible = False
ErrH:
LockWindowUpdate 0&
End Sub

and at the start of the module, we have:

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal ClassName As String, ByVal WindowName As String) As Long
Private Declare Function LockWindowUpdate Lib "user32" _
(ByVal hWndLock As Long) As Long

-Hendri.


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,726
Default programming button again

Hendri,

I have just created a button in 2003 and assigned an event procedure to it
no problems.

Here is the code that I used

'-----------------------------------------------------------------
Sub CreateControlButton()
'-----------------------------------------------------------------
Dim oWs As Worksheet
Dim oOLE As OLEObject

Set oWs = ActiveSheet

Set oOLE =
ActiveSheet.OLEObjects.Add(ClassType:="Forms.Comma ndButton.1", _
Left:=200, Top:=100, Width:=80, Height:=32)

'To set with a cell
'With Range("H2")
' Set oOLE =
ActiveSheet.OLEObjects.Add(ClassType:="Forms.Comma ndButton.1", _
' Left:=.Left, Top:=.Top, Width:=.Width,
Height:=.Height)
'End With

With oOLE
.Object.Caption = "Run myMacro"
.Name = "myMacro"
End With

With ThisWorkbook.VBProject.VBComponents(oWs.CodeName). CodeModule
.InsertLines .CreateEventProc("Click", oOLE.Name) + 1, _
vbTab & "If Range(""A1"").Value 0 Then " & vbCrLf & _
vbTab & vbTab & "Msgbox ""Hi""" & vbCrLf & _
vbTab & "End If"

End With

End Sub




--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)



"Hendri Adriaens" wrote in message
l...
I pasted the code that I use below.


Sorry, it was missing. Here it is:

Private Sub Add_start_button(cl)
Dim VBEHwnd As Long
Dim StartLine As Long
Dim cmdBtn As CommandButton
On Error GoTo ErrH:
Application.VBE.MainWindow.Visible = False
VBEHwnd = FindWindow("wndclass_desked_gsk",
Application.VBE.MainWindow.Caption)
If VBEHwnd Then
LockWindowUpdate VBEHwnd
End If
Set cmdBtn =
ActiveSheet.OLEObjects.Add(ClassType:="Forms.Comma ndButton.1", _
Left:=Range(cl).Left + 1, Top:=Range(cl).Top + 1, Width:=130,
Height:=24).Object
cmdBtn.Caption = "Ga naar startpagina"
With ActiveWorkbook.VBProject.VBComponents(ActiveSheet. Name).CodeModule
StartLine = .CreateEventProc("Click", "CommandButton1") + 1
.InsertLines StartLine, "Sheets(""Start"").Activate"
End With
Application.VBE.MainWindow.Visible = False
ErrH:
LockWindowUpdate 0&
End Sub

and at the start of the module, we have:

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal ClassName As String, ByVal WindowName As String) As Long
Private Declare Function LockWindowUpdate Lib "user32" _
(ByVal hWndLock As Long) As Long

-Hendri.



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default programming button again

Hi Bob,

I have just created a button in 2003 and assigned an event procedure to it
no problems.

Here is the code that I used


Yes, that works indeed. But unfortunately, I still need to enable "trust
access to visual basic project" to make it work. I prefer not to force the
user to enable that.

Do you think that is possible?

Thanks, best regards,
-Hendri Adriaens.


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,726
Default programming button again

No, that is one thing that you have to do I am afraid.

--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)



"Hendri Adriaens" wrote in message
l...
Hi Bob,

I have just created a button in 2003 and assigned an event procedure to
it no problems.

Here is the code that I used


Yes, that works indeed. But unfortunately, I still need to enable "trust
access to visual basic project" to make it work. I prefer not to force the
user to enable that.

Do you think that is possible?

Thanks, best regards,
-Hendri Adriaens.





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default programming button again

No, that is one thing that you have to do I am afraid.

Unfortunately, but thanks for your answer. I will consider using a hyperlink
instead.

Best regards,
-Hendri.


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
programming button Mike Excel Programming 8 February 13th 07 10:51 PM
Programming a Command Button TBoe Excel Programming 2 February 12th 07 10:51 PM
Button: ActiveX programming nicolascap[_4_] Excel Programming 1 March 14th 06 08:13 AM
Programming for a save button Candee[_23_] Excel Programming 5 May 25th 04 08:23 PM
Programming behind a button DRE Excel Programming 2 July 18th 03 03:40 PM


All times are GMT +1. The time now is 08:43 AM.

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"