PDA

View Full Version : typing into a textbox



caz1805
04-16-2006, 04:15 PM
I have a document and I was wondering was there a way that I could type text onto the document and it gets copied into the text box i have created at real time. Thanks

caz

Jacob Hilderbrand
04-16-2006, 06:40 PM
This is not something that you can easily do. Perhaps if you let us know what the end result you want to achieve is, someone may have a better way.

This may be close though. I set this up to take the text from the top left cell of a table and put it in the textbox. The table could have no border if you actually don't want a table, we just need some way to identify the text.

I am not sure how to get this to trigger when you type text though, but it will update when you move to a new place in the document (either with the mouse or arrow keys).

Ok, here is what you do to set this up.

In the ThisDocument Module:


Option Explicit

Private Sub Document_Open()

InitializeClass

End Sub


In a Standard Module:


Option Explicit

Dim Cls As New Class1

Sub InitializeClass()

Set Cls.App = ActiveDocument.Parent

End Sub


In a Class Module (Named Class1):


Option Explicit
Public WithEvents App As Application
Private Sub App_WindowSelectionChange(ByVal Sel As Selection)
Dim Txt As String

'Get the text
Txt = ActiveDocument.Tables(1).Cell(1, 1).Range.Text

'Remove the hidden table characters
Txt = Left(Txt, Len(Txt) - 2)

'Put the text in the Textbox
ActiveDocument.FormFields("Text1").Result = Txt

End Sub

TonyJollans
04-17-2006, 04:32 AM
Hi caz,

I've just looked at this, and the other questions you've asked, and I don't understand what it is you want at all. Particularly what sort of Textbox do you want, but more generally what are you trying to achieve?

caz1805
04-17-2006, 04:07 PM
Say I have created two textboxes in a word document called Text1 and Text2 and I am typing into Text1 but it is simaltaneosly copying everything I am doing into Text2. So really Text2 is a mirror image of Text1.

cheers caz

TonyJollans
04-17-2006, 05:02 PM
It may or may not be possible - it depends on what type of Textboxes they are. How have you created them?

I still can't see any real use for this behaviour. What is your ultimate goal?

caz1805
04-17-2006, 05:07 PM
Well i have created a textbox with the control toolbox, text box feature and would like to have it so that i could copy the text into the textbox and later i would like to change the original text but have what it was before in the textbox to reference to.

caz

TonyJollans
04-17-2006, 06:47 PM
With textboxes from the Control Toolbox you can do this:

Suppose they are called Text1 and Text2.
In Design Mode, double click on Text1.
You will be thrown to the VBA Editor, like this:Private Sub Text1_Change()

End Sub
Add a line of code in the procedure:Private Sub Text1_Change()
Text2.Text = Text1.Text
End Sub
It should do what you ask, but I'm not entirely sure I'd recommend this procedure.

fumei
04-17-2006, 11:17 PM
Unless you can explain WHY this is a desired thing to do, I have to agree with Tony. It seems very odd, and I would not recommend it either.

I will repeat Tony's question. What is your ultimate goal?

caz1805
04-19-2006, 04:00 AM
My ultimate goal is to create a simple language translator by using the autocorrect feature. Once the text is typed it is changed in the document but the autocorrection does not occur in the textbox. this means the user could view the original text and the translated text. I have done everything except for copying the text from the document to the textbox.

thanks again.

caz

caz1805
04-19-2006, 05:08 AM
So i have made it so i type into the textbox and it goes to a bookmark set onto the desktop, but the ouput at the bookmark is very wierd. Here is the code and the ouput.

Here is the code i have just got but with the same errors for some reason and underneath is the output.


visual basic code:
Private Sub TextBox1_Change()
With ActiveDocument
.Bookmarks("test1").range.Text = TextBox1.Text

End With
End Sub

Input in textbox: hello
Ouput at bookmark: hellohellhelheh




caz

caz1805
04-19-2006, 11:12 AM
hope that makes it clearer.

caz

fumei
04-19-2006, 02:37 PM
Walk through the code! EVERY time the textbox changes you are taking the textbox text and adding it to the boomark text. EVERY letter typed fires the instruction. You are NOT replacing the bookmark text.

Textbox (typed): Bookmark result: = textbox text + bookmark text
h h
e he + h = heh
l hel + heh = helheh
l hell + helheh = hellhelheh
o hello + hellhelheh = hellohellhelheh

TonyJollans
04-20-2006, 03:06 AM
Autocorrection works a word at a time, so your translator is pretty basic, at best. Is there a reason for using autocorrect? Equvalent functionality could be got directly from code with, say, an input box for input and any format you wanted for end results in the document.