PDA

View Full Version : input box



oleg_v
12-20-2010, 01:24 AM
Hi i need some help.
I have a table in the word document and i want that if i click with the mouse inside each cell of the table the input box will come out and i will write the containment of the cell insied the input box.

thanks

gmaxey
12-20-2010, 05:43 AM
May I ask why you don't simply click the mouse in the cell and type the cell content in the cell itself?

Regardless, you would need to create a class object to monitor the selection change event:

In a standard module:

Option Explicit
Dim oAppClass As New Class1
Sub AutoOpen()
Set oAppClass.oApp = Word.Application
End Sub


In the Class1 module:

Option Explicit
Public WithEvents oApp As Word.Application
Private Sub oApp_WindowSelectionChange(ByVal Sel As Selection)
If Selection.Type = wdSelectionIP And Selection.Information(wdWithInTable) Then
Selection.Cells(1).Range.Text = InputBox("Enter the text for this cell.")
End If
End Sub

oleg_v
12-20-2010, 05:55 AM
how can i get it to work?

gmaxey
12-20-2010, 06:23 AM
1. Open the document.
2. Open the VB Editory
3. Create a standard module and paste in the code that goes in the standard module.
4. Create a class module and pastte int he code that goes in the class module.
5. Run the procedure "AutoOpen" to inialize the class object. (Note: this procedure will run automatically when you save, close, and reopen the document)
6. Click in a table cell.

fumei
12-20-2010, 11:08 AM
Do be aware that this code replace all current content in the cell.

So if you have "Harry Belefonte" in the cell, and you realize..gosh, that is Belafonte, with an "a", click in the cell at the "e", you MUST type in the entire string. You can NOT replace a given character(s).

In other words, in the example, you can NOT just change the "e" to an "a". What ever is the the InputBox replaces the cell content.

fumei
12-20-2010, 11:15 AM
Not only that, but if you cancel out of the InputBox, any existing content in the table cell is deleted.

gmaxey
12-20-2010, 11:18 AM
I am still curious why the OP doesn't just click in the cell and type. That way all of these issues that you enumerate are mute ;-)

fumei
12-20-2010, 11:19 AM
If you want to be to retain existing content, you will have to use a userform, but I fail to see where you are getting any advantage going this route. As Greg mentions, what about just typing in the cell? What is your purpose here?

fumei
12-20-2010, 11:20 AM
And once again that GM slides his post in while I am typing away.

macropod
12-20-2010, 07:23 PM
If you want to be to retain existing content, you will have to use a userformNot if you use:
Private Sub oApp_WindowSelectionChange(ByVal Sel As Selection)
If Selection.Type = wdSelectionIP And Selection.Information(wdWithInTable) Then
Selection.Cells(1).Range.InsertAfter InputBox("Enter the text for this cell.")
End If
End Sub
Instead of .InsertAfter, one could use .Insertbefore.