I am trying to develop a dynamic user form in a Word document that will create an ActiveX radio button with a generic name and then rename that created radio button to make it have a specific number based on its location in the form.
So far I believe I have all the components to write the code because I am able to create the button. I create the button by using VBA to import a quick part that has the radio button with a defined radioButtonX name and I am able to change the name property of that radio button to radioButton1 for example. The problem that I am running into is that my code will insert the quick part with the generic radioButtonX name and then fail to change the radio button name because it says that radioButtonX does not exist. However, I can manually run the second part of the code after radioButtonX has been inserted and I am able to successfully change the radio button name giving me the end result that I am looking for in two separate steps.
I am a self taught VBA scripter for the last year so I am far from an expert at coding. A few of the strategies that I have taken so far were to:
Put in waits (thinking that pausing the script would give the script time to recognize the new radio button)
Separated the code into different subs having one sub create the radio button and then call a sub to change the name (hoping this would update VBA)
Separated the code into different modules having one modules create the radio button and then call a modules to change the name (hoping this would update VBA)
I was hoping for some advice, strategies, or sample codes that I can use to get VBA to recognize the new radio button that I have inserted while the code was running. Another thought that I had was maybe there is a command that exists that will have VBA refresh its knowledge of variables, but I have not have any luck finding something like that in my web searches to this point.
I tried to simplify the code that I was working on and pull out the code that would perform the function that I was requesting. Running the simpler code was successful and listed below.
Private Sub AddRadioButton()
Selection.TypeText Text:="TestRadioButton"
Selection.Range.InsertAutoText
ActiveWindow.View.ShowXMLMarkup = wdToggle
ChangeButtonName
End Sub
Private Sub ChangeButtonName()
ThisDocument.radioButtonX.Name = "radioButton1"
End Sub
For this script to work you must create a quick part or a radio button that is named TestRadioButton. I think now the root issue has to deal with timing that looked like it wasn't working because the script that this code came from is using a quick part with multiple generic radio buttons in it so the script executing does not recognize the new radio buttons while running.
Related
i want to run my code such that it can pause and allow user to filter a column and then the code continues to run. Is this possible?
I thought of using inputbox to prompt user to key in the name. However, this is not feasible because user may not be able to remember the exact name. It will be more convenient for the user to look at the drop down list and select the name he wants to filter.
Please advice me how can this be done. Thank you
You can't stop a VBA macro to wait until an action was done within Excel.
I see the following possibilities:
a) Create a small userform showing a dropdown. If you show that form modal (that is the default), the macro will pause and wait until the form is closed. However, you will need to implement code to fill up the dropdown and to set the filter by code.
b) End your macro and implement an event that fires when the user changes the filter. There is no "On filter change"-event in Excel, but there is a work around: Excel VBA Filter Change event handler. Put the second part of your code into a separate sub and call that from the event handler.
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:
Firstly I am new to vba, and in the course of learning have learned a ton from searching this site. Unfortunately this time no success.
I do not know how to tackle this.
My "home" userform has 6 command buttons with no caption to them (empty).
On pressing any one of these, a "names" userform is opened, with 10 command buttons - each is captioned with a different persons name.
I want the chosen perdons name assigned to the caption of the command button that triggered "names" to appear.
On each form I have placed a label. This label's caption changes to the caption of tge command button clicked.
Options I have considered:
1. Multiple multiple IF statements
2. Assigning variables - cant figure out what to assign
I can post my code, but as i do not know where to begin I don't think it would be to much use.
Managed to figure it out!
Made a module with if statments.
Called it from "names" every time a name is chosen.
By doing this no need for writing the same code over again.
I am adding several CommandButtons dynamically to my user form. To assign code, I used the answer of this question: Assign code to a button created dynamically
However, I need to determine which button has been clicked. They all do have different names. Therefore, my initial idea was to get the name of the clicked button in the CmdEvents_Click() procedure. However, I have not found a solution how to do that.
Does anyone have a solution how to trigger button specific events?
Add the Name of the new command e.g. like ctl_Command.Name = "name_" & i
Then in the CmdEvents_Click just use CmdEvents.Name.
Okay so I've been searching for a solution for a while but can't seem to find any answers tailored to what I need to do. Also this is my first post here so I'm sorry if I'm being to vague with what I need. Basically I'm creating a program that has a few text boxes on one form and I need to display the data in those text boxes into a list box on another form. There's a button at the bottom of the first form that allows me to switch to form 2 and also display the data in the list box. I can switch to form 2 but nothing shows up in the list box. I also have a few radio buttons and check boxes on form 1 that will affect which constant values will need to show up in form 2's list box as well. One other thing is that I can't just use a form load option for getting data in the list box, it has to happen with the button press because I will be switching between forms and from my understanding the form load option only works once when the form is loaded for the first time. Anyway here is part of my code for form 1 that shows the button click:
Dim strCustomer As String
Private Sub btnPlaceOrder_Click(sender As Object, e As EventArgs) Handles btnPlaceOrder.Click
strCustomer = txtCustomer.Text
frmInvoice.ShowDialog()
frmInvoice.lstCustomerOrder.Items.Add(Me.strCustomer)
End Sub
My first form is frmMain and my second form is frmInvoice. Can anyone please help me with what I need to do differently with this code and what exactly I need to have for my code for form 2 to make this work. Again I am somewhat new to this so I'm sorry if any of this seems vague or if I'm not quite posting this is the right way. Also I'm using VB.
The code you provided should work fine but you are modifying the ListBox after you show the invoice Form. Since you are using showDialog(), the code that follows this statement will only be executed once the dialog form is dealt with (for example if you close it). This means that if you click the button a 2nd time, the ListBox should contain one item. If you switch the two lines so it looks like this, it should do what you expect it to:
frmInvoice.lstCustomerOrder.Items.Add(Me.strCustomer)
frmInvoice.ShowDialog()