Cannot edit Excel Menus using VBA - vba

I'm trying to create my own menu in Excel's (2010) Ribbon.
I wrote the following Sub to create a 'test' menu but I cannot find any evidence that the menu was created other than it being listed in the Controls Collection.
I feel like I've done everything correctly especially since I looked up bunch of code samples (example, this does not work for me either) from other people as well.
I guess my question is twofold, does the code below produce a new menu in YOUR Excel, and if so any idea why would this fail on my computer?
Sub CreateInterface()
Dim Controls As CommandBarControls
Dim CmdBar As CommandBarControl
Dim NewMenu As CommandBarControl
Set Controls = Application.CommandBars("Worksheet Menu Bar").Controls
Set NewMenu = Controls.Add(Type:=msoControlPopup, Temporary:=True, before:=Controls.Count)
NewMenu.Caption = "&Test"
NewMenu.Visible = True
For Each CmdBar In Controls
Debug.Print CmdBar.Caption & "|" & CmdBar.Tag
Next
End Sub
EDIT:
Thanks to D.O. I can see that the code is working it just isn't creating a new menu in the ribbon (like the default ones (Home, Insert, Page Layout, etc.)).
That being said to the best of my knowledge this was supposed to create a new menu, not an item in one of the menus, how can I actually create a new menu?

To the best of my knowledge, the old (pre-2007) CommandBars and Controls way is obsolete enough that it only adds buttons to the "Add-ins" ribbon tab. This is for backward compatibility with old Office solutions, so that they can still be used (somewhat) under 2007+ Office apps.
The way forward (if you plan on using Office 2007 and up) is to modify the ribbon itself. It's a completely new paradigm, and real-time customizations are done in a completely different way, but if you get the hang of it (it's not that difficult) it is quite useful.I suggest Googling some ribbon tutorials, and using the "Custom UI editor" tool to add your ribbon code to your Office files. Ron de Bruin's web site is always a good resource for more information.

Related

Copying the content of multiple UserForm textboxes to clipboard

I built a Microsoft Word template with a userform containing textboxes. I want to copy the data of two textboxes into the clipboard separately upon submitting the completed userform. Just to clarify I want each textbox to have its own entry into the clipboard for easy access in the future.
Below is the code I'm using but when I repeat the procedure it replaces the first textbox in the clipboard with the second. Any help would be greatly appreciated.
Sub ComandButton_click()
Dim clipboard As MSForms.DataObject
Set clipboard = New MSForms.DataObject
clipboard.SetText Me.TextBox1.value
clipboard.PutInClipboard
End Sub
EDIT (additional background):
For accessing the information put on the Clipboard I created a custom QAT. One of the buttons on the toolbar opens the clipboard so they can easily begin to paste the name they need at any one time. Most reports involve two people so I want to send the value of two textboxes into the clipboard as if you individually selected and copied each name. My first idea was to assign a keyboard shortcut to the value of the two textboxes but I settled on utilizing the clipboard tab.
Given what you've told us, I'm guessing you're calling up the Office Clipboard. This is different from the Windows Clipboard, which is what your code is using. The Office Clipboard is not exposed to the developer (for internal, MS use only). There is a way to put information on it, but I'm uncertain how reliable it is; YMMV (your mileage may vary).
The code for your UserForm to call a procedure responsible for writing the information:
PutContentOnOfficeClipboard Me.[textBoxName].Value
PutContentOnOfficeClipboard Me.[textBoxName].Value
The procedure writes the text to the end of the document, copies it, then deletes again. In my tests, this all shows up on the Office Clipboard:
Sub PutContentOnOfficeClipboard(s As String)
Dim rng As word.Range
Set rng = ActiveDocument.Content
rng.Collapse wdCollapseEnd
rng.Text = s
rng.Copy
rng.Delete
End Sub
If it turns out to be unreliable, then you need a different approach. Here are some thoughts on alternative methods...
A taskpane is optimal as it makes the information visibly available users. Unfortunately, task panes aren't available to the VBA Developer. They can be used from a VSTO solution or an Office-JS solution, but that may be more effort than you're willing/able to put into the project (steep learning curve, enterprise deployment requirement, etc.)
Besides keyboard shortcuts (which aren't a bad idea to have in addition to any visible interface):
A dropdown list on the QAT or the Ribbon
Two buttons on the QAT or the Ribbon
Modify the Right-click (context) menu with two controls to which you can write the names as needed. (Requires call-back macros to make the dynamic update)
Save the entries as AutoText/Building Blocks, possibly to use in conjunction
with a BuildingBlock content control (which can be filtered to show only your entries; AutoText can also be used selectively from the keyboard).
Make a small, non-modal UserForm with buttons. You could optionally use the Windows API to make it "always on-top".

MS Office - ActiveX Buttons switching places

There are several instances of this problem, but this one is predominant. This is in relation to updates (our most notable problem child being KB2726958). We have a Leave Spreadsheet that looks like this:
Leave Spreadsheet example
By pressing the grey Leave button, you end up here:
Leave Word doc
All the programming for these is written in VBA (i've never worked with VBA before, I can understand it to a degree).
Now, the issue is that using the ActiveX button in the 'Leave Spreadsheet example' causes the 2 buttons 'Send by Email' and 'Save' to switch functions; Send by email attempts to save and save opens up Outlook and creates the email message.
Both functions have completely retained functionality, just on the wrong buttons.
The thing I find weird is that a hyperlink to the very same file works; the buttons aren't switched and have full functionality. The only hint that I have towards resolution is that when using a hyperlink, it's directly opening the file. When using the ActiveX button, it seems to be creating a new file based off the file it's linking to. For example, the hyperlink directly opens C:\Report.dotm but the ActiveX button opens Document1.doc with a template based on Report.dotm.
I'm considering that maybe the activeX button is opening up Word with an incorrect extension? But i'm not sure how to figure this out (code below shows that the linked file on the activeX control is a .dotm).
What further throws a spanner into the mix is that it only affects some computers... Considering on-site we all use the same type of PC with the same image... :(
My question is, does anyone know why they may be swapping? They're located on the same network drive albeit different directories. They require the same permissions to access. The code for the buttons is as follows:
Excel Button:
Private Sub CommandButton1_Click()
' This button links the excel spreadsheet to the word doc
Dim wrdApp As Object
Dim wrdDoc As Object
Dim i As Integer
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Add("\\networkdrive\directories\Request for Leave.dotm")
End Sub
Word buttons 1 and 2:
Private Sub cmdSend_Click()
' This is the code for the button 'Send by Email'
MsgBox "Send the following email to your Team Leader/Line Manager", vbInformation
SendDocumentAsAttachment "", "IPL Request for Leave"
End Sub
Private Sub cmdSave_Click()
' This is the code for 'Save'
modSend.SaveLeaveForm
End Sub
Please Note: The comments above are not in the code in VBA, i've written them in myself in this question to provide clarity.
Troubleshooting that i've done:
Removing all .exd files
Running the MS Hotfix (removes all .exd files in a GUI)
The next step would be to try running all 6 patches related to fixing ActiveX controls with the particular patches we've done to see if that fixes the problem. The reason I haven't done this yet is because of ITIL (Change management) although I may try testing this later today.
What is the outcome i'm after?
Ideally, I want to understand what is causing these buttons to, from what it looks like, swap their functions. I have different scenarios of button swaps, some of which are remedied by removing the .exd files, and some that aren't.
By understanding what is happening, I hope that I can apply the knowledge to the other scenarios (same problem, different coding).
Then, I'll be able to document my findings so that when we perform the next round of patching that is known to break ActiveX controls, my organization will know how to deal with it.
So the patch mentioned below has fixed this issue. There's still some other issues that I need to test this patch against, but I definitely should have started there. Lesson learnt.
From my work email:
I’ve just tried using the patch related to the ActiveX controls breaking, KB2920754. I’ve used it on two PC’s here in the training room; both had different issues:
- The first one had buttons that had switched around (save attempted to email, email attempted to save)
- The second one couldn’t use the buttons at all.
This patch cured both w/o requiring a restart or logging out and back in. I didn’t remove any .exd files, either.
It does state, however:
“Important For this fix to be fully effective, you also have to apply the other patches for Office 2013 that are listed in the "Resolution" section of the following Microsoft Knowledge Base article”
There are 6 in total.
Patches:
1. KB2920754 – (the one I’ve used successfully)
2. KB2956145
3. KB2956163
4. KB2965206
5. KB2956176
6. KB2956155

Modify Context menus

I have the following code to add a menu item on the Word 2013 right click menu:
Sub CreateMenuItem()
Dim MenuButton As CommandBarButton
With CommandBars("Text")
Set MenuButton = .Controls.Add(msoControlButton)
With MenuButton
.Caption = ChrW(&H2714) '"Correct"
.Style = msoButtonCaption
.OnAction = "InsertMark"
End With
End With
End Sub
The issue is that the new menu item is not displayed when I right click on a Text box or a table.
How do I remove a menu item that is not used by me, for example "Translate"
Please Assist
Thinus
Word provides a completely different CommandBar object for each context-sensitive menu, rather than dynamically changing the contents of a single menu. That means you need to make the change for each context in which your command should appear.
You can generate a list of CommandBars by looping the collection and writing the various properties out (for example, in a new document). Then you have to inspect that list, pick out the likely candidates, run your code then see if you guessed correctly.
You want to be very careful where these changes are SAVED in Word. If you don't specify it specifically, Word could save the change in one place and delete it (assuming you "undo" your changes) in a different one. This can really muck up the Word configuration and make users extremely unhappy. So always use Application.CustomizationContext at the beginning of code that changes the CommandBars. The CustomizationContext can be any Document or Template object.
NOTE: The CommandBars object is actually deprecated as of Word 2010. From Word 2013 onwards the context menus should be customized using Ribbon XML. Just saying... It's possible that in a future version your code will no longer work.

Excel Addins - Not Modifying Toolbar

I am looking for the best way to deploy Excel Macros to users. My goal is to make it super easy for end users to install and promote use by adding to the addin toolbar. I know that there are a number of help articles on this topic but couldn't find anything that covered this exact issue. Can you please help and excuse me if this is a noobie question. Please see below for replication steps for my issue.
I have added the code below as a worksheet event on "This Worksheet" of an excel macro file
I add the main code to a module that it references
I save this as an .XLAM in the addin roaming folder
I enable this as an addin in EXCEL 2013
After I install it adds the button to an add in tab
It works until I close Excel in which case the button disappears
It is still under active add ins but not on the toolbar
The code:
Option Explicit
Dim cControl As CommandBarButton
Private Sub Workbook_AddinInstall()
On Error Resume Next 'Just in case
'Delete any existing menu item that may have been left.
Application.CommandBars("Worksheet Menu Bar").Controls("Super Code").Delete
'Add the new menu item and Set a CommandBarButton Variable to it
Set cControl = Application.CommandBars("Worksheet Menu Bar").Controls.Add
'Work with the Variable
With cControl
.Caption = "Convert Survey Reporter Tables"
.Style = msoButtonCaption
.OnAction = "CMB_General_Table_Formatting"
'Macro stored in a Standard Module
End With
On Error GoTo 0
End Sub
Private Sub Workbook_AddinUninstall()
On Error Resume Next 'In case it has already gone.
Application.CommandBars("Worksheet Menu Bar").Controls("Convert Survey Reporter Tables").Delete
On Error GoTo 0
End Sub
If you want an alternative to using VBA to build the interface, I have previously deployed Excel add-ins (XLAM files) using some variety of Ribbon XML. This allows for very fine-grained control of the resulting interface and does not require you to work in VBA to build the interface. For most applications, I have found it is much easier to build the Ribbon components outside of VBA and then wire up the callbacks in VBA.
For the end user, I think this approach also delivers a better looking add-in since the resulting interface has its own Ribbon tab (or you can add to any of the existing ones) instead of being in the Add-ins Ribbon tab.
If you want to pursue this route, I highly recommend using the Ribbon X Visual Designer to build the interface and set callbacks. I have used it to build an add-in that had more than 50+ features accessible by buttons and other Ribbon form controls. It was fairly painless once I got going.

VBA PowerPoint Online Guide and How to Record a Macro

Could anyone recommend to me a good online guide to PowerPoint VBA? Also, does anyone has advice on how to record a macro in PowerPoint?
Microsoft remove macro recorder from PowerPoint 2007.
To view the struct of objects use Watch (Shift +F9) in object.
For Example
dim ppt as powerpoint.Presentation
set ppt =activepresentation
add watch to ppt to view the struct of object Presentation
otherwise
Add a new class module
in the class declare
Private WithEvents ppt As PowerPoint.Application
in comon module declare one instance from classCreate(default is Class1) using
set x= new class1
Now in module class you can get events for you presentation (in left combo up the code window)
Bruno Leite
Office Developer
To record a powerpoint macro:
In the menu bar, click on Tools
Mouse over Macro > and the submenu will be displayed
Click the Record button - a new toolbar will be displayed
Do your thing
Click the stop button on the new macro toolbar
Click on Tools->Macro->Macros. Find the macro you just recorded and click the Edit button. That will show you what was recorded. Make your modifications and click the triangular run button (or push F5) to run your code.
As far as an online guide, I usually think of a question and use Google or ask a question here on StackOverflow.com. I've been able to answer most of my questions that way, I haven't found a particular main resource for all things Powerpoint VBA.
Also, you can find answers that can help you by looking into VBA articles for other MS Office products - a lot of things that are not Powerpoint-specific (general VBA) will be the same as for the other products.