PDA

View Full Version : how to create new table feilds in VBA



dtrompstine
12-06-2010, 05:33 PM
ok, what im trying to do is create a VBA form front end that populates a word document.

i know how to populate bookmarks and objects from a VBA Form.

the problem is that i need to add an indeterminate amount of information.

for example: lets say i need to add names to a list which i want to appear in a table in a word document. But i don't know how many names will be added.. How do i make it so that every time someone enters information in a VBA form it creates a new line in a table and automatically populates the data?

macropod
12-06-2010, 09:40 PM
Hi dtrompstine,

Each time you add a name on your userform, simply add a new row to the table using code like:
Sub AddRow()
With ActiveDocument.Tables(1)
.Rows.Add
With .Rows.Last
'modify the following lines to insert the values from your useform
.Cells(1).Range.Text = "Hello World"
.Cells(2).Range.Text = "The quick brown fox."
End With
End With
End Sub

dtrompstine
12-06-2010, 11:10 PM
Thanks heaps for the reply.. thats awesome..

just one more question.. what code would i use to create a checkbox in one of the cells and give it a lable?

thanks again

macropod
12-07-2010, 12:14 AM
Hi dtrompstine,

Assuming you mean a checkbox formfield, plus some text:
Sub AddRow()
With ActiveDocument.Tables(1)
.Rows.Add
With .Rows.Last
'modify the following lines to insert the values from your useform
.Cells(1).Range.Text = "Hello World"
With .Cells(2)
.Range.FormFields.Add Range:=.Range, Type:=wdFieldFormCheckBox
.Range.InsertAfter vbTab & "The quick brown fox"
End With
End With
End With
End SubOf course, for the checkbox formfield to work, the document will at some stage have to be protected for forms.

dtrompstine
12-08-2010, 09:23 PM
cool... ok i have another question:

i am writing a form which based on a particular selection from a combo box will have to launch another form... I know how to use if statments in order to launch the new form but.... what i would like to do is: once it has identified that it needs to launch the new form, i would like the code to wait untill the new form has been filled in and then continue running the code..... is that possible??

does it even make sense?

fumei
12-09-2010, 09:56 AM
In a sense it will do this.

Userform1 has focus (it is running code).
Logic determines userform1 needs to be activated.
Userform2 activated. It now has focus. It will retain focus until you release it.
When userform2 is released, focus is passed back to whatever called it, in this case, Userform1.

Tinbendr
12-09-2010, 12:41 PM
ok i have another question:

The way I do that is, say, you have a command button that calls userform 2.


Private CommandButton1_Click()
Userform2.Show
'When Userform 2 is closed/unloaded, it will come back here.

'Close Userform 1 or do other things.
Unload Me
End Sub