word vba combo box values not displaying although populated - vba

I am trying to populate Mail Merge Data fields from excel database and I am successful in pulling it, but when trying to add it in user form combo box, it is not displayed. Although combo box is populated with 3 blank rows. Screenshot attached.
Below code is written in Module1. Do I need to write anything in Combobox1_change() in userform as well?
For Each aField In ActiveDocument.MailMerge.DataSource.FieldNames
UserForm1.ComboBox1.AddItem
Next aField
Userform screenshot

You haven't told the userform what to add to the combobox. For example:
For Each afield In ActiveDocument.MailMerge.DataSource.FieldNames
UserForm1.ComboBox1.AddItem afield.Name
Next afield

Related

Creation of Excel Form using VBA and save it

I am trying to create a excel form with text and combo boxes using VBA where the data I enter in the form should get saved in a different worksheet. There should be a save button on the form for users to click on it so that the data gets saved. Is that possible. please help
It's certainly possible, can you be more specific about what part you're having a problem with?
The general timeline of events would be this:
Create UserForm and add textboxes, comboboxes and save button.
a. To learn how to populate comboboxes, check out these resources: How to add items to a combobox in a form in excel VBA?
Give each of your textboxes, comboboxes and save button a name in properties, so that they can be referenced in the code.
Add code to the Save Button event. To do this, go to your UserForm and double click on the Save Button, this will bring you to the code that will execute when that button is clicked, you'll be adding something like this:
Sheets("Example").Range("A1").Value = textbox.Value
To answer your comment:
You shouldn't have any problem adding arrays to Comboboxes like this:
Private Sub UserForm_Initialize()
ComboBox1.List = Array("One", "Two", "Three")
ComboBox2.List = Array("Four", "Five", "Six")
ComboBox3.List = Array("Seven", "Eight", "Nine")
End Sub
If you're having issues, make sure your code, in this case "ComboBox1... etc." is referring to Comboboxes that exist.

Set focus on ComboBox (ActiveX Control) after code execution

I have an Excel file with a combobox (name = "Combobox1"). After running a script (basically pasting the selected value in "the next row" of a column) I want the focus to be reset on the combobox at the end of the script, so doing allowing me to type the next entry in the Combobox without having to click on the ComboBox text field first.
This does the job in Excel 2013 but I would like to have it working in 2007 as well:
Combobox1.Activate
Anyone any idea?
Or:
I can replace the combobox with an in-cell dropdown list (data validation) and the same data validation as the one I have in the combobox at the moment, but then I have another issue:
For a ComboBox you can choose to have the dropdown list active, but for an in-cell data validation that is not the case, at least not if you want to be able to type in the cell after the list is shown with ALT+UP or
Application.SendKeys "%{UP}"
Any idea here?
If this combobox is on a worksheet and not a userform, then "Combobox1.select" should return the focus to the combobox.

Select linked cell, cell next to checkbox, or get the checkbox name

I have a document full of Checkboxes and I dont want to write specific VBA code for each checkbox because the file size needs to stay relatively small. What I am trying to do with my code is when the checkbox is checked, it automatically selects the cells next to it(not hard coded in using "Range") and then perform the rest of the programed VBA function.
How do I either get the name of the checkbox, select the linked cell, or select the cell next to the checkbox using some kind of "offset" property? I am completely stumped!
Thanks for your help in advance.
Use Form Controls instead of ActiveX Controls for Check Box.
Following code will not be work with check box from ActiveX Controls. Also, you need to assign macro to the checkbox, simply trying to run this code from VBEditor will give error.
Assuming all the checkboxes are on same sheet, select all your checkboxes and assign them same macro, something like this
Sub checkBoxHandler()
Dim shp As Shape
Set shp = ActiveSheet.Shapes(Application.Caller)
MsgBox shp.Name 'Name
MsgBox shp.TopLeftCell.Offset(1).Address ' 1 Rows below checkbox
ActiveSheet.Range(shp.ControlFormat.LinkedCell).Select ' Select linked cell.
Set shp = Nothing
End Sub
here Application.Caller helps VBA to identify which checkbox is being clicked.

Clear contents of combo box

I have an invoice spreadsheet with comboboxes to select a product which fills out the product number and name in the invoice. Then I have a clear button that clears the information out to do another invoice, but the combo box and the linked cells don't get cleared. I tried adding the code ComboBox2.Clear or ComboBox2.Value="", or DropDown2.Clear, but I keep getting a run-time 424 error object required. What am I doing wrong.
Sub ClearIncoive()
ClearIncoive Macro
'Clear the invoice
Range("G6:G9,F16,G16:H16,F17,G17:H17,F18:H28").Select
Range("F18").Activate
Selection.ClearContents
ComboBox2.Clear
End Sub
Any help?
I played around with a few things and got the same error you did, so I figured I was on the right track. The combobox I created, with Excel 2010, was a Form Control. I then created another combobox using the ActiveX Control and used
Sheets(1).ComboBox2.Value = ""
Note that I set the contents of the combobox to A1:A8, which contained the data for the combobox.

Allowing user to select text in word vba macro

In VBA for Word 2007, I want to be able to open a document, highlight sections of text and replace those sections with fields linked to a docvariables. The process would be:
Open document.
Select text.
Select docvariable from list.
Insert field linked to selected docvariable.
Repeat steps 1-4 as required.
There is no way to know beforehand what the text to be selected is or which docvariable is going to be linked to which field or how many times these steps are going to be repeated.
Only with Microsoft could the most absolutely fundamental, simple task of allowing the user to make a selection at run-time and pass this selection back to sub-routine be so tortuous and surreal. I have spent 2 days trying to figure this out. If anyone can help, I will name my next child after you.
I think "tortuous and surreal" is a misconception.
Create a small form with a dropdown (named "selVarName", for example) that lets you select all document variable names available. Link the form to a custom button in the Quick Access Toolbar.
Upon clicking "OK" in this form do something like this:
Private Sub btnOK_Click()
Dim v As Word.Variable
Dim n As String
n = Me.selVarName.Value
With Selection
For Each v In .Document.Variables
If v.Name = n Then v.Delete: Exit For
Next v
.Document.Variables.Add n, .Range.Text
End With
End Sub
And this has bells and whistles already. You can do additional checking like "no text selected", for example.