1
votes

I am using a VBA userform to randomly generate values to be sent into a Word document to provide random questions on various math topics (for my students).

This seems to work well except that when I exit the userform (me.hide) and the values are sent and focus is back in the document, the previous values are still briefly visible every time I move the mouse.

This only happens for an instant. The correctly sent values appear if there is no mouse (or window slider) activity taking place. If I view another application and do some activity there and then return to the Word document the display is fine as well.

The flipping of values is visually annoying. Has anyone out there encountered a similar issue?

To reproduce:

Create a Word document with an ActiveX Textbox and button. The button activates a userform which also contains a Textbox and a button to send the Textbox value back to the Textbox in the document.

In my case, when the button in the document is clicked the form opens and the value does get returned to the Word document, but when rolling the mouse or moving the slider at the edge of the window the previous value in the text box briefly flickers into view. Here is the code from the document button:

Private Sub cmdOk_Click()
  ThisDocument.TextBox1.Value = TextBox1.Value
  Me.Hide
End Sub

I am using Word 2016 and Windows 10.

I tried to use the "new" Word textboxes but I do not know how to communicate with them from a userform. It is so simple using the "old" text boxes.

1
Based on the repro steps you gave (which I've integrated into your question) I can NOT see what you report. If you include information about the code to call the UserForm and the versions of Windows and Word involved, I'd be happy to try again. - Cindy Meister
A few remarks: 1) ActiveX controls were designed for UserForms, and are only incidentally enabled for the document surface. Due to this fact, their behavior can be "odd" sometimes. Form fields or content controls were designed for Word and would be more stable. 2) Word puts high demands on graphics card drivers. Sometimes, updating the driver or turning off hardware acceleration for the driver can make a difference in on-screen rendering quality. - Cindy Meister
I am using Word 2016 and Windows 10. I tried to use the "new" Word textboxes but I do not know how to communicate with them from a userform. It is so simple using the "old" text boxes. - George
You mean content controls? - Cindy Meister
Hello Cindy. Yes the content controls on the Developer tab. I could not figure out out to communicate with the those controls so I went to activeX solution. Is there a better way? Thanks. - George

1 Answers

1
votes

ActiveX controls were designed for VBA UserForms. It's possible to insert them on the surface of a Word document or Excel spreadsheet, but they don't always behave optimally. Content controls (and the legacy form fields), on the other hand are designed for the surface of a Word document.

In order to write to a content control you can use code like this:

ActiveDocument.SelectContentControlsByTitle("name").Item(1).Range.Text

Closer to what you're trying to do with identifying the content controls could be:

ActiveDocument.SelectContentControlsByTitle(TextBox1.Name).Item(1).Range.Text = TextBox1.Value

Why it's so complicated: The team that designed content controls wanted to avoid the problems people ran into with bookmarks and form fields, that a name could not be duplicated in a document. It's possible to give the same name to multiple content controls. So it's not possible to identify a content control using Document.ContentControls("name").

Instead, Document.SelectContentControlByTitle needs to be used, which returns an array of content controls. If there's only one (or if you want only one), then you can add on .Item([index]) to get the single content control directly.

Rather than type in a static name, you can query the UserForm's textbox control for its name, if that makes things easier for you.