Consulting

Results 1 to 13 of 13

Thread: Solved: Add-in with custom menu

  1. #1
    VBAX Expert TrippyTom's Avatar
    Joined
    Jul 2005
    Location
    New York, NY (USA)
    Posts
    556
    Location

    Solved: Add-in with custom menu

    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?
    Office 2010, Windows 7
    goal: to learn the most efficient way

  2. #2
    VBAX Expert TrippyTom's Avatar
    Joined
    Jul 2005
    Location
    New York, NY (USA)
    Posts
    556
    Location
    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.
    [vba]
    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
    [/vba]
    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).
    Last edited by TrippyTom; 11-01-2006 at 12:04 PM.
    Office 2010, Windows 7
    goal: to learn the most efficient way

  3. #3
    VBAX Expert TrippyTom's Avatar
    Joined
    Jul 2005
    Location
    New York, NY (USA)
    Posts
    556
    Location
    ok, let me further clarify my question:

    How do you create a submenu in PowerPoint vba?
    Here's my code so far:
    [vba]
    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
    [/vba]

    I want 1st Sub and 2nd Sub to be UNDER the 1st Menu item, but it's not.
    Office 2010, Windows 7
    goal: to learn the most efficient way

  4. #4
    VBAX Expert TrippyTom's Avatar
    Joined
    Jul 2005
    Location
    New York, NY (USA)
    Posts
    556
    Location
    Nevermind, I got it figured out. In case anyone else needs to know, the syntax is similar to this:
    [vba]
    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
    [/vba]
    Office 2010, Windows 7
    goal: to learn the most efficient way

  5. #5
    Thanks for the code!

    Can you provide your code for the DeleteNewMenu routine?

  6. #6
    VBAX Expert TrippyTom's Avatar
    Joined
    Jul 2005
    Location
    New York, NY (USA)
    Posts
    556
    Location
    Here's my code for it. Change "(Tombstones)" to whatever you named your menu:
    [vba]
    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
    [/vba]

    And of course, if you're making this an add-in you need to have auto-open and auto-close routines:
    [vba]
    Sub Auto_Open()
    AddNewMenu
    End Sub

    Sub Auto_Close()
    DeleteNewMenu
    End Sub
    [/vba]
    Office 2010, Windows 7
    goal: to learn the most efficient way

  7. #7
    VBAX Newbie
    Joined
    Jul 2011
    Posts
    5
    Location

    help

    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.

  8. #8
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    Maybe try something like this:

    [VBA]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[/VBA]
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  9. #9
    VBAX Newbie
    Joined
    Jul 2011
    Posts
    5
    Location
    yes i was able to work it out after some extensive searching - basically got the same as what you wrote up.. Thanks

  10. #10
    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

  11. #11
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    In 2010 none of this is needed. You can create Ribbon entries in XML.

    See here 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
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  12. #12
    Thanks John, I will surely check out custom UI editor and all details given in the link.

  13. #13
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    There is a learning curve but in the long run it's way easier!
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •