Does Excel VBA object model allows for access to keyboard shortcut to VBA IDE menu items? - vba

Does Excel VBA object model allows for access to keyboard shortcuts to VBA IDE menu items ?
(I would like to create additional item in menu and connect it with choosen shortcut)
For example code below returns "C&lear" which means you can access "Clear" item by typing "l" when the "Edit" group in VBE IDE is active, but you can also access "Clear" by typing "Delete" button alone :
Debug.Print Application.VBE.CommandBars(1).Controls(2).Controls(6).Caption
is there a way to find pragrammaticaly direct shortcut to item in IDE menu ("Delete" in the case above) ? I would like to add item to VBA IDE menu with new custom shortcut ?
Debug.Print Application.VBE.CommandBars(1).Controls(3).Controls(6).Caption
returns "&Immediate Window" but you could also access Immediate Window by direct shortcut "Ctrl+G"

is there a way to find pragrammaticaly direct shortcut to item in IDE menu ("Delete" in the case above)
Yes, there is. Try this
Debug.Print Application.VBE.CommandBars(1).Controls(2).Controls(6).ShortcutText
This will give you Del
Followup from comments:

Dim b As CommandBarButton
Set b = Application.VBE.CommandBars(1).Controls(2).Controls(6)
Debug.Print b.TooltipText
returns:
C&lear (Del)
Similarly,
Set b = Application.VBE.CommandBars(1).Controls(2).Controls(6)
Debug.Print b.TooltipText
returns
Export (Ctrl+E)

Related

VBA - Create a menu list when you right click a listbox

I would like to have a menu with two options when user right clicks listbox.
With that menu user would do some actions on selected data, e.g. add new line, change some columns data or delete row. I think I can manage that code but I can not grasp how to create a right click custom menu.
I can not find a way to create such option. Does anyone has an idea?
Here's some code for the right click event:
Select ListBox item on rightclick in Word VBA
Replace this code with your context menu code:
Me.Caption = derivedIndex & " = " & ListBox1.List(derivedIndex)
Here's a example for the popup menu (you need skills to understand Win32 API calls...)
https://www.vbarchiv.net/api/api_createpopupmenu.html
The right click menu on the listbox that created by using only a class(without using Api or Declare function) contains the following buttons ;
Increase Font Size
Decrease Font Size
Sort A-Z
Remove List
Remove All
For VBA codes and sample file review this link

Disable File>Share in Excel with VBA

I need a way to disable the ability for a user to go to (or use) the menu File > Share
I have similar for other save commands like below but need something for this feature as well
Application.CommandBars("Worksheet Menu Bar").Controls("File").Controls("Save As...").Enabled = False
Application.CommandBars("Worksheet Menu Bar").Controls("File").Controls("Save").Enabled = False
I have tried:
Application.CommandBars("Worksheet Menu Bar").Controls("File").Controls("Share").Enabled = False
to no avail
My aim is to stop users saving copies of this file (Hosted on a server), I fully understand Excel isn't meant to be secure and there is always a way to do this but want to make it as hard as I can for the average Joe
Regards
You could use a for each loop to extract the available commands:
' Iterate available commands from the file command bar.
Sub PrintAllCommandBarControlNames()
Dim cbControl As CommandBarControl
For Each cbControl In Application.CommandBars("Worksheet Menu Bar").Controls("File").Controls
Debug.Print cbControl.Caption
Next
End Sub
Run on Excel 2010, I couldn't find a share option. Might just be my system. The above returned:
&New...
&Open...
&Close
&Save
Save &As...
Single Web Page (*.mht)
Save &Workspace...
File Searc&h...
Per&mission...
Per&mission
Ch&eck Out
Ch&eck In...
Ve&rsion History...
We&b Page Preview
Page Set&up...
Prin&t Area
Print Pre&view
&Print...
Sen&d To
Propert&ies
&Recent File Name Goes Here
&Recent File Name Goes Here
Sign ou&t
E&xit Excel
The 'backstage' menu (that you get when you click top left File menu) is effectively part of the Ribbon, and not a Command Bar. If you tried disabling Save for instance, using your example, you'll find they don't work on 2010/2013 etc.
There is this answer which tells you how to manipulate those menu items in the ribbon: Remove "Save & Send" from File menu in Excel 2010 using custom XML and one of the items is called TabShare.

Access command from the Quick Access Toolbar

When I use a macro, that opens a new email from a template, I would also like to access one of the commands from the Quick Access Toolbar and make sure a particular option is selected from the drop-down list on this command.
For example, the tab on the toolbar is called 'Add-Ins' and within this tab the area on the toolbar is called 'custom toolbars'. There is a command in this section entitled 'ACT! History', and I would like to automatically chose 'None' from the drop-down list for this command. I do have a screen grab of the toolbar I can send you if this helps.
The current macro is a very simple one as the text below shows.
Sub MakeItem()
Set newItem = Application.CreateItemFromTemplate("C:\Users\jfield\AppData\[file location]\New Placement!.oft")
newItem.Display
Set newItem = Nothing
End Sub

In Word, Programmatically Open New Document Dialog

I am looking for a way to programatically open the "New Document" dialog in Word 2007. It is the same one you get when you select File->New . You can also open it using the FileNew macro or the "New..." menu command. However, I have been unable to find a way to do this programmatically.
I have tried:
Application.Run MacroName:="FileNew"
and
Dialogs(wdDialogFileNew).Show
and
CommandBars.FindControl(ID:=5746).Execute
but both of these open the old dialog, not the new one that word 2007 uses.
If a 'real' VBA command exists for open that dialog, I can't find it. However, I did find this utterly lame workaround via some quick googling:
SendKeys "%"
SendKeys "F"
SendKeys "N"
It does what you want though! Found it here http://www.eggheadcafe.com/software/aspnet/32228837/new-file-dialog-in-word-2.aspx
You could get the Command ID for the button and execute it?
Dim c As CommandBarControl
Set c = CommandBars.FindControl(ID:=18)
c.Execute
Control ID 18 is the word application ID for the New... button.
I think that you can just use:
Documents.Add
without any parameters.

How to get selected text in VBA

I have a macro that changes the selected text, and I have it assigned to a button.
It works perfectly when i run it directly from visual basic, but when I click the button, the button gets the focus and my text is no longer selected so the macro change the selected element to (button).
How can I select the text and run the macro by clicking on the button and still have the text selected?
The way to do this is to set the set the TakeFocusOnClick property of the CommandButton to False. Here are is the code I use.
Private Sub CommandButton1_Click()
Dim Sel As Selection
Set Sel = Application.Selection
If Sel.Type <> wdSelectionIP Then
MsgBox Sel.Text
End If
End Sub
Is the button embedded in the document? You may need to put it on a form that loads on top of the Word window or in a menu/toolbar, so that clicking it does not affect the selection in the document itself.
Edit:
I think you can use Application.Selection.Previous to get at what you need. You could use this to restore the selection after the click event, or to act upon that section of the document, or both.
I assume that this is available in previous versions of Word, but have only confirmed its presence in 2007.
You need to change TakeFocusOnClick to "False" in the Button Preferences.