Line below table

A

Al & Cay Grant

I am still getting the hang of working with inserting variables into the
document.

I am in Word97 where their is no InsertRowAbove / InsertRowBelow.

So to add some rows to the bottom of the table I need to first set the range
to the line below the bottom of the table then do my insert, I am using
autotext to insert 4 rows that are preformatted.

But what I want to know is the best way to get to the first line below the
last row of the first table?

Cheers

-Al
 
A

Al & Cay Grant

Jean-Guy and Jonathon know the background to this question, so if your
reading this guys I would love to hear your thoughts.

I have a series of listboxes which ask for;
Name, Address, Town, DOB, Age, Job.

This is then saved as an array, which is used to populate a list box with
the above information.

Each ROW in the listbox represents a person, but because of Redim Preserve
affecting only the columns each COLUMN of the array is a person. They are
transposed between the listbox and the array.....

I am at the stage now where I am inserting the personal info into a table in
a template.

The table is the first table in the template and is 4 x 4. If their is more
than one person additional tables (which contain formatting) are inserted by
way of a autotext entry.

Now I could insert each bit of personal information bit by bit with
something like:

For r = 0 To numberinlistbox
..Tables(1).Cell((r * 4) + 1, 2).Range.Text = ListBox2.List(r, 0) ' Insert
Full Name
Next r

But that is gonna mean 6 loops, that loop for each person, and typically
talking about 2-3 people, and is rather ugly way to do it.

I have read on a MVP website that its better to insert as much data as
possible at once, and that to do it, peice by peice is very slow.

Anybody know a way to do this quicker?

The end result should look like:

Name1 DOB: 12/12/80
Address1 AGE: 23
Town1 OCC: Builder

Where DOB/AGE/OCC are in the autotext table.....

Any anybody able to help point me in the right direction?

Ta

-Al
 
D

Doug Robbins - Word MVP

Hi Al,

The following code will add a row to the bottom of the first table in the
document.

ActiveDocument.Tables(1).Rows.Add

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
D

Doug Robbins - Word MVP

Hi Al,

This is a somewhat different approach that adds, in this case an employee to
a table in a document and also populates the listbox with the employees at
the same time. It will give you the details for each person on a separate
row rather than in columns (which would be difficult to work with):

Private Sub cmdAddEmployee_Click()
'Check that data has been entered into both the FirstName and the LastName
controls.
If Len(Trim(txtFirstName)) = 0 Or Len(Trim(txtLastName)) = 0 Then
MsgBox "You must enter both the first name and the last name of the
Employee"
Exit Sub
End If
'Get the path to the file containing the Training Tracker Data.
PathofSystemFiles =
System.PrivateProfileString("C:\RNMFormSettings.txt", "MacroSettings", _
"TrainingTrackerDataFileSource")
'Open the TrainingData File
Set sourcedoc = Documents.Open(PathofSystemFiles & "\TrainingData.doc",
, , False)
'Determine what action to take based on the caption that appears on the
button
If cmdAddEmployee.Caption = "Save" Then 'modify existing record
'Locate the row containing the employee
For j = 2 To sourcedoc.Tables(2).Rows.Count
Set myitem = sourcedoc.Tables(2).Cell(j, 1).Range
myitem.End = myitem.End - 1
If myitem = i Then
i = j
Exit For
End If
Next j
Else ' Add new record
i = sourcedoc.Tables(2).Rows.Count
Set myitem = sourcedoc.Tables(1).Cell(i, 1).Range
sourcedoc.Tables(2).Rows.Add
i = i + 1
sourcedoc.Tables(2).Cell(i, 1).Range.Text = Val(myitem) + 1
End If
'Enter the revised data into the Course Details table and Refresh the
ListCourses ListBox
sourcedoc.Tables(2).Cell(i, 2).Range.Text = txtEmployeeNumber
sourcedoc.Tables(2).Cell(i, 3).Range.Text = txtLastName
sourcedoc.Tables(2).Cell(i, 4).Range.Text = txtFirstName
sourcedoc.Tables(2).Cell(i, 5).Range.Text = TxtDepartment
sourcedoc.Tables(2).Cell(i, 6).Range.Text = txtPhone
sourcedoc.Tables(2).Cell(i, 7).Range.Text = txtEmail
sourcedoc.Save
'Sort the Employee list by Last Name, First Name
sourcedoc.Tables(2).Sort ExcludeHeader:=True, FieldNumber:="Column 3",
SortFieldType _
:=wdSortFieldAlphanumeric, SortOrder:=wdSortOrderAscending,
FieldNumber2:="Column 4", _
SortFieldType2:=wdSortFieldAlphanumeric,
SortOrder2:=wdSortOrderAscending
'Populate the ListEmployees listbox
Call PopulateListEmployees
'Clear the fields in the FrameNewEmployeeDetails
txtEmployeeNumber = ""
txtFirstName = ""
txtLastName = ""
TxtDepartment = ""
txtPhone = ""
txtEmail = ""
'Enable the ListEmployees control
ListEmployees.Enabled = True
'Modify the caption of the FrameNewEmployeeDetails and hide it
FrameNewEmployeeDetails.Visible = False
FrameNewEmployeeDetails.Caption = "New Employee Details"
'Modify the caption of the cmdAddEmployee button and disable the buton
cmdAddEmployee.Caption = "Add Employee to List"
cmdAddEmployee.Enabled = False
'Enable the NewEmployee button
cmdNewEmployee.Enabled = True
'Hide the cmdCancel Button
cmdCancelEmployee.Visible = False

End Sub

This is the PopulateListEmployees routine that is called by the above code:

Sub PopulateListEmployees()
'Get the number or courses = number of rows in the table of course details
less one
i = sourcedoc.Tables(2).Rows.Count - 1
'Get the number of columns in the table of Employees
j = sourcedoc.Tables(2).Columns.Count
'Set the number of columns in the Listbox to match
'the number of columns in the table of Employees
ListEmployees.ColumnCount = j + 1
'Define an array to be loaded with the Employee data
Dim MyArray() As Variant
'Load client data into MyArray
ReDim MyArray(i, j)
For n = 0 To 1
For m = 0 To i - 1
Set myitem = sourcedoc.Tables(2).Cell(m + 2, n + 1).Range
myitem.End = myitem.End - 1
MyArray(m, n) = myitem.Text
Next m
Next n
For m = 0 To i - 1
Set Lname = sourcedoc.Tables(2).Cell(m + 2, 3).Range
Lname.End = Lname.End - 1
Set Fname = sourcedoc.Tables(2).Cell(m + 2, 4).Range
Fname.End = Fname.End - 1
MyArray(m, 2) = Lname.Text & ", " & Fname.Text
Next m
For n = 2 To j - 1
For m = 0 To i - 1
Set myitem = sourcedoc.Tables(2).Cell(m + 2, n + 1).Range
myitem.End = myitem.End - 1
MyArray(m, n + 1) = myitem.Text
Next m
Next n
'Load data into the ListCourses listbox
ListEmployees.List() = MyArray
'Close the file containing the client details
sourcedoc.Close SaveChanges:=wdDoNotSaveChanges
Application.ScreenUpdating = True
End Sub

This is the code that you would need to delete an employee form the table in
the document as well as from the listbox:

Private Sub CmdDeleteEmployee_Click()
'Display a warning message
Dim Msg, Style, Title, Response, MyString
Msg = "You are about to delete the selected employee. Do you want to
continue ?" ' Define message.
Style = vbYesNo + vbCritical + vbDefaultButton2 ' Define buttons.
Title = "Warning" ' Define title.
Response = MsgBox(Msg, Style, Title)
If Response = vbYes Then ' User chose Yes.
'Disable the cmdDeleteEmployee button
CmdDeleteEmployee.Enabled = False
'Get the Row Number for the Employee who is to be deleted.
i = ListEmployees.Column(0)
'Open the TrainingData File
Set sourcedoc = Documents.Open(PathofSystemFiles &
"\TrainingData.doc", , , False)
'Locate the row containing the employee
For j = 2 To sourcedoc.Tables(2).Rows.Count
Set myitem = sourcedoc.Tables(2).Cell(j, 1).Range
myitem.End = myitem.End - 1
If myitem = i Then
i = j
Exit For
End If
Next j
'Delete the row containing the data for the employee
sourcedoc.Tables(2).Rows(i).Delete
'Save and Close the TrainingData file
sourcedoc.Close wdSaveChanges
'Remove the item from the listEmployees
ListEmployees.RemoveItem (ListEmployees.ListIndex)
Else ' User chose No.
Exit Sub
End If
End Sub
--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
J

JGM

Hi Al,

Your array, if I remember correctly had 10 rows and as many column as there
were people.
If in the end you are using only 6 data type per person, then you do not
need to put 10 coloumns everything in the array.

In any case, you should have something like: MyArray(5 , x) were x = number
of people.

You use that array to populate the combobox. Then the user used the combobox
to confirm that everything is OK (I strongly recommend that you give the
user a way of reviewing, adding, modifying, deleting names). Once everything
has been confirmed, your array is ready to go.

You can use the array, not only the combobox, to populate your table.

Find the logic of your table and combine it to your array. You are going to
have to figure out the logic behind inserting the text in the appropriate
places.

Prepare an Autotext that has all the rows/cells neccessary for one person,
then, before inserting the text, insert as many Autotext as you have people,
then either place the cursor, or use the range object to define the table
you need for the job.

For example:

With MyRange.Tables(1)

For r = 0 To UBound(MyArray, 2) 'Look for UBound in the Help
.Cell((r * 4) + 1, 2).Range.Text = MyArray(0 , r)
.Cell((r * 4) + 1, 4).Range.Text = MyArray(1 , r)
.Cell((r * 4) + 2, 2).Range.Text = MyArray(2 , r)
.Cell((r * 4) + 2, 4).Range.Text = MyArray(3 , r)
.Cell((r * 4) + 3, 2).Range.Text = MyArray(4 , r)
'etc.
Next r

End With

Or something like that...

HTH
Cheers.
 
A

Al & Cay Grant

Hi Jean-Guy & Doug,

Yes it does actually have 10, but not all ten are used at this stage, though
they are later.

I see in both your example and Doug's example that the data is inserted one
piece at a time.

I am sur eyou guys have seen it, but if you have'nt have a look at;
http://www.mvps.org/word/FAQs/TblsFldsFms/FastTables.htm
point number 5 in particular, and 8 and 10.

Does that relate to this scenario?

-Al


The MVP site I read on tables
 
J

JGM

Hi Al,

Just to clarify something, my previous post did not contain code you could
actually use... it was just a quick adaptation of the code you posted before
to show you how I think you could proceed...

Point 5: No. This is if you were inserting the text as a block and then
converting it to a table. You did not seem to lean that way, especially
since you have specific formatting in mind. This point would be worth
considering if you were creating a table that contained hundreds of rows...
It would then be a lot faster and stable.

Point 8: Yes, but again, since you will not be dealing with hundreds of
records at a time in the same table it will not make a big difference
speed-wise... But it is good practice to do it the right way even for a
small table.

Point 10: Not really, I thought that you were going to insert the data by
following the rows... so this point on column is not relevant, but good to
know! ( I have had this situation before where I noticed that my column
range was in fact the whole range from the top cell, going right to the next
cell, and so on to the last cell in my "column range." So if I assign a
bookmark to the third column of a 4x4 table, I actually have 13 cells in the
range instead of the 4... This has an impact when you assign a bookmark to a
column, then use a formula to get the total of the bookmark... the the total
will be wrong everytime you have numbers in cells adjacent to your "columnar
bookmark" because the idiots at Microsoft decided that a columnar bookmark
would include the adjacent cells even if the display shows only the
column.... anyway, I digress!)

As far as inserting the data one at the time, I do not think you have much
choice, especially since you are not using the whole array and you have
labels in your table that are static...

HTH
Cheers!
 
D

Doug Robbins - Word MVP

Hi Al,

I think you might not have fully understood how my code works. Certainly,
the data is inserted into the new document, one piece at a time, that time
however is when the user clicks on the Add button. The procedure behind
that button also adds the person's details to the list box so that the user
is aware of the persons whose information has already been entered. The
delete routine allows the user to delete the person selected in the listbox
from both the list box and the document. A third command button would be
used on the userform to insert the balance of the information into the new
document.

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top