Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / New Users / December 2007

Tip: Looking for answers? Try searching our database.

Creating Command Buttons from VBA - adding code

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
tcb - 16 Dec 2007 14:19 GMT
The code doTEST_XLS below creates a command button.  Once the button
is created, in VBA, how do I create the sub or function I want to
run?  For example, if I want to create this MsgBox, what must I add to
my  doTEST_XLS code?

Private Sub CommandButton1_Click()
   MsgBox ("Hello World!")
End Sub

This creates the comand button:

Sub doTEST_XLS()

Dim objXL As Object
Dim strWhat As String, boolXL As Boolean
Dim objActiveWkb As Object

   If fIsAppRunning("Excel") Then
       Set objXL = GetObject(, "Excel.Application")
       boolXL = False
   Else
       Set objXL = CreateObject("Excel.Application")
       boolXL = True
   End If

   objXL.Application.Workbooks.Add
   Set objActiveWkb = objXL.Application.ActiveWorkbook

   With objActiveWkb.ActiveSheet.OLEObjects.Add("Forms.CommandButton.
1")
       .Left = 35
       .Top = 200
       .Object.Caption = "Hello World"
       .Width = 100
       .Height = 50

   End With

   objActiveWkb.Close savechanges:=True

   If boolXL Then objXL.Application.Quit

   Set objActiveWkb = Nothing: Set objXL = Nothing

End Sub
Bob Phillips - 16 Dec 2007 14:23 GMT
Assuming that it is in a worksheet, use

Dim StartLine As Long

   With ActiveWorkbook.VBProject.VBComponents("Sheet1").CodeModule
       StartLine = .CreateEventProc("Click", "CommandButton1") + 1
       .InsertLines StartLine, _
       "    MsgBox ""Hello World!"""
   End With

Signature

---
HTH

Bob

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

> The code doTEST_XLS below creates a command button.  Once the button
> is created, in VBA, how do I create the sub or function I want to
[quoted text clipped - 41 lines]
>
> End Sub
tcb - 16 Dec 2007 14:36 GMT
> Assuming that it is in a worksheet, use
>
[quoted text clipped - 9 lines]
> ---
> HTH

To that I get "Run time error '1004':  Programmatic access to Visual
Basic project is not trusted."  How can I get by that?
Dave Peterson - 16 Dec 2007 15:04 GMT
In xl2003:
Tools|Macro|security...|Trusted publishers tab

But this is a user setting.  If you're sharing your workbook with others, you'll
want to tell them to make the same security change.

There is no way you can include changing this setting in code.

> > Assuming that it is in a worksheet, use
> >
[quoted text clipped - 12 lines]
> To that I get "Run time error '1004':  Programmatic access to Visual
> Basic project is not trusted."  How can I get by that?

Signature

Dave Peterson


Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.