PDA

View Full Version : Solved: Add-in with custom menu



TrippyTom
11-01-2006, 10:25 AM
Hi everyone,

I'm sick of the limitations of the Slide Layout task pane. I made some macros to insert entire slides (with different layouts per slide) from a file I called "Layouts.ppt".

Now I'm wondering how I can create a custom menu to call these macros into the current presentation and make it an add-in.

Is this relatively easy?

TrippyTom
11-01-2006, 11:08 AM
This is the code for one of the subroutines. Very simple, and I expect I might have to change some of it if it's to become an add-in.

Sub twoSectionsDown()
On Error Resume Next

With Application.Presentations("Layouts.ppt")
.Slides(15).Copy
End With
With Application.ActiveWindow
.ViewType = ppViewSlideSorter
.View.Paste
.ViewType = ppViewNormal
End With
End Sub

Note: I was seeing it paste as a picture sometimes, so that's why I force it to go into slide sorter mode before I paste then switch back (it seems to have fixed that problem).

TrippyTom
11-02-2006, 02:41 PM
ok, let me further clarify my question:

How do you create a submenu in PowerPoint vba?
Here's my code so far:

Sub AddNewMenu()
Dim cToolsMenu As CommandBarControl
Dim NewMenu As CommandBarPopup
Dim MenuItem As Variant
Dim SubMenu As Variant

'delete the menu if it already exists
Call DeleteNewMenu

'find the (Tools) Menu
Set cToolsMenu = CommandBars(1).FindControl(Id:=30009)

If cToolsMenu Is Nothing Then
'add the menu to the end
Set NewMenu = CommandBars(1).Controls.Add(Type:=msoControlPopup, temporary:=True)
Else
'add the menu before (Tools)
'Set NewMenu = CommandBars(1).Controls.Add(Type:=msoControlButtonDropdown, before:=cToolsMenu.Index, temporary:=True)
Set NewMenu = CommandBars(1).Controls.Add(Type:=msoControlPopup, before:=cToolsMenu.Index, temporary:=True)
End If

'add a caption for the menu
NewMenu.Caption = "(Tombstones)"

'1st menu item
'Set MenuItem = NewMenu.Controls.Add(Type:=msoControlButton)
Set MenuItem = NewMenu.Controls.Add(Type:=msoControlButton)
With MenuItem
.Caption = "Normal..."
'1st submenu item
Set MenuItem = NewMenu.Controls.Add(Type:=msoControlButton)
With MenuItem
.Caption = "1st sub"
.OnAction = "ControlRoutine"

'2nd submenu item
Set MenuItem = NewMenu.Controls.Add(Type:=msoControlButton)
With MenuItem
.Caption = "2nd sub"
.OnAction = "ControlRoutine"
End With
End With
'2nd menu item
Set MenuItem = NewMenu.Controls.Add(Type:=msoControlButton)
With MenuItem
.BeginGroup = True '<------- begins a new group
.Caption = "With PM..."
End With
End With
End Sub


I want 1st Sub and 2nd Sub to be UNDER the 1st Menu item, but it's not.

TrippyTom
11-02-2006, 04:21 PM
Nevermind, I got it figured out. In case anyone else needs to know, the syntax is similar to this:

Sub AddNewMenu()
Dim cWindowMenu As CommandBarControl
Dim NewMenu As CommandBarPopup
Dim menuItem As CommandBarButton
Dim subItem As CommandBarPopup

'delete the menu if it already exists
Call DeleteNewMenu

'find the (Tools) Menu
Set cWindowMenu = CommandBars(1).FindControl(Id:=30009) 'id = Window menu - we will put it before (to the left) of that

If cWindowMenu Is Nothing Then
'add the menu to the end
Set NewMenu = CommandBars(1).Controls.Add(Type:=msoControlPopup, temporary:=True)
Else
'add the menu before the Window menu
'Set NewMenu = CommandBars(1).Controls.Add(Type:=msoControlButtonDropdown, before:=cToolsMenu.Index, temporary:=True)
Set NewMenu = CommandBars(1).Controls.Add(Type:=msoControlPopup, before:=cWindowMenu.Index, temporary:=True)
End If

'add a caption for the menu
NewMenu.Caption = "(Tombstones)"

'1st menu item
Set subItem = NewMenu.Controls.Add(Type:=msoControlPopup)
With subItem
.Caption = "Normal"
Set menuItem = subItem.Controls.Add(Type:=msoControlButton)
With menuItem
.Caption = "1st"
End With
Set menuItem = subItem.Controls.Add(Type:=msoControlButton)
With menuItem
.Caption = "2nd"
End With
Set menuItem = subItem.Controls.Add(Type:=msoControlButton)
With menuItem
.Caption = "3rd"
End With
End With
'2nd menu item
Set subItem = NewMenu.Controls.Add(Type:=msoControlPopup)
With subItem
.Caption = "With PM"
End With
End Sub

grime
11-09-2006, 12:51 PM
Thanks for the code!

Can you provide your code for the DeleteNewMenu routine?

TrippyTom
11-09-2006, 04:29 PM
Here's my code for it. Change "(Tombstones)" to whatever you named your menu:

Sub DeleteNewMenu()
Dim cb As CommandBarControl

For Each cb In Application.CommandBars("Menu Bar").Controls
If cb.Caption = "(Tombstones)" Then cb.Delete
Next
End Sub


And of course, if you're making this an add-in you need to have auto-open and auto-close routines:

Sub Auto_Open()
AddNewMenu
End Sub

Sub Auto_Close()
DeleteNewMenu
End Sub

twnty2
07-19-2011, 03:55 PM
Not sure if reviving an old thread is the best way to go about this but im after a similar code - i need to code a macro which create a custom menu and then inserts all the other macros into the menu - can anyone help? Trippytoms coding works however not sure how to take it to the next step by having the additional macros inserted into the custom menu.

this is for powerpoint 2003.

John Wilson
07-22-2011, 01:34 PM
Maybe try something like this:

Sub Auto_Open()
Dim myMainMenuBar As CommandBar
Dim myCustomMenu As CommandBarControl
Dim myTempMenu As CommandBarControl


On Error Resume Next
Application.CommandBars.ActiveMenuBar.Controls("This name").Delete
On Error GoTo errorhandler
Set myMainMenuBar = Application.CommandBars.ActiveMenuBar


Set myCustomMenu = myMainMenuBar.Controls.Add(Type:=msoControlPopup, _
before:=3)
myCustomMenu.Caption = "This name"
Set myTempMenu = myCustomMenu.Controls.Add(Type:=msoControlButton)
With myTempMenu

.Caption = "Whatever"
.OnAction = "Macro1"
End With

Set myTempMenu = myCustomMenu.Controls.Add(Type:=msoControlButton)
With myTempMenu

.Caption = "Something else"
.OnAction = "Macro2"
End With

Set myTempMenu = myCustomMenu.Controls.Add(Type:=msoControlButton)
With myTempMenu

.Caption = "Another thing"
.OnAction = "Macro3"
End With

'repeat these as often as you wish

Exit Sub
errorhandler:
MsgBox "Sorry there's been an error " & Err.Description, vbCritical
End Sub

twnty2
07-24-2011, 09:57 PM
yes i was able to work it out after some extensive searching - basically got the same as what you wrote up.. Thanks

magnel
08-20-2013, 03:51 AM
I'm using PPT 2010, can we use "msoControlButton" and "msocontrolbuttondropdown" type of buttons in the same toolbar of addin. Can you help me with a sample code. Thanks

John Wilson
08-20-2013, 04:09 AM
In 2010 none of this is needed. You can create Ribbon entries in XML.

See here (http://www.pptalchemy.co.uk/custom_UI.html) for a very basic tutorial and some free help files - Make sure you read the notes on the new version of the CustomUi editor and the extra step needed.

Google for RibbonX for more

magnel
08-22-2013, 03:30 AM
Thanks John, I will surely check out custom UI editor and all details given in the link.

John Wilson
08-22-2013, 04:37 AM
There is a learning curve but in the long run it's way easier!