PDA

View Full Version : VBA help with word tables



quicksilver
02-05-2008, 02:17 AM
Hello everyone, this is my first post here.

Im after some help, ive got a ms word document with a table and it has 5 columns and one row. In the row i have set some form labels in each of the fields already and this is filled in by information from the form i display.

What i would like to do is to display a message saying would you like to add another row, then in the table it will add another row and insert more labels with the correct value.

Can i do it that way or with i have to set a number of rows with the label fields ready and do my code based on this?

any advice is appreciated

quicksilver
02-05-2008, 06:48 AM
Sorry for the double post,

I have got around the above problem now, but i have a new problem that has developed.

I can get it to write the text in the table for the row and column but i had to specify the row number and the column number.

What i would like to do is that when the user clicks yes on vbYesNo then it will add another row and then they fill in the details on userform2. The when they click ok now it will write that information in the row just added.

Here is my code so far:



Private Sub cmdform2_Click()
Dim response

Dim table As Integer

Dim Row As Integer

'Places the values of userform2 text boxes into the variables

VManufact = UserForm2.txtmanu.Text
VModel = UserForm2.txtmodel.Text
VSerial = UserForm2.txtserialno.Text
VSuppliedBy = UserForm2.txtsuppliedby.Text
Vdate = Date
table = 2
Row = 4

'places the values of the variables into labels on the document

ActiveDocument.Tables(table).Cell(Row:=Row, Column:=1).Range.InsertAfter Text:=VManufact
ActiveDocument.Tables(table).Cell(Row:=Row, Column:=2).Range.InsertAfter Text:=VModel
ActiveDocument.Tables(table).Cell(Row:=Row, Column:=3).Range.InsertAfter Text:=VSerial
ActiveDocument.Tables(table).Cell(Row:=Row, Column:=4).Range.InsertAfter Text:=VSuppliedBy
ActiveDocument.Tables(table).Cell(Row:=Row, Column:=5).Range.InsertAfter Text:=Vdate

'clears the form ready for another insert

UserForm2.txtmanu.Text = ""
UserForm2.txtmodel.Text = ""
UserForm2.txtserialno.Text = ""
UserForm2.txtsuppliedby.Text = ""

'ask the user do they want to add another row

response = MsgBox("Insert Another Row?", vbyesno)

'if the user clicks yes then run vbyesnocancel

If response = vbYes Then
vbyesnoanswer

'else hide the form

Else
UserForm2.Hide

End If

End Sub

Sub vbyesnoanswer()

'insert a row below

Selection.InsertRowsBelow NumRows:=1

NextRow

End Sub

pnewton
02-07-2008, 06:56 PM
A quick solution to your problem is to use bookmarks at the end of the table ... when prompting user for new row, if yes, goto bookmark, insert autotext (formatted row)
for example:
Selection.GoTo what:=wdGoToBookmark, Name:="EndRow"
Application.DisplayAutoCompleteTips = True
ActiveDocument.AttachedTemplate.AutoTextEntries("NewRow"). _
Insert Where:=Selection.Range, RichText:=True
A different way of looking at the same problem. This way you could use components of autotext to build your tables.

Cheers

Pete

gwkenny
02-11-2008, 12:13 AM
OP:

I suggest you code slightly differently. Instead of, "What i would like to do is that when the user clicks yes on vbYesNo then it will add another row and then they fill in the details on userform2. The when they click ok now it will write that information in the row just added."

When they click to add more data, then bring up the form. After they fill in the details (and have their entries data validated) and the click OK THEN will the table insert another row and enter the data.

You can insert a row at the bottom by simply:

ActiveDocument.Tables(table).Rows.Add

Then you get the last row number (ActiveDocument.Tables(table).Rows.Count) so you can input your data directly into the Row and Column necessary.

Don't use bookmarks unless necessary.

quicksilver
02-11-2008, 03:19 AM
Hey,

thanks for the replies i have now got it working, i used the insert a new row and insert the data to the end of the row using the .count option.

cheers for all your help.