MS Word RibbonX How can I dynamically populate a combobox when a dotm file opens? - vba

I'm a little stumped here. I'm diving deeper into designing ribbons for MS Word 2010, and I came across something new: populating comboboxes on the fly. In the image below, you can see...
...that I'm a dude who likes music while he works, just like any other dude. Problem is my list of playlists changes from time to time, so I don't want to hard-code that list into my ribbon's combobox. I can easily hard-code it, but I want this thing to be dynamic. And so, in my ribbon code:
<comboBox id="cmbPLaylist" label="Playlist" getItemLabel="Document_Open">
<item id="none" label="None"/>
</comboBox>
I have left only one item, "none," which is fine if I want the music player to launch with no playlist loaded. But what if I want a playlist to automatically load?
First, from my Google and book research, I've determined that I need to have a getItemLabel callback to populate the control. Is this the right way to go? But how do I run that automatically when my Normal.dotm loads? I'm having problems running this thing in the Document_Open event, and I've been reading online that I'm not alone.
My problem is a bit threefold: first, I'm really new at using these predefined callbacks like getEnabled, getItemLabel, etc. The callback territory is a very new territory for me. Second, I've never used a combobox in a ribbon before. Three, I've never dynamically populated a combobox in a ribbon before. I might be trying to bite off more than I can chew at once, but can anyone point me in the right direction?
My code so far, inserted into my Normal.dotm Document_Open event, is such:
Private Sub Document_Open(control As IRibbonControl, ByRef label)
Dim ListOfPlaylists() As String
ListOfPlaylists = GetPlaylists()
ListOfPlaylists(UBound(ListOfPlaylists)) = "Random"
End Sub
After this, I'm stumped. As you can see, I'm not sure how to tell MS Word, "Hey, MS Word, insert this value into the combobox list!"
Maybe it's my newbness at this whole thing, but when I Google for an answer, I'm not seeing it in the code. So any help is appreciated. Thanks!

I actually did some fiddling and almost stumbled on the answer. I put this in my code and it seems to work just fine now:
Sub drpPlaylists_getItemCount(Control As IRibbonControl, ByRef drpPlaylists_itemCount)
drpPlaylists_itemCount = UBound(ReadDirectoryContents(MusicDirectory, "*.m3u"))
End Sub
I guess this gets launched every time the ribbon has to reload itself. But it's solved for now. Still have some things to study up on on when these callbacks get called, but I'll figure this out. Thanks for the help!

Related

MS Access: Repair broken button/control/vba

is there some way to repair broken functionality in ms access, like recompiling the thing?
I have a button on a form that is supposed to run vba code when clicked. The button no longer works, after like a couple of months. Except for the button getting focus nothing happens. I have tried the compact and repair option in access with no luck. The code works fine btw. I have made a new random button somewhere else on the form and literally copy pasted the vba code from the on_click event of the broken button to the new one, and on the new button it works perfectly. No problems at all. The old one, however, nothing. No matter what I do. I deleted the vba code in the on_click event, saved, closed and opened access again, then reentered the code. Still nothing. I tried removing certain functionality from the button by removing line by line, trying to see if any one line is the issue. Nothing again, no errors are being displayed, I don't think the button even runs the vba code at all.
In case someone wants to look at the code:
Private Sub Befehl107_Click()
[NavigationSubform].Form![stoffe_abfrage subform].Form.FilterOn = False
[NavigationSubform].Form![stoffe_abfrage subform].Form.Requery
Me.FilterAlleLink = "Alle"
Me.FocusDummy.SetFocus
End Sub
The button basically turns off the filter on a subform, requeries the form, sets the value for a textbox on the local form, and then gives focus to another textfield. Nothing complex really. I have 3 more buttons of the same type on my form that manage different filters for the same subform with almost identical code, they all work fine. Just this one broke.
Again, the code works fine if I make a new button, but on the old one it does not do anything.
Obviously I can just replace the button, but that would be annoying if down the line there is other functionality referencing it etc. So I was wondering if anybody encountered a similar problem and has a solution. Given that I have only worked with access for like 2 months total and already have this issue I hope there is some way to repair this more quickly, since I am now assuming that this will happen more frequently. Not sure though, maybe someone with more experience knows if this is common.
Edit: I just checked if literally copy pasting the broken button itself works and yes. I copy paste the broken button somewhere else, then copy paste the vba code from the broken button to the copy, and the copy now works...

Accessing controls that were moved

I am returning to coding after a long hiatus, and have come across a tiny issue that has me baffled. What I'm trying to do is simple, and it worked perfectly until I copied the controls from the winform and placed them in a tab control.
Very simply, I have a yes/no combobox with a conditional statement making a date time picker visible if the combobox is set to "Yes". Since moving the controls into a tabcontrol (The project had so many controls, I had to condense them for space). What's really crazy is I have a subroutine that resets all the controls in the project, and it still works.
when it worked, it looked like this:
If cmbAssessment.SelectedIndex = 1 Then
dtpAssessment.Visible = True
End if
(TabControl is named tcNursing and form is frmNursing)
I just can't figure out how to reference these controls inside the Tab Control.
It seems like solutions from Googling the issue are very complex. I'm finding it hard to believe this is
a complex problem. Thank you for ANY assistance at all!
As it turns out, Jimi, you were on the right track. There was nothing obviously wrong with the event handlers, although deleting them and re-adding them solved the problem. Thank you all for your help in this matter!

Word VBA: Make macro easy to run

I've made a form-letter document with a macro that performs the mail merge. I don't want the user to have run it from the menus, and I want this to be portable. If there's a way for a button to appear on each user's ribbon or quick command menu, I'm not familiar with it.
So I put a button in the document itself. Unfortunately, every form-letter created has the same button in it. I suppose I could write the code to delete every one, but I think that would be slow.
Is there a way to assign a shortcut key to an existing macro, and have it reside in the document?
I had to implement something pretty similar to what you were referring to some 10 staff. My solution (by no means as portable as desired) was to export the macros and forms from my Normal template to the other users, I coupled this with Ribbon customization and it worked well. Unfortunately, when a change was needed, I had to trudge over to everyone's machine individually.
I would suggest you stick with your solution of deleting button after the merge is complete. Here's some code to help with that:
Sub DeleteCommandButton()
For Each o In ActiveDocument.InlineShapes
If o.OLEFormat.Object.Name = "CommandButton1" Then
o.Delete
End If
Next
End Sub
Good luck, hopefully this helps.

How to Make Exception Handling on VBA Excel Form Combobox?

I have this userform that i'm developing for my report formatting task. My whole code is finished, there are only 2 problems that i need to work on.
Making my VBA application work on every excel file that user choose
Exception Handling for ComboBoxes on my form.
(Edit: I hope trying to get attention of people who are interested in this question to another stackoverflow.com topic is not against forum rules)
You guys can reach first problem's topic from here: (There is already one suggestion but i'm little bit confused. So it would be super for alternative suggestions) http://bit.ly/VnF3cK
And about my second problem, when i click empty place of combobox, i can type whatever i want, but i want to restrict it, so users can only choose values [1-5] that i put inside of combobox. How can I achieve this?
In answer to your secondary question, you need to change the ComboBox's Style property to frmStyleDropDownList

VBA Status Bar

I am working on a Word VBA macro app for 80 or so users. The office has high staff turnover, so training suffers, and so one of the self imposed requirements for this project is comprehensive, friendly documentation. However, to supplement this, and to save newbies having to open up a 100 page document when they want to try something new, I want a status bar on every userform (there are five) that provides contextual help. I find tooltips annoying.
I don't have a lot of experience, so I was wanting to
Essentially, I have a file containing every status string. (This is currently a text file, but I was wondering if I should use a spreadsheet or csv for ease of editing by other staff in future.) Every control has a MouseMove event which refers to a function: getStatus(cID) that opens the file, grabs the line and displays it in the status label. It also grabs a few parameters from the same line in the file, such as whether the label is clickable (to link to a page in the help file), and what colour the label should be.
So a few questions really:
Will the application be slow if a userform is constantly referring to a file? It feels fine to me, but I've been in it far too long, and I'm the only user accessing that file. There will be 80 constantly accessing it.
Is MouseMove over a control the best way? Should I instead use co-ordinates?
Most importantly (in terms of me having to do as little work as possible) is there some way to do this so that I do not have to have a MouseMove event on every single control? I have a good few hundred or so controls, each with their own identifier (well, not yet, but they will if this is the only way to do it). Maybe when the form loads I could load ALL the possible status lines so they're ready for whenever the control is moused over. But then, maybe the loading time is negligible?
Appreciate any ideas or thoughts - especially if VBA already has a whole range of functions to do this already and I'm just trying to reinvent the wheel. I can't use the application status bar, because the user rarely sees the application itself.
Thanks!
EDIT:
It is for both data entry, clicking around and a bit of document generation.
It is a controlled environment so macro security issues aren't a big concern for me - and if something goes wrong it's someone else's fault or problem :)
Is this data entry app or do they just click stuff? Because often the field with focus is different to the item the mouse is hovering over, this can cause a lot of confusion.
Constantly reading from a file is a huge waste of time and resources - it is much better to load them only once into an array or collection when the form is loaded.
On MouseMouse event is better than coordinates because you can move things around without worrying. It's a lot of code but you should be able to generate most of that if you have a list of control names because the code should be identical.
ie
Sub Control_MouseMove()
DisplayStatus(Control)
End sub
I would consider the StatusText property and ControlTipText property of controls for this kind of help.
StatusText
This example sets the status bar help text for the form field named "Age."
With ActiveDocument.FormFields("Age")
.OwnStatus = True
.StatusText = "Type your current age."
End With
ControlTipText
This can be assigned from the property sheet for the control.
Private Sub UserForm_Initialize()
MultiPage1.Page1.ControlTipText = "Here in page 1"
MultiPage1.Page2.ControlTipText = "Now in page 2"
CommandButton1.ControlTipText = "And now here's"
CommandButton2.ControlTipText = "a tip from"
CommandButton3.ControlTipText = "your controls!"
End Sub