0
votes

I want to write a Word VBA Macro that automatically fills in ActiveX control fields in a Word document using data contained in an Excel file. I managed to open and retrieve values from the Excel file, but I am getting stuck on changing the values of the ActiveX control fields.

How can the value of ActiveX control fields be accessed and modified in VBA ?

By "field", I means both text fields, radio buttons and check boxes (e.g. checking/unchecking the box in the VBA Macro).

A link to a page with a clear explanation of how to work with ActiveX control fields in VBA is also welcome, as I haven't been able to find any useful material so far (on this particular point).

2

2 Answers

0
votes

For exemple you can do :

For Label

UserFormName.LabelName.Caption = "Test" 'Label's text will be Test

For CheckBox

UserFormName.CheckBoxName.Value = True 'CheckBox Checked

For TextBox

UserFormName.TextBoxName.Value = "Some Text" 'Text in TextBox will be Some Text

For Radio Button

UserFormName.RadioButtonName.Value = True ' Radio Button activated

Is it what you asked for (Not Sure that I understood your needs so please leave Feedbacks so I can improve this answer) ?

Edit :

To get the name :

  1. Select the item
  2. read the name in the property window

For exemple : enter image description here

0
votes

While ActiveX controls on the document surface can be accessed via the ThisDocument class module, this only works with controls in that document.

If the code should work with controls in other documents, or documents created from a template, then a different approach is required.

The following code sample can be in any code module. It accesses the ActiveX controls in any document via their "OLE interface".

The control can be either an InlineShape (in-line with the text) or a Shape (text-wrap formatting) - just use the appropriate collection. OLEFormat.Object picks up the control, itself.

Sub WriteToActiveX()
    Dim ils As Word.InlineShape
    Dim olef As Word.OleFormat
    Dim tb As MSForms.TextBox

    Set ils = ActiveDocument.InlineShapes(1)
    Set olef = ils.OleFormat
    Set tb = olef.Object
    tb.Text = "Test"
End Sub