Log in

View Full Version : Put text from textbox in userform into normal textbox in document -- MSWord 2010



Xenowyn
08-06-2014, 03:11 PM
I'd like to take text that a user enters into a textbox on a userform and put it into a regular textbox in the document. I've tried using

ActiveDocument.Shapes(1).TextFrame.TextRange.text = UserForm1.TextBox1.Value

but then I get an error because of unmatching object types. Is there even a way to do this? If not, is there any other way to take text from a textbox in a userform and insert it into a document such that the inserted text will be able to stay in the correct relative spot even if the document is edited? (The normal textboxes in the document are currently aligned in line with text to account for that)

macropod
08-06-2014, 11:41 PM
The code you posted works just fine for me... What error message are you getting? Are you sure you're inserting into a textbox and not into a text formfield or a content control?

Xenowyn
08-07-2014, 07:26 AM
By textbox do you mean an ActiveX textbox? Because I was trying to insert it into a normal textbox that you draw into the document from the insert menu. Sorry I don't know much, but what is a text formfield? Can't seem to find much on it around the internet.

Oh and the error is "Run-time error '5917': This object does not support attached text."

macropod
08-07-2014, 03:15 PM
In that case, how many shape objects are there in your document ahead of the textbox? It seems you're addressing the wrong one.

Xenowyn
08-07-2014, 03:49 PM
Unfortunately I don't quite remember since I've added some since then but I'm pretty sure it was the only one when I tried that at first. Although if I had added some then deleted them before adding the one I was talking about, would it have indexed the newer one at a higher number? Is there a way to view what the index of a shape in the doc is?

macropod
08-07-2014, 04:24 PM
Rather than trying to retrieve the textbox's index, which can change, you'd do better to name it, then refer to it by name. For example, you could do that with code like:
Selection.ShapeRange.Name = "Target"
then, with your userform, you'd use:
ActiveDocument.Shapes("Target").TextFrame.TextRange.text = UserForm1.TextBox1.Value

Xenowyn
08-08-2014, 07:35 AM
Thanks! I got that to work with one. But if I have multiple textboxes, how would I select each individual one for naming?

macropod
08-08-2014, 03:59 PM
But if I have multiple textboxes, how would I select each individual one for naming?
Umm, by selecting each one of them in turn...