PDA

View Full Version : Automated Inesrtion of a table...



DeusDNE
05-03-2007, 06:48 AM
Hi all,

New to this, well returning after a long absence, so be nice please!!!

I have been asked to created a word doc that automatically creates a table with X number of rows and 1 column depending on the number entered into a textbox (or whatever si the best medium for this).

Ideally if you entered 5 into the textbox, a 5x1 table would be created below that textbox...If you then chnaged your mind and entered a 3 into the box, the table would be chnaged to a 3x1 !!

Heres hoping you can help on this one

Deus
--
DNE

fumei
05-03-2007, 07:51 AM
Sub cmdMakeTable_Click()
If Selection.Information(wdWithInTable) = True Then
Selection.Tables(1).Delete
End If

ActiveDocument.Tables.Add Range:=Selection.Range, _
NumRows:=TextBox1.Text, NumColumns:=1, _
DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitFixed
TextBox1.Text = ""
TextBox1.SetFocus
UserForm1.Hide
UserForm1.Show
End SubDeletes the table if the Selection is in a table, then creates a table of x row(s) (from the textbox) and 1 column.

It then clears the textbox value, and sets the focus back to it.

It hides and shows the userform because otherwise you do not see the changes in the document.

Enter 5 in the textbox and click the commandbutton, and a table 5 row / 1 column is created.

Enter 3 and click the button, the 5 x 1 table is deleted, and a new 3 x 1 table is created.

DeusDNE
05-03-2007, 07:54 AM
Cheers Will test immediately.

Thanks again.

Deus
--
DNE

fumei
05-03-2007, 07:56 AM
Ooops. Sorry. I made an assumption that you were talking about doing this from a userform.

Are you asking about a textbox IN the document?

DeusDNE
05-03-2007, 08:09 AM
OK well the table appears ok, but the command button dissappears !

I have had to remove the user forms part, (Office politics!!!! You dont wanna know) so this is now all just text boxes in plain old word...Does this change things dramatically?

TIA

Deus
--
DNE

fumei
05-03-2007, 12:39 PM
Perhaps it would be a good idea to actually describe what the situation is.

WHAT commandbutton?

DeusDNE
05-04-2007, 02:54 AM
OK.

A word doc with various text boxes for various reasons.

There is a command button to email completed doc to spefic mailbox.

As I couldnt get the code to work as an on exit command frmo the Textbox containing the number, I created a command button and assigned it to that.

I realise your code wasnt written for this situation, so wasnt expecting it to work correctly, but thought i could get it working. Im obviously thicker than i thought.. Help please. !!!

Charlize
05-04-2007, 03:32 AM
Sub Make_Table()
Dim No_Rows As String
No_Rows = InputBox("Give no of rows", "No of required rows", "1")
If Selection.Information(wdWithInTable) = True Then
Selection.Tables(1).Delete
End If
ActiveDocument.Tables.Add Range:=Selection.Range, _
NumRows:=Val(No_Rows), NumColumns:=1, _
DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitFixed
End SubCharlize

fumei
05-05-2007, 06:40 AM
There you go. That will work as code for a commandbutton....except.....

Charlize, did you actually run this as code for a commandbutton? The code works fine, as code. However, if it is run from a commandbutton it will make the table, yes...but, um, it now has the commandbutton inside it.

Also if the user cancels the Inputbox you get an error.

Charlize
05-05-2007, 08:57 AM
There you go. That will work as code for a commandbutton....except.....

Charlize, did you actually run this as code for a commandbutton? The code works fine, as code. However, if it is run from a commandbutton it will make the table, yes...but, um, it now has the commandbutton inside it.

Also if the user cancels the Inputbox you get an error.I used a macrofield with a custom action (a macro) on doubleclick. This way the macrobutton is deleted and in return you get a table. Not a real button.Sub Make_Table()
Dim No_Rows As String
No_Rows = InputBox("Give no of rows", "No of required rows", "1")
If No_Rows <> vbNullString Then
If Selection.Information(wdWithInTable) = True Then
Selection.Tables(1).Delete
End If
ActiveDocument.Tables.Add Range:=Selection.Range, _
NumRows:=Val(No_Rows), NumColumns:=1, _
DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitFixed
Else
MsgBox "Please, give a number.", vbCritical
End If
End SubThis is for the cancelbutton.

Charlize

Charlize
05-05-2007, 09:09 AM
A variation to limit the number of rows.Sub Make_Table()
Dim No_Rows As String
No_Rows = InputBox("Give no of rows", "No of required rows", "1")
If No_Rows <> vbNullString Then
Select Case No_Rows
Case "1", "2", "3", "4", "5", "6", "7", "8", "9"
If Selection.Information(wdWithInTable) = True Then
Selection.Tables(1).Delete
End If
ActiveDocument.Tables.Add Range:=Selection.Range, _
NumRows:=Val(No_Rows), NumColumns:=1, _
DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitFixed
Case Else
MsgBox "Give a number from 1 'till 9", vbInformation
End Select
Else
MsgBox "Please, give a number.", vbCritical
End If
End SubCharlize

fumei
05-05-2007, 04:38 PM
So if the macrobutton is deleted, how does the user change their mind? This was in the OP.

User types in 5, a 5 x 1 table is created. User changes mind, types in 3, old 5 x 1 table deleted, 3 x 1 table created.

fumei
05-05-2007, 05:00 PM
Ok, here is a sort of solution.

ActiveX textbox. The _Change event is as follows:Sub TextBox1_Change()
Select Case TextBox1.Text
Case 0, ""
MsgBox "Value can not be 0, or blank. " _
& "Please enter a value."
Exit Sub
Case Else
Selection.Move unit:=wdCharacter, _
Count:=2
If Selection.Information(wdWithInTable) = True Then
Selection.Tables(1).Delete
End If
ActiveDocument.Tables.Add Range:=Selection.Range, _
NumRows:=TextBox1.Text, NumColumns:=1, _
DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitFixed
End Select
End Sub User types in 5, Selection is moved two characters.

If it is in a table, it deletes it and creates a 5x1 table.

User goes back in, and types 3. Selection is moved two characters. It IS in a table, so it is deleted and a 3x1 table is created.

User types in 0 or nothing, error message is displayed. Focus remains in the control. Demo attached.

DeusDNE
05-08-2007, 03:53 AM
Thanks guys n gals.

All sorted, and the bods that be are happy.

You are stars

Deus
--
DNE