PDA

View Full Version : Solved: Macro men?



sasa
05-21-2008, 01:06 AM
Hi All,
I need some help about this matter. I find this routine at http://pptfaq.com/FAQ00031.htm

Start with a new PowerPoint file
Start a new PowerPoint file. Press ALT+F11 to start the VB Editor. Choose Insert, New Module.
Add this code to the new module:
Sub Auto_Open()
Dim oToolbar As CommandBar
Dim oButton As CommandBarButton
Dim MyToolbar As String

' Give the toolbar a name
MyToolbar = "Kewl Tools"

On Error Resume Next
' so that it doesn't stop on the next line if the toolbar's already there

' Create the toolbar; PowerPoint will error if it already exists
Set oToolbar = CommandBars.Add(Name:=MyToolbar, _
Position:=msoBarFloating, Temporary:=True)
If Err.Number <> 0 Then
' The toolbar's already there, so we have nothing to do
Exit Sub
End If

On Error GoTo ErrorHandler

' Now add a button to the new toolbar
Set oButton = oToolbar.Controls.Add(Type:=msoControlButton)

' And set some of the button's properties
With oButton
.DescriptionText = "This is my first button"
'Tooltip text when mouse if placed over button
.Caption = "Button1"
'Text if Text in Icon is chosen
.OnAction = "Button1"
'Runs the Sub Button1() code when clicked
.Style = msoButtonIcon
' Button displays as icon, not text or both
.FaceId = 52
'52 is my favorite pig;
' chooses icon #52 from the available Office icons
End With

' Repeat the above for as many more buttons as you need to add
' Be sure to change the .OnAction property at least for each new button

' You can set the toolbar position and visibility here if you like
' By default, it'll be visible when created
oToolbar.Top = 150
oToolbar.Left = 150
oToolbar.Visible = True

NormalExit:
Exit Sub ' so it doesn't go on to run the errorhandler code

ErrorHandler:
'Just in case there is an error
MsgBox Err.Number & vbCrLf & Err.Description
Resume NormalExit:
End Sub

Sub Button1()
' This code will run when you click Button 1 added above
' Add a similar subroutine for each additional button you create on the toolbar
' This is just some silly example code.
' You'd put your real working code here to do whatever
' it is that you want to do
MsgBox "Will you PLEASE stop clicking me? I have a headache already!"
End Sub
It works well, but as I have more than one macro and I need To have these alltogether in a unique menu, I don't know how to make the correct steps to do this. In other words what I have to modify to obtain the result I need.

Thanks

sasa

John Wilson
05-21-2008, 02:06 AM
First of all you should add a routine to delete the existing toolbar on close

Something like:

Sub auto_close()
Dim ocmd As CommandBar
For Each ocmd In Application.CommandBars
If ocmd.Name Like "Kewl*" Then ocmd.Delete
Next ocmd
End Sub

To add more buttons Dim more as required eg

Dim oButton1 As CommandBarButton
Dim oButton2 As CommandBarButton

In the auto_open module repeat the section from "Set.oButton= etc" TO "End With" using the different names for buttons and of course different actions and FaceIDs.

Hope that makes sense

sasa
05-21-2008, 02:53 AM
Thanks a lot. Your advices were very useful. A spare a lot of time.
Thanks again !!!!