Click/DoubleClick to open a Userform on Word - vba

I was just creating a template on Word which uses content controls and I have a userform to help auto fill the data which is activated via a command key. You know how on excel VBA you can click a cell to make it refresh or open up a userform, or perform a series of tasks. On Word, can you double or single click a Content Control per say and have a userform pop up?
I think it would be pretty neat to be able to do that, open for any ideas that I should try out.

Unlike Content Controls, ActiveX controls have Click and DoubleClick events. You can add an ActiveX control (e.g., a button or a label) and use its Click event to do whatever you want:
And then simply, use something like this:
Private Sub lblTest_Click()
MsgBox "You clicked on a label."
End Sub
Private Sub btnTest_Click()
MsgBox "You clicked on a button."
End Sub
Result:
Hope that helps.

Related

Using VBA to allow a checkbox to hide/show content in Microsoft Word

I've gone through a number of tutorials and instructional videos trying to achieve my intended result of simply allowing a checkbox in my form to hide content when selected, or re-show it when being de-selected, but nothing seems to be working.
Currently, I've created a bookmark for the content I want hidden, and try to call his this in VBA with the following statement - which a number of resources indicated as the solution:
Private Sub CheckBox1_Click()
ActiveDocument.Bookmarks("bookmark").Range.Font.Hidden = CheckBox1.Value
End Sub
But despite doing this, selecting the checkbox has no affect on the content.
Is there something additional I'm missing (I'm using Microsoft Word 2013).
Your code worked fine when I tested it, but I ran into a few issues since I've never messed with userforms/checkboxs in Word VBA and I suspect you may have the same.
For instance, the first thing I did was create Insert --> Module. This is incorrect. What you want to do is Insert --> Userform then drag a checkbox over from the ToolBox
https://smallbusiness.chron.com/use-check-boxes-word-54673.html
Then double click the checkbox to bring up the "module" it would run, notice there is no module in the side pane! Edit the module, then go back to the userform and press F5. You should be presented with a checkbox that will hide/unhide your text.
Here is my module:
Public Sub CheckBox1_Click()
ActiveDocument.Bookmarks("bookmark").Range.Font.Hidden = CheckBox1.Value
End Sub
Here is an image:
Note: I didn't test how to insert the checkbox into the word doc, I'll leave you some of the learning!
Update:
This sub will make the CheckBox appear when run, but I'm not sure the logic you would use to call it, perhaps an event like opening of document?
Sub Loadform()
Load UserForm1
UserForm1.Show
End Sub
This could be called via a keyboard shortcut or event, but this will cause a "pop-up window". If you want an inform checkbox you may need to look into using this Legacy Form/Checkbox. I was following the URL from above but I think it's dated.
Update:
You could also add this easily to a toolbar, but that isn't likely what you want. I found the best way is to use a "field code" see --> https://word.tips.net/T001571_Assigning_a_Macro_to_a_Button_in_Your_Text.html
I was able to get this to work by pressing CTRL + F9 then typing { MacroButton Loadform ClickMe} then clicking this text. This may be the best bet as using an image seems not to be a great idea.. (https://answers.microsoft.com/en-us/msoffice/forum/all/using-graphic-with-macrobutton/a9c1ef3b-cf1f-48ba-92a8-c44bffcdc131) & (http://www.addbalance.com/usersguide/parts/macrobutton_fields.htm#Different_behavior)
Gif Example:

How to make refresh function in VBA

I am using Access Database to make a program.
Here is the problem:
After I enter the data in textbox, which is in a blue boxes, and click 'Add Data' button, data move to the ListBox, which is marked with orange box. But I should press 'F5' button(refresh) to see the data. I want to see the data immediately after I click 'Add Data' button. Is there any way to do that?
Any comments would be greatly thankful(It would be nice if you can share your code)
Add ListBox.Requery to the button's click event right after new data has been added to the ListBox.
Private Sub YourButtonName_Click()
'Add data
Me.YourListBoxControlName.Requery
End Sub
requerying the form is the simplest way probably, but with no idea of what the button does, how your data is structured, how the form is structured, i.e. is the listbox bound or unbound, so cant really say, if F5, is doing it, then just look for the VBA method for refreshing the form.

Drop-Down List Detached From ComboBox - Excel VBA

The drop down list of a ActiveX ComboBox detaches whenever I scroll down the page. How
can I fix it so it won't move?
Here's the VBA code designated for the comboBox.
Private Sub ComboBox1_Change()
ComboBox1.ListFillRange = "DropDownList"
Me.ComboBox1.DropDown
End Sub
If you switch out your ComboBox to the one from the Forms toolbar, then it collapses the list as soon as you start scrolling. I think the "detaching" of the dropdown is the default behavior and I don't think you can influence that programmatically (as in here, too).
See this post on the different options for the comboboxes:
Run Macro When ComboBox is Clicked
Another option is to disable the mouse scroll, as in this post:
VBA Excel Combobox: drop-down list scrolling issue

Clicking away from userform open text box VBA Excel

I am not very good with userforms so hopefully this is an easy question.
I have a userform in a spreadsheet that opens with
UserForm1.Show False
To ensure excel can still be used while it is open. What I would like to happen is that whenever you click away from (but not close) the userform a text box is opened asking if you want to maintain the edited values or not. I can't figure out how to initiate more code when the userform is no long the point of interest. Usually items on userforms have enter and exit which I believe would do what I need but I can't find the equivalent for the actual userform.
Any advice would be appreciated.
To allow a user to enter values into excel while a form is visible, you will need to update the ShowModal property on the userform to false.
Or as you are showing the userform, you can show it vbModeless.
UserForm.Show vbModeless

Excel - Display ComboBox DropDown by VBA

I need a workbook to display the Combobox List Dropdown when it opens.
The combobox in the Workbook is a form control, so a shape.
Cant seem to find the associated property.
If you are using ActiveX Controls then see the below, else if you are using Form Controls then replace them with ActiveX Controls if you want the drop down to happen via code. The below code will work for both ActiveX Controls in a Form as well as Worksheet. If the Control is on a worksheet then change ComboBox1.SetFocus to ComboBox1.Activate
Two ways I can think of...
Using a simple command
Tried And Tested
Private Sub CommandButton1_Click()
ComboBox1.DropDown
End Sub
Using Sendkeys. Sendkeys are unreliable if not used properly.
Tried And Tested
Private Sub CommandButton1_Click()
ComboBox1.SetFocus
SendKeys "%{Down}"
End Sub
SCREENSHOTS
I have had plenty of crashes with .dropdown but find some success with
the SendKeys...
I consider best
UserForm combo box is as Above by Siddharth Rout
ComboBox1.SetFocus
SendKeys "%{Down}"
for some Combo boxes on a worksheets
CB.DropDown Is enough
.. Just as well as they have no setfocus or activate