Log in

View Full Version : Copy row with bookmarks as part of userform



zazrex
09-03-2012, 07:32 PM
Hi,

I'm trying to create a mini database using Word 2003 with a table and a userform.

WhilstI know Excel would probably be the better place to do it, my user base will find it easier in Word.

So in step form, what I'm trying to achieve is:

1. User enters data into form
2. User clicks submit, details entered into row in table
3. At the same time as 2. a new row with same bookmarks (placeholders) are copied into next row.
4. User form presented for user to continue entering details.
5. continue steps 2 - 4 as often as required.

Step 3 I've included code that creates a new row in the document. The problem is that it does not copy the bookmarks. So when the user form is completed again, it is entering new text back within the first row.

Also Step 4, I'm struggling to work out how to code for the form to reappear after submit has been selected.

Any help would be very much appreciated. Thanks!

gmaxey
09-03-2012, 07:59 PM
3. If you are using a table (which is a range container itself), maybe you don't need bookmarks. You can't copy bookmarks (a bookmark has to be unique). If you must have bookmarks then you will have to create new ones (i.e., same name with a row number index or something).

4. Don't hide the form with the button (don't call it submit either). Have one button to write to the last row in the table and add a new row, use a second button to close (submit) the form:

Private Sub CommandButton1_Click()
Dim oRow As Row
'Target last row of table.
Set oRow = ActiveDocument.Tables(1).Rows.Last
With oRow
.Cells(1).Range.Text = Me.TextBox1
.Cells(2).Range.Text = Me.TextBox2
.Cells(3).Range.Text = Me.TextBox3
.Cells(4).Range.Text = Me.TextBox4
.Cells(5).Range.Text = Me.TextBox5
End With
ActiveDocument.Tables(1).Rows.Add
End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub

fumei
09-03-2012, 08:00 PM
Bookmarks MUST be uniquely named. You can not copy them.

zazrex
09-03-2012, 10:42 PM
Thanks Greg!

Got it working a treat with:

Private Sub cmdOK_Click()
Dim oRow As Row
Set oRow = ActiveDocument.Tables(1).Rows.Last
With oRow
.Cells(1).Range.Text = Me.txtDateOfBio
.Cells(2).Range.Text = Me.txtName
.Cells(3).Range.Text = Me.txtTitle
.Cells(4).Range.Text = Me.txtBiography
.Cells(5).Range.Text = Me.txtDatePermission
.Cells(6).Range.Text = Me.txtAuthor
End With
ActiveDocument.Tables(1).Rows.Add
Unload Me
frmBioEntry.Show
End Sub

I'm enthused to find other reasons to play with VBA now :-)