How to add icons to custom menu in excel - vba - vba

I have created some custom menu referring "Custom Menu visible to one document in Excel". Now I want to add some icons to each menu item. Either that may be system icons or some others. Please provide some solution.

use the .FaceID property. If you want to know what id to use, then download a FaceID Browser addin for excel. Example:
Set MenuItem = NewMenu.Controls.Add _
(Type:=msoControlButton)
With MenuItem
.Caption = "Save XML Data"
.FaceId = 270
.OnAction = "AskExportXml"
.Enabled = True
End With

Related

Excel VBA Adding 'paste special as value' icon to right click menu (cell context menu customization)

I wonder how to add new icon to right click menu using VBA code which will be the shortcut to paste special as value Excel functionality (which can be found in Excel 2010, but not in 2007). Adding icon itself isn't the problem, but is there generic method (not VBA macro) which could be associated with that icon ?
Below code which add icon associated with ToggleCaseMacro macro (which isn't defined here) :
Sub AddToCellMenu()
Dim ContextMenu As CommandBar
Dim MySubMenu As CommandBarControl
' Delete the controls first to avoid duplicates.
Call DeleteFromCellMenu
' Set ContextMenu to the Cell context menu.
Set ContextMenu = Application.CommandBars("Cell")
' Add one built-in button(Save = 3) to the Cell context menu.
ContextMenu.Controls.Add Type:=msoControlButton, ID:=3, before:=1
' Add one custom button to the Cell context menu.
With ContextMenu.Controls.Add(Type:=msoControlButton, before:=2)
.OnAction = "'" & ThisWorkbook.Name & "'!" & "ToggleCaseMacro"
.FaceId = 59
.Caption = "Toggle Case Upper/Lower/Proper"
.Tag = "My_Cell_Control_Tag"
End With
End Sub
MSDN page with above code
You just need to add:
ContextMenu.Controls.Add Type:=msoControlButton, ID:=370, Before:=2

Calling a Macro in Word Com Addin on msocontrolbutton.onaction

I am developing a com Add In For Word 2007.
I am trying to create a popup menu and each control button in that is calling a macro present inside the same ThisAddin Class.
But in word when i click on the control button in popup menu i get the following error :
"The macro cannot be found or has been disabled because of your macro
security settings"
I have tried a lot of search but unable to get any related info. so i think it might not be a issue at all or not a feature at all.
The code for menu generation is as below :
cbrCmdBar = MyWordAddIn.Globals.ThisAddIn.Application.CommandBars.Add(Name:="MyPopup", Position:=Office.MsoBarPosition.msoBarPopup, MenuBar:=False, Temporary:=True)
With cbrCmdBar.Controls
With .Add(Type:=Office.MsoControlType.msoControlButton)
.Caption = "Point"
.OnAction = "StopPoint"
.FaceId = 70
End With
End With
And the macro is :
Public Sub StopPoint()
MsgBox("Popup Control Button Selected")
End Sub
On the other hand , if i put the macro in template of the ActiveDocument It works fine.
Please help.
Below FaceID, add:
.OnAction = "=StopPoint()"

Customizing a ribbon with VBA in Excel

I've already learned that creating a custom tab is in Excel not possible in this! post (unlike i.e. MSProject)
Specifically, can I change the paths of the macros to the current location ?
Edit
It seems that this page may lead to the answer, but I still don't know how, though
Some more in-detail description of the situation:
The user will download a file, which contains a list of materials, equipment, labor. Each column contains information about quantities, pricing etc
When the user click on this button I want to create (I have created manually), a macro called 'Main' in another workbook launches and copies the whole sheet (the contents) to itself and performs some things that procedures do, on it.
So the problem I'm facing, is twhen I'm sending a new version to the client, he has to put it in the exact location or it won't work. Since there's a mix of Mac and Windows computers involved, I'd rather see a situation where the button is assigned to the procedure when the user opens WorkBook B (the one that contains the code).
This way, a new version has to be openened only once, and then for continuous use, the user can just open the downloaded file, click the appropriate button and WorkBook B will open itself and execute.
Maybe there's other ways to go about this. I haven't checked if it's not easier to assign a button to the quick access toolbar...
This is some code I use to add a custom toolbar:
Set cbToolbar = Application.CommandBars.Add(csToolbarName, msoBarTop, False, True)
With cbToolbar
Set ctButton1 = .Controls.Add(Type:=msoControlButton, ID:=2950)
Set ctButton2 = .Controls.Add(Type:=msoControlButton, ID:=2950)
Set ctButton3 = .Controls.Add(Type:=msoControlButton, ID:=2950)
End With
With ctButton1
.Style = msoButtonIconAndCaption
.Caption = "Set &Picklists"
.FaceId = 176
.OnAction = "SetPicklist"
End With
With ctButton2
.Style = msoButtonIconAndCaption
.Caption = "Set &Defaults"
.FaceId = 279
.OnAction = "SetDefaults"
End With
With ctButton3
.Style = msoButtonIconAndCaption
.Caption = "&Visibility Settings"
.FaceId = 2174
.OnAction = "VisibilitySettings"
End With
With cbToolbar
.Visible = True
.Protection = msoBarNoChangeVisible
End With
The 'OnAction' controls the macro that runs... If you wanted to expand that to run a macro on a specific workbook, use "whatever.xls!MacroName"

Set a CommandBar "Popup" submenu icon dynamically

I'm just trying to confirm this: In Office 2003, I want to create a custom submenu--what is known in CommandBar parlance as a popup (msoControlPopup)--at runtime, and set an image for it. With a CommandBarButton, this is very straightforward
Set btn1 = mnu.Controls.Add(msoControlButton, temporary:=True)
btn1.Caption = "Btn1"
btn1.Picture = stdole.LoadPicture("C:\temp\test.bmp")
But with a CommandBarPopup, or CommandBarControl of type msoControlPopup, it fails
Set sub1 = mnu.Controls.Add(msoControlPopup, temporary:=True)
sub1.Caption = "Sub1"
'object doesn't support this property or method for next line
sub1.Picture = stdole.LoadPicture("C:\temp\test.bmp")
The msoControlPopup type doesn't seem to allow a .Style property either, which is how Office determines what to show--icon, text, both--on the control. I haven't found this proven yet, so am holding out a last hope that I'm doing something wrong, and there is, in fact, a way to insert an icon on a submenu at runtime.
Thanks for any light you can shed.
Ok well more tumbleweeds. I'm pretty sure the answer to this is, it can't be done. And here's my "proof": None of the built-in submenus have icons (which I didn't realize until after I posted the above, and if you run the above code, go to Tools > Customize in the menu bar, then click on the Test menu to drop it down, and right-click on Sub1, you should see all the button and style options greyed out. Right-click on Btn1, and they're available.
Any other thoughts still welcome.
Of course if you need to set the image or FaceID of the submenu heading, that is not an available method for submenu headings, but if you want to set the image or FaceID on the submenu itself, I modified code from here to accomplish this:
Public Sub newSubMenu()
Dim menuBar As CommandBar
Dim newMenu As CommandBarControl
Dim menuItem As CommandBarControl
Dim subMenuItem As CommandBarControl
CommandBars("Sub Menu Bar").Delete
Set menuBar = CommandBars.Add(menuBar:=False, Position:=msoBarPopup, Name:="Sub Menu Bar", Temporary:=True)
Set newMenu = menuBar.Controls.Add(Type:=msoControlPopup)
newMenu.Caption = "&First Menu"
Set newMenu = menuBar.Controls.Add(Type:=msoControlPopup)
newMenu.Caption = "&Second Menu"
Set newMenu = menuBar.Controls.Add(Type:=msoControlPopup)
newMenu.Caption = "&Third Menu"
Set menuItem = newMenu.Controls.Add(Type:=msoControlButton)
With menuItem
.Caption = "F&irst Sub"
.FaceId = "356"
.OnAction = "myTest"
End With
Set menuItem = newMenu.Controls.Add(Type:=msoControlButton)
With menuItem
.Caption = "S&econd Sub"
.FaceId = "333"
.OnAction = "otherTest"
End With
Set menuItem = newMenu.Controls.Add(Type:=msoControlPopup)
menuItem.Caption = "Sub Menus"
Set subMenuItem = menuItem.Controls.Add(Type:=msoControlButton)
With subMenuItem
.Caption = "Item 1"
.FaceId = 321
.OnAction = "firstMacro"
End With
Set subMenuItem = menuItem.Controls.Add(Type:=msoControlButton)
With subMenuItem
.Caption = "Item 2"
'.FaceId = 432
.Picture = stdole.StdFunctions.LoadPicture("C:\temp\test.bmp")
.OnAction = "secondMacro"
End With
CommandBars("Sub Menu Bar").ShowPopup
End Sub
I tested this and it appears to work fine for both FaceID's and loaded pictures.
Of course to get the "on run time" affect, I would recommend placing this in a function which was called each time the user clicked on a specific control.
It could be further expended to handle variable pictures here as well.
Hope this helps.

Programmatically add a hyperlink button to outlook 2007

I am adding a toolbar to Outlook.
The toolbar will have one button on it.
This button, when pressed, will open up a web site.
I've created the button & toolbar no problem, but cannot work out how to add the hyperlink?
Set oTBar = oView.Add("toolbarname")
oTBar.Position = 1
oTBar.Visible = true
Set oButton = OTBar.Controls.Add
With oButton
.Caption = "Click here!"
.Style = 3
.HyperlinkType = 1
.FaceId = 1707
End with
Haha!
You need to add it as the tooltip:
.ToolTipText = "http://your_url"