Tables and inserting rows

B

bryan

2 part question:
1st part) If I have a template with multiple tables, how do I know which is
table(1), table(2), etc. in order to work with

2nd part) On this form, 1 table will be for insurance vehicle coverage.
Column 1 is coverage, col 2 is effectvie date, col3 is amount, col4 is
deductible.
As I am reading a database I want to populate col1 - col4 formfileds.
Since I will have more than 1 coverage, how can I add the next row(s) and
populate those fileds.

After this I list the endorsement forms: same principle as above
I'll have a 1 row table and then add row/columns for each endorsement found

Or for the second part would it be easier to have each table (lets say with
8 rows) and then populate and once complete then check for empty at 8 and
delete that row and continue up until not empty.

Thanks in advance fro the help in this 2 part question.

Bryan
 
D

Doug Robbins - Word MVP

Tables are numbered starting from the beginning of the document. Therefore,
Table(1) is the first table in the document.

The following code will populate a table with data from an Access database:

Dim myDataBase As Database
Dim myActiveRecord As Recordset
Dim i As Long
Dim dtable As Table, drow As Row
'Open a database
Set myDataBase = OpenDatabase("c:\Access\Procurement Plan.mdb")
'Access the first record from a particular table
Set myActiveRecord = myDataBase.OpenRecordset("Currencies",
dbOpenForwardOnly)
'Add a table to the document with one row and as many fields as there are in
the database table
Set dtable = ActiveDocument.Tables.Add(Range:=Selection.Range, NumRows:=1,
numcolumns:=myActiveRecord.Fields.Count)
Set drow = dtable.Rows(1)
'Loop through all the records in the table until the end-of-file marker is
reached
Do While Not myActiveRecord.EOF
'Populate the cells in the Word table with the data from the current
record
For i = 1 To myActiveRecord.Fields.Count
drow.Cells(i).Range.Text = myActiveRecord.Fields(i - 1)
Next i
'Add a new row to the Word table and access the next record
Set drow = dtable.Rows.Add
myActiveRecord.MoveNext
Loop
'The last row will be empty, so delete it
drow.Delete
'Then close the database
myActiveRecord.Close
myDataBase.Close


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
B

bryan

I did not mean to hot no on if this was helpful!

Since I will have at least 2 tables to populate:
1 being for coverage info and the other table for endorsements, how do I
position to a certain spot on the document for each, using bookmarks?
If so how?
I'm getting data from a sql database so I assume it somewhat the same as I
select the column count. Also How does the column width get set for each in
row or does it size based upon data?
If I cannot position the table to certain spot then what?

Thanks,
Bryan
 
B

bryan

Hi Doug,
Just want to reiterate this:
I will have multiple table on this document:
1 for coverage info, 1 for lienholders, and 1 for endorsement forms.
Others to follow.

If I am inserting a table as I am going along, how do I position this to a
certain spot on the document?
I suppose I could use a bookmark for the heading above each and then insert
and populate.
Still the question,
Sizing of each cell..........
Is that determined by the data length?

Thanks,
Bryan
 
D

Doug Robbins - Word MVP

I think that the better option might be to start with a one row table for
each of those that might be required in the document, complete with the
necessary row headings and with columns sized as required and in the
locations that you want them. Then use code to add rows to each table as
required by the data that you want to insert into them. In the event that
there is no data for some of the tables and they are thus not required, the
code could delete them.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
B

bryan

Hi Doug,
I appreciate the help but, need a little more to go on since I am new on
dealing with adding tables and populating with data.
Lets say I have a 1 row table with 5 columns to start.
Will this row be for the headings only?
How do I then add the next row(s) and populate data, with adding formfields
or by populating like you had indicated previously, and by identifying which
table?

As much info as possible would be appreciated.

Thanks,
Bryan
 
D

Doug Robbins - Word MVP

You could identify each of the one row tables by assigning a bookmark to
them - e.g. Coverage, Lienholders, etc

The use something like

Dim myDataBase As Database
Dim myActiveRecord As Recordset
Dim i As Long
Dim dtable As Table, drow As Row
'Open a database
Set myDataBase = OpenDatabase("[Databasepath\filename.mdb]")
'Access the first record from a particular table
Set myActiveRecord = myDataBase.OpenRecordset("[tblNameforCoverageData]",
dbOpenForwardOnly)
'Add a table to the document with one row and as many fields as there are in
the database table
Set dtable = ActiveDocument.Bookmarks("Coverage").Range.Tables(1)
'Loop through all the records in the table until the end-of-file marker is
reached
Do While Not myActiveRecord.EOF
'Add a row to the table and populate the cells in that row with the data
from the current
record
For i = 1 To myActiveRecord.Fields.Count
Set drow = dtable.Rows.Add
drow.Cells(i).Range.Text = myActiveRecord.Fields(i - 1)
Next i
myActiveRecord.MoveNext
Loop
'Then close the database
myActiveRecord.Close

'Access the first record from the next table
Set myActiveRecord = myDataBase.OpenRecordset("[tblNameforLienHolderData]",
dbOpenForwardOnly)
'Add a table to the document with one row and as many fields as there are in
the database table
Set dtable = ActiveDocument.Bookmarks("Lienholders").Range.Tables(1)
'Loop through all the records in the table until the end-of-file marker is
reached
Do While Not myActiveRecord.EOF
'Add a row to the table and populate the cells in that row with the data
from the current
record
For i = 1 To myActiveRecord.Fields.Count
Set drow = dtable.Rows.Add
drow.Cells(i).Range.Text = myActiveRecord.Fields(i - 1)
Next i
myActiveRecord.MoveNext
Loop
'Then close the database
myActiveRecord.Close

'etc.

Set myActiveRecord = Nothing
myDataBase.Close
Set myDataBase = Nothing


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
B

bryan

See if I have this striaght to get started:
For each table I will have a bookmark for placement. Am I creating the table
as I go or do I want a 1 row table in autotext with the headings to insert at
bookmark and then add rows to that?
Having the one row as autotext would allow for sizing of columns. If using
autotext for the 1 row table, do I want the headings in this row?

Again, I truly appreciate all the help on this.

Thanks,
Bryan



Doug Robbins - Word MVP said:
You could identify each of the one row tables by assigning a bookmark to
them - e.g. Coverage, Lienholders, etc

The use something like

Dim myDataBase As Database
Dim myActiveRecord As Recordset
Dim i As Long
Dim dtable As Table, drow As Row
'Open a database
Set myDataBase = OpenDatabase("[Databasepath\filename.mdb]")
'Access the first record from a particular table
Set myActiveRecord = myDataBase.OpenRecordset("[tblNameforCoverageData]",
dbOpenForwardOnly)
'Add a table to the document with one row and as many fields as there are in
the database table
Set dtable = ActiveDocument.Bookmarks("Coverage").Range.Tables(1)
'Loop through all the records in the table until the end-of-file marker is
reached
Do While Not myActiveRecord.EOF
'Add a row to the table and populate the cells in that row with the data
from the current
record
For i = 1 To myActiveRecord.Fields.Count
Set drow = dtable.Rows.Add
drow.Cells(i).Range.Text = myActiveRecord.Fields(i - 1)
Next i
myActiveRecord.MoveNext
Loop
'Then close the database
myActiveRecord.Close

'Access the first record from the next table
Set myActiveRecord = myDataBase.OpenRecordset("[tblNameforLienHolderData]",
dbOpenForwardOnly)
'Add a table to the document with one row and as many fields as there are in
the database table
Set dtable = ActiveDocument.Bookmarks("Lienholders").Range.Tables(1)
'Loop through all the records in the table until the end-of-file marker is
reached
Do While Not myActiveRecord.EOF
'Add a row to the table and populate the cells in that row with the data
from the current
record
For i = 1 To myActiveRecord.Fields.Count
Set drow = dtable.Rows.Add
drow.Cells(i).Range.Text = myActiveRecord.Fields(i - 1)
Next i
myActiveRecord.MoveNext
Loop
'Then close the database
myActiveRecord.Close

'etc.

Set myActiveRecord = Nothing
myDataBase.Close
Set myDataBase = Nothing


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
bryan said:
Hi Doug,
I appreciate the help but, need a little more to go on since I am new on
dealing with adding tables and populating with data.
Lets say I have a 1 row table with 5 columns to start.
Will this row be for the headings only?
How do I then add the next row(s) and populate data, with adding
formfields
or by populating like you had indicated previously, and by identifying
which
table?

As much info as possible would be appreciated.

Thanks,
Bryan
 
D

Doug Robbins - Word MVP

It would be easiest just to have each one row table with the column heading
names inserted into the document/template and have it identified by a
bookmark (rather than try and do something with autotext). Having the table
itself allows equally well the sizing of the columns.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
bryan said:
See if I have this striaght to get started:
For each table I will have a bookmark for placement. Am I creating the
table
as I go or do I want a 1 row table in autotext with the headings to insert
at
bookmark and then add rows to that?
Having the one row as autotext would allow for sizing of columns. If using
autotext for the 1 row table, do I want the headings in this row?

Again, I truly appreciate all the help on this.

Thanks,
Bryan



Doug Robbins - Word MVP said:
You could identify each of the one row tables by assigning a bookmark to
them - e.g. Coverage, Lienholders, etc

The use something like

Dim myDataBase As Database
Dim myActiveRecord As Recordset
Dim i As Long
Dim dtable As Table, drow As Row
'Open a database
Set myDataBase = OpenDatabase("[Databasepath\filename.mdb]")
'Access the first record from a particular table
Set myActiveRecord = myDataBase.OpenRecordset("[tblNameforCoverageData]",
dbOpenForwardOnly)
'Add a table to the document with one row and as many fields as there are
in
the database table
Set dtable = ActiveDocument.Bookmarks("Coverage").Range.Tables(1)
'Loop through all the records in the table until the end-of-file marker
is
reached
Do While Not myActiveRecord.EOF
'Add a row to the table and populate the cells in that row with the
data
from the current
record
For i = 1 To myActiveRecord.Fields.Count
Set drow = dtable.Rows.Add
drow.Cells(i).Range.Text = myActiveRecord.Fields(i - 1)
Next i
myActiveRecord.MoveNext
Loop
'Then close the database
myActiveRecord.Close

'Access the first record from the next table
Set myActiveRecord =
myDataBase.OpenRecordset("[tblNameforLienHolderData]",
dbOpenForwardOnly)
'Add a table to the document with one row and as many fields as there are
in
the database table
Set dtable = ActiveDocument.Bookmarks("Lienholders").Range.Tables(1)
'Loop through all the records in the table until the end-of-file marker
is
reached
Do While Not myActiveRecord.EOF
'Add a row to the table and populate the cells in that row with the
data
from the current
record
For i = 1 To myActiveRecord.Fields.Count
Set drow = dtable.Rows.Add
drow.Cells(i).Range.Text = myActiveRecord.Fields(i - 1)
Next i
myActiveRecord.MoveNext
Loop
'Then close the database
myActiveRecord.Close

'etc.

Set myActiveRecord = Nothing
myDataBase.Close
Set myDataBase = Nothing


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
bryan said:
Hi Doug,
I appreciate the help but, need a little more to go on since I am new
on
dealing with adding tables and populating with data.
Lets say I have a 1 row table with 5 columns to start.
Will this row be for the headings only?
How do I then add the next row(s) and populate data, with adding
formfields
or by populating like you had indicated previously, and by identifying
which
table?

As much info as possible would be appreciated.

Thanks,
Bryan

:

I think that the better option might be to start with a one row table
for
each of those that might be required in the document, complete with
the
necessary row headings and with columns sized as required and in the
locations that you want them. Then use code to add rows to each table
as
required by the data that you want to insert into them. In the event
that
there is no data for some of the tables and they are thus not
required,
the
code could delete them.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
Hi Doug,
Just want to reiterate this:
I will have multiple table on this document:
1 for coverage info, 1 for lienholders, and 1 for endorsement forms.
Others to follow.

If I am inserting a table as I am going along, how do I position
this
to a
certain spot on the document?
I suppose I could use a bookmark for the heading above each and then
insert
and populate.
Still the question,
Sizing of each cell..........
Is that determined by the data length?

Thanks,
Bryan

:

Tables are numbered starting from the beginning of the document.
Therefore,
Table(1) is the first table in the document.

The following code will populate a table with data from an Access
database:

Dim myDataBase As Database
Dim myActiveRecord As Recordset
Dim i As Long
Dim dtable As Table, drow As Row
'Open a database
Set myDataBase = OpenDatabase("c:\Access\Procurement Plan.mdb")
'Access the first record from a particular table
Set myActiveRecord = myDataBase.OpenRecordset("Currencies",
dbOpenForwardOnly)
'Add a table to the document with one row and as many fields as
there
are
in
the database table
Set dtable = ActiveDocument.Tables.Add(Range:=Selection.Range,
NumRows:=1,
numcolumns:=myActiveRecord.Fields.Count)
Set drow = dtable.Rows(1)
'Loop through all the records in the table until the end-of-file
marker
is
reached
Do While Not myActiveRecord.EOF
'Populate the cells in the Word table with the data from the
current
record
For i = 1 To myActiveRecord.Fields.Count
drow.Cells(i).Range.Text = myActiveRecord.Fields(i - 1)
Next i
'Add a new row to the Word table and access the next record
Set drow = dtable.Rows.Add
myActiveRecord.MoveNext
Loop
'The last row will be empty, so delete it
drow.Delete
'Then close the database
myActiveRecord.Close
myDataBase.Close


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of
my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
2 part question:
1st part) If I have a template with multiple tables, how do I
know
which
is
table(1), table(2), etc. in order to work with

2nd part) On this form, 1 table will be for insurance vehicle
coverage.
Column 1 is coverage, col 2 is effectvie date, col3 is amount,
col4
is
deductible.
As I am reading a database I want to populate col1 - col4
formfileds.
Since I will have more than 1 coverage, how can I add the next
row(s)
and
populate those fileds.

After this I list the endorsement forms: same principle as above
I'll have a 1 row table and then add row/columns for each
endorsement
found

Or for the second part would it be easier to have each table
(lets
say
with
8 rows) and then populate and once complete then check for empty
at
8
and
delete that row and continue up until not empty.

Thanks in advance fro the help in this 2 part question.

Bryan
 
B

bryan

Doug,
Works like a charm.................

Thanks again,
Bryan

Doug Robbins - Word MVP said:
It would be easiest just to have each one row table with the column heading
names inserted into the document/template and have it identified by a
bookmark (rather than try and do something with autotext). Having the table
itself allows equally well the sizing of the columns.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
bryan said:
See if I have this striaght to get started:
For each table I will have a bookmark for placement. Am I creating the
table
as I go or do I want a 1 row table in autotext with the headings to insert
at
bookmark and then add rows to that?
Having the one row as autotext would allow for sizing of columns. If using
autotext for the 1 row table, do I want the headings in this row?

Again, I truly appreciate all the help on this.

Thanks,
Bryan



Doug Robbins - Word MVP said:
You could identify each of the one row tables by assigning a bookmark to
them - e.g. Coverage, Lienholders, etc

The use something like

Dim myDataBase As Database
Dim myActiveRecord As Recordset
Dim i As Long
Dim dtable As Table, drow As Row
'Open a database
Set myDataBase = OpenDatabase("[Databasepath\filename.mdb]")
'Access the first record from a particular table
Set myActiveRecord = myDataBase.OpenRecordset("[tblNameforCoverageData]",
dbOpenForwardOnly)
'Add a table to the document with one row and as many fields as there are
in
the database table
Set dtable = ActiveDocument.Bookmarks("Coverage").Range.Tables(1)
'Loop through all the records in the table until the end-of-file marker
is
reached
Do While Not myActiveRecord.EOF
'Add a row to the table and populate the cells in that row with the
data
from the current
record
For i = 1 To myActiveRecord.Fields.Count
Set drow = dtable.Rows.Add
drow.Cells(i).Range.Text = myActiveRecord.Fields(i - 1)
Next i
myActiveRecord.MoveNext
Loop
'Then close the database
myActiveRecord.Close

'Access the first record from the next table
Set myActiveRecord =
myDataBase.OpenRecordset("[tblNameforLienHolderData]",
dbOpenForwardOnly)
'Add a table to the document with one row and as many fields as there are
in
the database table
Set dtable = ActiveDocument.Bookmarks("Lienholders").Range.Tables(1)
'Loop through all the records in the table until the end-of-file marker
is
reached
Do While Not myActiveRecord.EOF
'Add a row to the table and populate the cells in that row with the
data
from the current
record
For i = 1 To myActiveRecord.Fields.Count
Set drow = dtable.Rows.Add
drow.Cells(i).Range.Text = myActiveRecord.Fields(i - 1)
Next i
myActiveRecord.MoveNext
Loop
'Then close the database
myActiveRecord.Close

'etc.

Set myActiveRecord = Nothing
myDataBase.Close
Set myDataBase = Nothing


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
Hi Doug,
I appreciate the help but, need a little more to go on since I am new
on
dealing with adding tables and populating with data.
Lets say I have a 1 row table with 5 columns to start.
Will this row be for the headings only?
How do I then add the next row(s) and populate data, with adding
formfields
or by populating like you had indicated previously, and by identifying
which
table?

As much info as possible would be appreciated.

Thanks,
Bryan

:

I think that the better option might be to start with a one row table
for
each of those that might be required in the document, complete with
the
necessary row headings and with columns sized as required and in the
locations that you want them. Then use code to add rows to each table
as
required by the data that you want to insert into them. In the event
that
there is no data for some of the tables and they are thus not
required,
the
code could delete them.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
Hi Doug,
Just want to reiterate this:
I will have multiple table on this document:
1 for coverage info, 1 for lienholders, and 1 for endorsement forms.
Others to follow.

If I am inserting a table as I am going along, how do I position
this
to a
certain spot on the document?
I suppose I could use a bookmark for the heading above each and then
insert
and populate.
Still the question,
Sizing of each cell..........
Is that determined by the data length?

Thanks,
Bryan

:

Tables are numbered starting from the beginning of the document.
Therefore,
Table(1) is the first table in the document.

The following code will populate a table with data from an Access
database:

Dim myDataBase As Database
Dim myActiveRecord As Recordset
Dim i As Long
Dim dtable As Table, drow As Row
'Open a database
Set myDataBase = OpenDatabase("c:\Access\Procurement Plan.mdb")
'Access the first record from a particular table
Set myActiveRecord = myDataBase.OpenRecordset("Currencies",
dbOpenForwardOnly)
'Add a table to the document with one row and as many fields as
there
are
in
the database table
Set dtable = ActiveDocument.Tables.Add(Range:=Selection.Range,
NumRows:=1,
numcolumns:=myActiveRecord.Fields.Count)
Set drow = dtable.Rows(1)
'Loop through all the records in the table until the end-of-file
marker
is
reached
Do While Not myActiveRecord.EOF
'Populate the cells in the Word table with the data from the
current
record
For i = 1 To myActiveRecord.Fields.Count
drow.Cells(i).Range.Text = myActiveRecord.Fields(i - 1)
Next i
'Add a new row to the Word table and access the next record
Set drow = dtable.Rows.Add
myActiveRecord.MoveNext
Loop
'The last row will be empty, so delete it
drow.Delete
'Then close the database
myActiveRecord.Close
myDataBase.Close


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of
my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
2 part question:
1st part) If I have a template with multiple tables, how do I
know
which
is
table(1), table(2), etc. in order to work with

2nd part) On this form, 1 table will be for insurance vehicle
coverage.
Column 1 is coverage, col 2 is effectvie date, col3 is amount,
col4
is
deductible.
As I am reading a database I want to populate col1 - col4
formfileds.
Since I will have more than 1 coverage, how can I add the next
row(s)
and
populate those fileds.

After this I list the endorsement forms: same principle as above
I'll have a 1 row table and then add row/columns for each
endorsement
found

Or for the second part would it be easier to have each table
(lets
say
with
8 rows) and then populate and once complete then check for empty
at
8
and
delete that row and continue up until not empty.

Thanks in advance fro the help in this 2 part question.

Bryan
 
B

bryan

Hi Doug,
Works great but, now users want to add a twist and I am having a problem
with this.
I have the main template and then am inserting a document which has all the
tables.
Initially it was set that I used a combo box at first to ge the unit desired
and then inserted the doc and got the coverage, lienholder, and endorsment
info for that unit and populated the table.

Now they want to have it work where they can select multiple units and do
the same.
The problem is that when the second unit adds a document and then populates
table info, it just adds onto the first rather than on the new added doc.
Set dtable = ActiveDocument.Bookmarks("Coverage").Range.Tables(1)

Is there a way to rename this bookmark?
The bookmark is inside the table of the document which gets attached.

Thanks,
Bryan

Doug Robbins - Word MVP said:
It would be easiest just to have each one row table with the column heading
names inserted into the document/template and have it identified by a
bookmark (rather than try and do something with autotext). Having the table
itself allows equally well the sizing of the columns.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
bryan said:
See if I have this striaght to get started:
For each table I will have a bookmark for placement. Am I creating the
table
as I go or do I want a 1 row table in autotext with the headings to insert
at
bookmark and then add rows to that?
Having the one row as autotext would allow for sizing of columns. If using
autotext for the 1 row table, do I want the headings in this row?

Again, I truly appreciate all the help on this.

Thanks,
Bryan



Doug Robbins - Word MVP said:
You could identify each of the one row tables by assigning a bookmark to
them - e.g. Coverage, Lienholders, etc

The use something like

Dim myDataBase As Database
Dim myActiveRecord As Recordset
Dim i As Long
Dim dtable As Table, drow As Row
'Open a database
Set myDataBase = OpenDatabase("[Databasepath\filename.mdb]")
'Access the first record from a particular table
Set myActiveRecord = myDataBase.OpenRecordset("[tblNameforCoverageData]",
dbOpenForwardOnly)
'Add a table to the document with one row and as many fields as there are
in
the database table
Set dtable = ActiveDocument.Bookmarks("Coverage").Range.Tables(1)
'Loop through all the records in the table until the end-of-file marker
is
reached
Do While Not myActiveRecord.EOF
'Add a row to the table and populate the cells in that row with the
data
from the current
record
For i = 1 To myActiveRecord.Fields.Count
Set drow = dtable.Rows.Add
drow.Cells(i).Range.Text = myActiveRecord.Fields(i - 1)
Next i
myActiveRecord.MoveNext
Loop
'Then close the database
myActiveRecord.Close

'Access the first record from the next table
Set myActiveRecord =
myDataBase.OpenRecordset("[tblNameforLienHolderData]",
dbOpenForwardOnly)
'Add a table to the document with one row and as many fields as there are
in
the database table
Set dtable = ActiveDocument.Bookmarks("Lienholders").Range.Tables(1)
'Loop through all the records in the table until the end-of-file marker
is
reached
Do While Not myActiveRecord.EOF
'Add a row to the table and populate the cells in that row with the
data
from the current
record
For i = 1 To myActiveRecord.Fields.Count
Set drow = dtable.Rows.Add
drow.Cells(i).Range.Text = myActiveRecord.Fields(i - 1)
Next i
myActiveRecord.MoveNext
Loop
'Then close the database
myActiveRecord.Close

'etc.

Set myActiveRecord = Nothing
myDataBase.Close
Set myDataBase = Nothing


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
Hi Doug,
I appreciate the help but, need a little more to go on since I am new
on
dealing with adding tables and populating with data.
Lets say I have a 1 row table with 5 columns to start.
Will this row be for the headings only?
How do I then add the next row(s) and populate data, with adding
formfields
or by populating like you had indicated previously, and by identifying
which
table?

As much info as possible would be appreciated.

Thanks,
Bryan

:

I think that the better option might be to start with a one row table
for
each of those that might be required in the document, complete with
the
necessary row headings and with columns sized as required and in the
locations that you want them. Then use code to add rows to each table
as
required by the data that you want to insert into them. In the event
that
there is no data for some of the tables and they are thus not
required,
the
code could delete them.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
Hi Doug,
Just want to reiterate this:
I will have multiple table on this document:
1 for coverage info, 1 for lienholders, and 1 for endorsement forms.
Others to follow.

If I am inserting a table as I am going along, how do I position
this
to a
certain spot on the document?
I suppose I could use a bookmark for the heading above each and then
insert
and populate.
Still the question,
Sizing of each cell..........
Is that determined by the data length?

Thanks,
Bryan

:

Tables are numbered starting from the beginning of the document.
Therefore,
Table(1) is the first table in the document.

The following code will populate a table with data from an Access
database:

Dim myDataBase As Database
Dim myActiveRecord As Recordset
Dim i As Long
Dim dtable As Table, drow As Row
'Open a database
Set myDataBase = OpenDatabase("c:\Access\Procurement Plan.mdb")
'Access the first record from a particular table
Set myActiveRecord = myDataBase.OpenRecordset("Currencies",
dbOpenForwardOnly)
'Add a table to the document with one row and as many fields as
there
are
in
the database table
Set dtable = ActiveDocument.Tables.Add(Range:=Selection.Range,
NumRows:=1,
numcolumns:=myActiveRecord.Fields.Count)
Set drow = dtable.Rows(1)
'Loop through all the records in the table until the end-of-file
marker
is
reached
Do While Not myActiveRecord.EOF
'Populate the cells in the Word table with the data from the
current
record
For i = 1 To myActiveRecord.Fields.Count
drow.Cells(i).Range.Text = myActiveRecord.Fields(i - 1)
Next i
'Add a new row to the Word table and access the next record
Set drow = dtable.Rows.Add
myActiveRecord.MoveNext
Loop
'The last row will be empty, so delete it
drow.Delete
'Then close the database
myActiveRecord.Close
myDataBase.Close


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of
my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
2 part question:
1st part) If I have a template with multiple tables, how do I
know
which
is
table(1), table(2), etc. in order to work with

2nd part) On this form, 1 table will be for insurance vehicle
coverage.
Column 1 is coverage, col 2 is effectvie date, col3 is amount,
col4
is
deductible.
As I am reading a database I want to populate col1 - col4
formfileds.
Since I will have more than 1 coverage, how can I add the next
row(s)
and
populate those fileds.

After this I list the endorsement forms: same principle as above
I'll have a 1 row table and then add row/columns for each
endorsement
found

Or for the second part would it be easier to have each table
(lets
say
with
8 rows) and then populate and once complete then check for empty
at
8
and
delete that row and continue up until not empty.

Thanks in advance fro the help in this 2 part question.

Bryan
 
D

Doug Robbins - Word MVP

I am trouble by your statement "I have the main template and then am
inserting a document which has all the
tables"

I would have everything set up in the template and if they want to create a
document for another unit, they should be creating a new document from that
template.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
bryan said:
Hi Doug,
Works great but, now users want to add a twist and I am having a problem
with this.
I have the main template and then am inserting a document which has all
the
tables.
Initially it was set that I used a combo box at first to ge the unit
desired
and then inserted the doc and got the coverage, lienholder, and endorsment
info for that unit and populated the table.

Now they want to have it work where they can select multiple units and do
the same.
The problem is that when the second unit adds a document and then
populates
table info, it just adds onto the first rather than on the new added doc.
Set dtable = ActiveDocument.Bookmarks("Coverage").Range.Tables(1)

Is there a way to rename this bookmark?
The bookmark is inside the table of the document which gets attached.

Thanks,
Bryan

Doug Robbins - Word MVP said:
It would be easiest just to have each one row table with the column
heading
names inserted into the document/template and have it identified by a
bookmark (rather than try and do something with autotext). Having the
table
itself allows equally well the sizing of the columns.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
bryan said:
See if I have this striaght to get started:
For each table I will have a bookmark for placement. Am I creating the
table
as I go or do I want a 1 row table in autotext with the headings to
insert
at
bookmark and then add rows to that?
Having the one row as autotext would allow for sizing of columns. If
using
autotext for the 1 row table, do I want the headings in this row?

Again, I truly appreciate all the help on this.

Thanks,
Bryan



:

You could identify each of the one row tables by assigning a bookmark
to
them - e.g. Coverage, Lienholders, etc

The use something like

Dim myDataBase As Database
Dim myActiveRecord As Recordset
Dim i As Long
Dim dtable As Table, drow As Row
'Open a database
Set myDataBase = OpenDatabase("[Databasepath\filename.mdb]")
'Access the first record from a particular table
Set myActiveRecord =
myDataBase.OpenRecordset("[tblNameforCoverageData]",
dbOpenForwardOnly)
'Add a table to the document with one row and as many fields as there
are
in
the database table
Set dtable = ActiveDocument.Bookmarks("Coverage").Range.Tables(1)
'Loop through all the records in the table until the end-of-file
marker
is
reached
Do While Not myActiveRecord.EOF
'Add a row to the table and populate the cells in that row with
the
data
from the current
record
For i = 1 To myActiveRecord.Fields.Count
Set drow = dtable.Rows.Add
drow.Cells(i).Range.Text = myActiveRecord.Fields(i - 1)
Next i
myActiveRecord.MoveNext
Loop
'Then close the database
myActiveRecord.Close

'Access the first record from the next table
Set myActiveRecord =
myDataBase.OpenRecordset("[tblNameforLienHolderData]",
dbOpenForwardOnly)
'Add a table to the document with one row and as many fields as there
are
in
the database table
Set dtable = ActiveDocument.Bookmarks("Lienholders").Range.Tables(1)
'Loop through all the records in the table until the end-of-file
marker
is
reached
Do While Not myActiveRecord.EOF
'Add a row to the table and populate the cells in that row with
the
data
from the current
record
For i = 1 To myActiveRecord.Fields.Count
Set drow = dtable.Rows.Add
drow.Cells(i).Range.Text = myActiveRecord.Fields(i - 1)
Next i
myActiveRecord.MoveNext
Loop
'Then close the database
myActiveRecord.Close

'etc.

Set myActiveRecord = Nothing
myDataBase.Close
Set myDataBase = Nothing


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
Hi Doug,
I appreciate the help but, need a little more to go on since I am
new
on
dealing with adding tables and populating with data.
Lets say I have a 1 row table with 5 columns to start.
Will this row be for the headings only?
How do I then add the next row(s) and populate data, with adding
formfields
or by populating like you had indicated previously, and by
identifying
which
table?

As much info as possible would be appreciated.

Thanks,
Bryan

:

I think that the better option might be to start with a one row
table
for
each of those that might be required in the document, complete with
the
necessary row headings and with columns sized as required and in
the
locations that you want them. Then use code to add rows to each
table
as
required by the data that you want to insert into them. In the
event
that
there is no data for some of the tables and they are thus not
required,
the
code could delete them.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of
my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
Hi Doug,
Just want to reiterate this:
I will have multiple table on this document:
1 for coverage info, 1 for lienholders, and 1 for endorsement
forms.
Others to follow.

If I am inserting a table as I am going along, how do I position
this
to a
certain spot on the document?
I suppose I could use a bookmark for the heading above each and
then
insert
and populate.
Still the question,
Sizing of each cell..........
Is that determined by the data length?

Thanks,
Bryan

:

Tables are numbered starting from the beginning of the document.
Therefore,
Table(1) is the first table in the document.

The following code will populate a table with data from an
Access
database:

Dim myDataBase As Database
Dim myActiveRecord As Recordset
Dim i As Long
Dim dtable As Table, drow As Row
'Open a database
Set myDataBase = OpenDatabase("c:\Access\Procurement Plan.mdb")
'Access the first record from a particular table
Set myActiveRecord = myDataBase.OpenRecordset("Currencies",
dbOpenForwardOnly)
'Add a table to the document with one row and as many fields as
there
are
in
the database table
Set dtable = ActiveDocument.Tables.Add(Range:=Selection.Range,
NumRows:=1,
numcolumns:=myActiveRecord.Fields.Count)
Set drow = dtable.Rows(1)
'Loop through all the records in the table until the end-of-file
marker
is
reached
Do While Not myActiveRecord.EOF
'Populate the cells in the Word table with the data from the
current
record
For i = 1 To myActiveRecord.Fields.Count
drow.Cells(i).Range.Text = myActiveRecord.Fields(i - 1)
Next i
'Add a new row to the Word table and access the next record
Set drow = dtable.Rows.Add
myActiveRecord.MoveNext
Loop
'The last row will be empty, so delete it
drow.Delete
'Then close the database
myActiveRecord.Close
myDataBase.Close


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself
of
my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via
msnews.microsoft.com
2 part question:
1st part) If I have a template with multiple tables, how do I
know
which
is
table(1), table(2), etc. in order to work with

2nd part) On this form, 1 table will be for insurance vehicle
coverage.
Column 1 is coverage, col 2 is effectvie date, col3 is amount,
col4
is
deductible.
As I am reading a database I want to populate col1 - col4
formfileds.
Since I will have more than 1 coverage, how can I add the next
row(s)
and
populate those fileds.

After this I list the endorsement forms: same principle as
above
I'll have a 1 row table and then add row/columns for each
endorsement
found

Or for the second part would it be easier to have each table
(lets
say
with
8 rows) and then populate and once complete then check for
empty
at
8
and
delete that row and continue up until not empty.

Thanks in advance fro the help in this 2 part question.

Bryan
 
B

bryan

The tables are in a seperate document which I auto attached right away after
combo box indicating which vehicle so the info of coverages, leinholder, and
endorsements would populate for that 1 vehicle.
A seperate document for a variety of reasons.
Like I said it works great but, now they would like to select multiple
vehicles. With that in mind I need to attach multiple doc's.
I have played with this a bit by having a form field at each spot where I
want the table and then cursor to that field and then build the table like:

ActiveDocument.Bookmarks("Cov").Range.Fields(1).Result.Select

Set dtable = ActiveDocument.Tables.Add(Range:=Selection.Range,
NumRows:=1, numcolumns:=4, DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitContent)
dtable.AutoFormat ApplyBorders:=False, ApplyShading:=False,
AutoFit:=False
Set drow = dtable.Rows(1)
etc.

Cov would be renamed with each insertion then positioned to to build the
table.

This will work but, I was hoping to rename the actual bookmark within the
table so that formatting of the cells I could set and space out better using:

Set dtable = ActiveDocument.Bookmarks("Cov").Range.Tables(1)

Is it possible to rename the bookmark or do I have to use the other option
of having a form field to position to and then create a table?


Thanks,
Bryan


Doug Robbins - Word MVP said:
I am trouble by your statement "I have the main template and then am
inserting a document which has all the
tables"

I would have everything set up in the template and if they want to create a
document for another unit, they should be creating a new document from that
template.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
bryan said:
Hi Doug,
Works great but, now users want to add a twist and I am having a problem
with this.
I have the main template and then am inserting a document which has all
the
tables.
Initially it was set that I used a combo box at first to ge the unit
desired
and then inserted the doc and got the coverage, lienholder, and endorsment
info for that unit and populated the table.

Now they want to have it work where they can select multiple units and do
the same.
The problem is that when the second unit adds a document and then
populates
table info, it just adds onto the first rather than on the new added doc.
Set dtable = ActiveDocument.Bookmarks("Coverage").Range.Tables(1)

Is there a way to rename this bookmark?
The bookmark is inside the table of the document which gets attached.

Thanks,
Bryan

Doug Robbins - Word MVP said:
It would be easiest just to have each one row table with the column
heading
names inserted into the document/template and have it identified by a
bookmark (rather than try and do something with autotext). Having the
table
itself allows equally well the sizing of the columns.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
See if I have this striaght to get started:
For each table I will have a bookmark for placement. Am I creating the
table
as I go or do I want a 1 row table in autotext with the headings to
insert
at
bookmark and then add rows to that?
Having the one row as autotext would allow for sizing of columns. If
using
autotext for the 1 row table, do I want the headings in this row?

Again, I truly appreciate all the help on this.

Thanks,
Bryan



:

You could identify each of the one row tables by assigning a bookmark
to
them - e.g. Coverage, Lienholders, etc

The use something like

Dim myDataBase As Database
Dim myActiveRecord As Recordset
Dim i As Long
Dim dtable As Table, drow As Row
'Open a database
Set myDataBase = OpenDatabase("[Databasepath\filename.mdb]")
'Access the first record from a particular table
Set myActiveRecord =
myDataBase.OpenRecordset("[tblNameforCoverageData]",
dbOpenForwardOnly)
'Add a table to the document with one row and as many fields as there
are
in
the database table
Set dtable = ActiveDocument.Bookmarks("Coverage").Range.Tables(1)
'Loop through all the records in the table until the end-of-file
marker
is
reached
Do While Not myActiveRecord.EOF
'Add a row to the table and populate the cells in that row with
the
data
from the current
record
For i = 1 To myActiveRecord.Fields.Count
Set drow = dtable.Rows.Add
drow.Cells(i).Range.Text = myActiveRecord.Fields(i - 1)
Next i
myActiveRecord.MoveNext
Loop
'Then close the database
myActiveRecord.Close

'Access the first record from the next table
Set myActiveRecord =
myDataBase.OpenRecordset("[tblNameforLienHolderData]",
dbOpenForwardOnly)
'Add a table to the document with one row and as many fields as there
are
in
the database table
Set dtable = ActiveDocument.Bookmarks("Lienholders").Range.Tables(1)
'Loop through all the records in the table until the end-of-file
marker
is
reached
Do While Not myActiveRecord.EOF
'Add a row to the table and populate the cells in that row with
the
data
from the current
record
For i = 1 To myActiveRecord.Fields.Count
Set drow = dtable.Rows.Add
drow.Cells(i).Range.Text = myActiveRecord.Fields(i - 1)
Next i
myActiveRecord.MoveNext
Loop
'Then close the database
myActiveRecord.Close

'etc.

Set myActiveRecord = Nothing
myDataBase.Close
Set myDataBase = Nothing


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
Hi Doug,
I appreciate the help but, need a little more to go on since I am
new
on
dealing with adding tables and populating with data.
Lets say I have a 1 row table with 5 columns to start.
Will this row be for the headings only?
How do I then add the next row(s) and populate data, with adding
formfields
or by populating like you had indicated previously, and by
identifying
which
table?

As much info as possible would be appreciated.

Thanks,
Bryan

:

I think that the better option might be to start with a one row
table
for
each of those that might be required in the document, complete with
the
necessary row headings and with columns sized as required and in
the
locations that you want them. Then use code to add rows to each
table
as
required by the data that you want to insert into them. In the
event
that
there is no data for some of the tables and they are thus not
required,
the
code could delete them.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of
my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
Hi Doug,
Just want to reiterate this:
I will have multiple table on this document:
1 for coverage info, 1 for lienholders, and 1 for endorsement
forms.
Others to follow.

If I am inserting a table as I am going along, how do I position
this
to a
certain spot on the document?
I suppose I could use a bookmark for the heading above each and
then
insert
and populate.
Still the question,
Sizing of each cell..........
Is that determined by the data length?

Thanks,
Bryan

:

Tables are numbered starting from the beginning of the document.
Therefore,
Table(1) is the first table in the document.

The following code will populate a table with data from an
Access
database:

Dim myDataBase As Database
Dim myActiveRecord As Recordset
Dim i As Long
Dim dtable As Table, drow As Row
'Open a database
Set myDataBase = OpenDatabase("c:\Access\Procurement Plan.mdb")
'Access the first record from a particular table
Set myActiveRecord = myDataBase.OpenRecordset("Currencies",
dbOpenForwardOnly)
'Add a table to the document with one row and as many fields as
there
are
in
the database table
Set dtable = ActiveDocument.Tables.Add(Range:=Selection.Range,
NumRows:=1,
numcolumns:=myActiveRecord.Fields.Count)
Set drow = dtable.Rows(1)
'Loop through all the records in the table until the end-of-file
marker
is
reached
Do While Not myActiveRecord.EOF
'Populate the cells in the Word table with the data from the
current
record
For i = 1 To myActiveRecord.Fields.Count
drow.Cells(i).Range.Text = myActiveRecord.Fields(i - 1)
Next i
'Add a new row to the Word table and access the next record
Set drow = dtable.Rows.Add
myActiveRecord.MoveNext
Loop
'The last row will be empty, so delete it
drow.Delete
'Then close the database
myActiveRecord.Close
myDataBase.Close


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself
of
my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via
msnews.microsoft.com
2 part question:
1st part) If I have a template with multiple tables, how do I
know
which
 
D

Doug Robbins - Word MVP

I do not see why you would need the bookmark. You already have a handle to
the table by the use of the dtable = ActiveDocument.Tables.Add command.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
bryan said:
The tables are in a seperate document which I auto attached right away
after
combo box indicating which vehicle so the info of coverages, leinholder,
and
endorsements would populate for that 1 vehicle.
A seperate document for a variety of reasons.
Like I said it works great but, now they would like to select multiple
vehicles. With that in mind I need to attach multiple doc's.
I have played with this a bit by having a form field at each spot where I
want the table and then cursor to that field and then build the table
like:

ActiveDocument.Bookmarks("Cov").Range.Fields(1).Result.Select

Set dtable = ActiveDocument.Tables.Add(Range:=Selection.Range,
NumRows:=1, numcolumns:=4, DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitContent)
dtable.AutoFormat ApplyBorders:=False, ApplyShading:=False,
AutoFit:=False
Set drow = dtable.Rows(1)
etc.

Cov would be renamed with each insertion then positioned to to build the
table.

This will work but, I was hoping to rename the actual bookmark within the
table so that formatting of the cells I could set and space out better
using:

Set dtable = ActiveDocument.Bookmarks("Cov").Range.Tables(1)

Is it possible to rename the bookmark or do I have to use the other option
of having a form field to position to and then create a table?


Thanks,
Bryan


Doug Robbins - Word MVP said:
I am trouble by your statement "I have the main template and then am
inserting a document which has all the
tables"

I would have everything set up in the template and if they want to create
a
document for another unit, they should be creating a new document from
that
template.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
bryan said:
Hi Doug,
Works great but, now users want to add a twist and I am having a
problem
with this.
I have the main template and then am inserting a document which has all
the
tables.
Initially it was set that I used a combo box at first to ge the unit
desired
and then inserted the doc and got the coverage, lienholder, and
endorsment
info for that unit and populated the table.

Now they want to have it work where they can select multiple units and
do
the same.
The problem is that when the second unit adds a document and then
populates
table info, it just adds onto the first rather than on the new added
doc.
Set dtable = ActiveDocument.Bookmarks("Coverage").Range.Tables(1)

Is there a way to rename this bookmark?
The bookmark is inside the table of the document which gets attached.

Thanks,
Bryan

:

It would be easiest just to have each one row table with the column
heading
names inserted into the document/template and have it identified by a
bookmark (rather than try and do something with autotext). Having the
table
itself allows equally well the sizing of the columns.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
See if I have this striaght to get started:
For each table I will have a bookmark for placement. Am I creating
the
table
as I go or do I want a 1 row table in autotext with the headings to
insert
at
bookmark and then add rows to that?
Having the one row as autotext would allow for sizing of columns. If
using
autotext for the 1 row table, do I want the headings in this row?

Again, I truly appreciate all the help on this.

Thanks,
Bryan



:

You could identify each of the one row tables by assigning a
bookmark
to
them - e.g. Coverage, Lienholders, etc

The use something like

Dim myDataBase As Database
Dim myActiveRecord As Recordset
Dim i As Long
Dim dtable As Table, drow As Row
'Open a database
Set myDataBase = OpenDatabase("[Databasepath\filename.mdb]")
'Access the first record from a particular table
Set myActiveRecord =
myDataBase.OpenRecordset("[tblNameforCoverageData]",
dbOpenForwardOnly)
'Add a table to the document with one row and as many fields as
there
are
in
the database table
Set dtable = ActiveDocument.Bookmarks("Coverage").Range.Tables(1)
'Loop through all the records in the table until the end-of-file
marker
is
reached
Do While Not myActiveRecord.EOF
'Add a row to the table and populate the cells in that row with
the
data
from the current
record
For i = 1 To myActiveRecord.Fields.Count
Set drow = dtable.Rows.Add
drow.Cells(i).Range.Text = myActiveRecord.Fields(i - 1)
Next i
myActiveRecord.MoveNext
Loop
'Then close the database
myActiveRecord.Close

'Access the first record from the next table
Set myActiveRecord =
myDataBase.OpenRecordset("[tblNameforLienHolderData]",
dbOpenForwardOnly)
'Add a table to the document with one row and as many fields as
there
are
in
the database table
Set dtable =
ActiveDocument.Bookmarks("Lienholders").Range.Tables(1)
'Loop through all the records in the table until the end-of-file
marker
is
reached
Do While Not myActiveRecord.EOF
'Add a row to the table and populate the cells in that row with
the
data
from the current
record
For i = 1 To myActiveRecord.Fields.Count
Set drow = dtable.Rows.Add
drow.Cells(i).Range.Text = myActiveRecord.Fields(i - 1)
Next i
myActiveRecord.MoveNext
Loop
'Then close the database
myActiveRecord.Close

'etc.

Set myActiveRecord = Nothing
myDataBase.Close
Set myDataBase = Nothing


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of
my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
Hi Doug,
I appreciate the help but, need a little more to go on since I am
new
on
dealing with adding tables and populating with data.
Lets say I have a 1 row table with 5 columns to start.
Will this row be for the headings only?
How do I then add the next row(s) and populate data, with adding
formfields
or by populating like you had indicated previously, and by
identifying
which
table?

As much info as possible would be appreciated.

Thanks,
Bryan

:

I think that the better option might be to start with a one row
table
for
each of those that might be required in the document, complete
with
the
necessary row headings and with columns sized as required and in
the
locations that you want them. Then use code to add rows to each
table
as
required by the data that you want to insert into them. In the
event
that
there is no data for some of the tables and they are thus not
required,
the
code could delete them.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself
of
my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via
msnews.microsoft.com
Hi Doug,
Just want to reiterate this:
I will have multiple table on this document:
1 for coverage info, 1 for lienholders, and 1 for endorsement
forms.
Others to follow.

If I am inserting a table as I am going along, how do I
position
this
to a
certain spot on the document?
I suppose I could use a bookmark for the heading above each
and
then
insert
and populate.
Still the question,
Sizing of each cell..........
Is that determined by the data length?

Thanks,
Bryan

:

Tables are numbered starting from the beginning of the
document.
Therefore,
Table(1) is the first table in the document.

The following code will populate a table with data from an
Access
database:

Dim myDataBase As Database
Dim myActiveRecord As Recordset
Dim i As Long
Dim dtable As Table, drow As Row
'Open a database
Set myDataBase = OpenDatabase("c:\Access\Procurement
Plan.mdb")
'Access the first record from a particular table
Set myActiveRecord = myDataBase.OpenRecordset("Currencies",
dbOpenForwardOnly)
'Add a table to the document with one row and as many fields
as
there
are
in
the database table
Set dtable =
ActiveDocument.Tables.Add(Range:=Selection.Range,
NumRows:=1,
numcolumns:=myActiveRecord.Fields.Count)
Set drow = dtable.Rows(1)
'Loop through all the records in the table until the
end-of-file
marker
is
reached
Do While Not myActiveRecord.EOF
'Populate the cells in the Word table with the data from
the
current
record
For i = 1 To myActiveRecord.Fields.Count
drow.Cells(i).Range.Text = myActiveRecord.Fields(i -
1)
Next i
'Add a new row to the Word table and access the next
record
Set drow = dtable.Rows.Add
myActiveRecord.MoveNext
Loop
'The last row will be empty, so delete it
drow.Delete
'Then close the database
myActiveRecord.Close
myDataBase.Close


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail
yourself
of
my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via
msnews.microsoft.com
2 part question:
1st part) If I have a template with multiple tables, how do
I
know
which
 
B

bryan

In previous threads I indicated a need to position these tables.
I was able to accomplish this by inserting a bookmark inside a predefined 1
row table on the document which is added to the template. This worked great
if there were 1 vehicle to add. With multiple I need to rename the formfields
which I can do but, the question is:
Is there a way to rename a bokmark which has been inserted. This would be
ideal as then I can predefine each table cell format.

The work around I came up with was to have a formfield on the document as a
reference point, able to rename that and position to to add the table. Works
but, not the preferred method as I have less control of format of each row
cell.

Possible to rename an inserted bookmark?

Thanks,
Bryan

Doug Robbins - Word MVP said:
I do not see why you would need the bookmark. You already have a handle to
the table by the use of the dtable = ActiveDocument.Tables.Add command.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
bryan said:
The tables are in a seperate document which I auto attached right away
after
combo box indicating which vehicle so the info of coverages, leinholder,
and
endorsements would populate for that 1 vehicle.
A seperate document for a variety of reasons.
Like I said it works great but, now they would like to select multiple
vehicles. With that in mind I need to attach multiple doc's.
I have played with this a bit by having a form field at each spot where I
want the table and then cursor to that field and then build the table
like:

ActiveDocument.Bookmarks("Cov").Range.Fields(1).Result.Select

Set dtable = ActiveDocument.Tables.Add(Range:=Selection.Range,
NumRows:=1, numcolumns:=4, DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitContent)
dtable.AutoFormat ApplyBorders:=False, ApplyShading:=False,
AutoFit:=False
Set drow = dtable.Rows(1)
etc.

Cov would be renamed with each insertion then positioned to to build the
table.

This will work but, I was hoping to rename the actual bookmark within the
table so that formatting of the cells I could set and space out better
using:

Set dtable = ActiveDocument.Bookmarks("Cov").Range.Tables(1)

Is it possible to rename the bookmark or do I have to use the other option
of having a form field to position to and then create a table?


Thanks,
Bryan


Doug Robbins - Word MVP said:
I am trouble by your statement "I have the main template and then am
inserting a document which has all the
tables"

I would have everything set up in the template and if they want to create
a
document for another unit, they should be creating a new document from
that
template.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
Hi Doug,
Works great but, now users want to add a twist and I am having a
problem
with this.
I have the main template and then am inserting a document which has all
the
tables.
Initially it was set that I used a combo box at first to ge the unit
desired
and then inserted the doc and got the coverage, lienholder, and
endorsment
info for that unit and populated the table.

Now they want to have it work where they can select multiple units and
do
the same.
The problem is that when the second unit adds a document and then
populates
table info, it just adds onto the first rather than on the new added
doc.
Set dtable = ActiveDocument.Bookmarks("Coverage").Range.Tables(1)

Is there a way to rename this bookmark?
The bookmark is inside the table of the document which gets attached.

Thanks,
Bryan

:

It would be easiest just to have each one row table with the column
heading
names inserted into the document/template and have it identified by a
bookmark (rather than try and do something with autotext). Having the
table
itself allows equally well the sizing of the columns.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
See if I have this striaght to get started:
For each table I will have a bookmark for placement. Am I creating
the
table
as I go or do I want a 1 row table in autotext with the headings to
insert
at
bookmark and then add rows to that?
Having the one row as autotext would allow for sizing of columns. If
using
autotext for the 1 row table, do I want the headings in this row?

Again, I truly appreciate all the help on this.

Thanks,
Bryan



:

You could identify each of the one row tables by assigning a
bookmark
to
them - e.g. Coverage, Lienholders, etc

The use something like

Dim myDataBase As Database
Dim myActiveRecord As Recordset
Dim i As Long
Dim dtable As Table, drow As Row
'Open a database
Set myDataBase = OpenDatabase("[Databasepath\filename.mdb]")
'Access the first record from a particular table
Set myActiveRecord =
myDataBase.OpenRecordset("[tblNameforCoverageData]",
dbOpenForwardOnly)
'Add a table to the document with one row and as many fields as
there
are
in
the database table
Set dtable = ActiveDocument.Bookmarks("Coverage").Range.Tables(1)
'Loop through all the records in the table until the end-of-file
marker
is
reached
Do While Not myActiveRecord.EOF
'Add a row to the table and populate the cells in that row with
the
data
from the current
record
For i = 1 To myActiveRecord.Fields.Count
Set drow = dtable.Rows.Add
drow.Cells(i).Range.Text = myActiveRecord.Fields(i - 1)
Next i
myActiveRecord.MoveNext
Loop
'Then close the database
myActiveRecord.Close

'Access the first record from the next table
Set myActiveRecord =
myDataBase.OpenRecordset("[tblNameforLienHolderData]",
dbOpenForwardOnly)
'Add a table to the document with one row and as many fields as
there
are
in
the database table
Set dtable =
ActiveDocument.Bookmarks("Lienholders").Range.Tables(1)
'Loop through all the records in the table until the end-of-file
marker
is
reached
Do While Not myActiveRecord.EOF
'Add a row to the table and populate the cells in that row with
the
data
from the current
record
For i = 1 To myActiveRecord.Fields.Count
Set drow = dtable.Rows.Add
drow.Cells(i).Range.Text = myActiveRecord.Fields(i - 1)
Next i
myActiveRecord.MoveNext
Loop
'Then close the database
myActiveRecord.Close

'etc.

Set myActiveRecord = Nothing
myDataBase.Close
Set myDataBase = Nothing


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of
my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
Hi Doug,
I appreciate the help but, need a little more to go on since I am
new
on
dealing with adding tables and populating with data.
Lets say I have a 1 row table with 5 columns to start.
Will this row be for the headings only?
How do I then add the next row(s) and populate data, with adding
formfields
or by populating like you had indicated previously, and by
identifying
which
table?

As much info as possible would be appreciated.

Thanks,
Bryan

:

I think that the better option might be to start with a one row
table
for
each of those that might be required in the document, complete
with
the
necessary row headings and with columns sized as required and in
the
locations that you want them. Then use code to add rows to each
table
as
required by the data that you want to insert into them. In the
event
that
there is no data for some of the tables and they are thus not
required,
the
code could delete them.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself
of
my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via
msnews.microsoft.com
Hi Doug,
Just want to reiterate this:
I will have multiple table on this document:
1 for coverage info, 1 for lienholders, and 1 for endorsement
forms.
Others to follow.

If I am inserting a table as I am going along, how do I
position
this
to a
certain spot on the document?
I suppose I could use a bookmark for the heading above each
and
then
insert
and populate.
Still the question,
Sizing of each cell..........
Is that determined by the data length?
 
D

Doug Robbins - Word MVP

You cannot rename a bookmark using VBA, but you can delete it and create a
new bookmark at the same location as follows:

Dim myrange As Range

With ActiveDocument
Set myrange = .Bookmarks("Test").Range
myrange.Bookmarks(1).Delete
.Bookmarks.Add Name:="Test1"
End With

You would have to do this every time that you insert the table (including
the first time)

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
bryan said:
In previous threads I indicated a need to position these tables.
I was able to accomplish this by inserting a bookmark inside a predefined
1
row table on the document which is added to the template. This worked
great
if there were 1 vehicle to add. With multiple I need to rename the
formfields
which I can do but, the question is:
Is there a way to rename a bokmark which has been inserted. This would be
ideal as then I can predefine each table cell format.

The work around I came up with was to have a formfield on the document as
a
reference point, able to rename that and position to to add the table.
Works
but, not the preferred method as I have less control of format of each row
cell.

Possible to rename an inserted bookmark?

Thanks,
Bryan

Doug Robbins - Word MVP said:
I do not see why you would need the bookmark. You already have a handle
to
the table by the use of the dtable = ActiveDocument.Tables.Add command.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
bryan said:
The tables are in a seperate document which I auto attached right away
after
combo box indicating which vehicle so the info of coverages,
leinholder,
and
endorsements would populate for that 1 vehicle.
A seperate document for a variety of reasons.
Like I said it works great but, now they would like to select multiple
vehicles. With that in mind I need to attach multiple doc's.
I have played with this a bit by having a form field at each spot where
I
want the table and then cursor to that field and then build the table
like:

ActiveDocument.Bookmarks("Cov").Range.Fields(1).Result.Select

Set dtable = ActiveDocument.Tables.Add(Range:=Selection.Range,
NumRows:=1, numcolumns:=4, DefaultTableBehavior:=wdWord9TableBehavior,
_
AutoFitBehavior:=wdAutoFitContent)
dtable.AutoFormat ApplyBorders:=False, ApplyShading:=False,
AutoFit:=False
Set drow = dtable.Rows(1)
etc.

Cov would be renamed with each insertion then positioned to to build
the
table.

This will work but, I was hoping to rename the actual bookmark within
the
table so that formatting of the cells I could set and space out better
using:

Set dtable = ActiveDocument.Bookmarks("Cov").Range.Tables(1)

Is it possible to rename the bookmark or do I have to use the other
option
of having a form field to position to and then create a table?


Thanks,
Bryan


:

I am trouble by your statement "I have the main template and then am
inserting a document which has all the
tables"

I would have everything set up in the template and if they want to
create
a
document for another unit, they should be creating a new document from
that
template.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
Hi Doug,
Works great but, now users want to add a twist and I am having a
problem
with this.
I have the main template and then am inserting a document which has
all
the
tables.
Initially it was set that I used a combo box at first to ge the unit
desired
and then inserted the doc and got the coverage, lienholder, and
endorsment
info for that unit and populated the table.

Now they want to have it work where they can select multiple units
and
do
the same.
The problem is that when the second unit adds a document and then
populates
table info, it just adds onto the first rather than on the new added
doc.
Set dtable = ActiveDocument.Bookmarks("Coverage").Range.Tables(1)

Is there a way to rename this bookmark?
The bookmark is inside the table of the document which gets
attached.

Thanks,
Bryan

:

It would be easiest just to have each one row table with the
column
heading
names inserted into the document/template and have it identified by
a
bookmark (rather than try and do something with autotext). Having
the
table
itself allows equally well the sizing of the columns.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of
my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
See if I have this striaght to get started:
For each table I will have a bookmark for placement. Am I
creating
the
table
as I go or do I want a 1 row table in autotext with the headings
to
insert
at
bookmark and then add rows to that?
Having the one row as autotext would allow for sizing of columns.
If
using
autotext for the 1 row table, do I want the headings in this row?

Again, I truly appreciate all the help on this.

Thanks,
Bryan



:

You could identify each of the one row tables by assigning a
bookmark
to
them - e.g. Coverage, Lienholders, etc

The use something like

Dim myDataBase As Database
Dim myActiveRecord As Recordset
Dim i As Long
Dim dtable As Table, drow As Row
'Open a database
Set myDataBase = OpenDatabase("[Databasepath\filename.mdb]")
'Access the first record from a particular table
Set myActiveRecord =
myDataBase.OpenRecordset("[tblNameforCoverageData]",
dbOpenForwardOnly)
'Add a table to the document with one row and as many fields as
there
are
in
the database table
Set dtable =
ActiveDocument.Bookmarks("Coverage").Range.Tables(1)
'Loop through all the records in the table until the end-of-file
marker
is
reached
Do While Not myActiveRecord.EOF
'Add a row to the table and populate the cells in that row
with
the
data
from the current
record
For i = 1 To myActiveRecord.Fields.Count
Set drow = dtable.Rows.Add
drow.Cells(i).Range.Text = myActiveRecord.Fields(i - 1)
Next i
myActiveRecord.MoveNext
Loop
'Then close the database
myActiveRecord.Close

'Access the first record from the next table
Set myActiveRecord =
myDataBase.OpenRecordset("[tblNameforLienHolderData]",
dbOpenForwardOnly)
'Add a table to the document with one row and as many fields as
there
are
in
the database table
Set dtable =
ActiveDocument.Bookmarks("Lienholders").Range.Tables(1)
'Loop through all the records in the table until the end-of-file
marker
is
reached
Do While Not myActiveRecord.EOF
'Add a row to the table and populate the cells in that row
with
the
data
from the current
record
For i = 1 To myActiveRecord.Fields.Count
Set drow = dtable.Rows.Add
drow.Cells(i).Range.Text = myActiveRecord.Fields(i - 1)
Next i
myActiveRecord.MoveNext
Loop
'Then close the database
myActiveRecord.Close

'etc.

Set myActiveRecord = Nothing
myDataBase.Close
Set myDataBase = Nothing


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself
of
my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via
msnews.microsoft.com
Hi Doug,
I appreciate the help but, need a little more to go on since I
am
new
on
dealing with adding tables and populating with data.
Lets say I have a 1 row table with 5 columns to start.
Will this row be for the headings only?
How do I then add the next row(s) and populate data, with
adding
formfields
or by populating like you had indicated previously, and by
identifying
which
table?

As much info as possible would be appreciated.

Thanks,
Bryan

:

I think that the better option might be to start with a one
row
table
for
each of those that might be required in the document,
complete
with
the
necessary row headings and with columns sized as required and
in
the
locations that you want them. Then use code to add rows to
each
table
as
required by the data that you want to insert into them. In
the
event
that
there is no data for some of the tables and they are thus not
required,
the
code could delete them.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail
yourself
of
my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via
msnews.microsoft.com
Hi Doug,
Just want to reiterate this:
I will have multiple table on this document:
1 for coverage info, 1 for lienholders, and 1 for
endorsement
forms.
Others to follow.

If I am inserting a table as I am going along, how do I
position
this
to a
certain spot on the document?
I suppose I could use a bookmark for the heading above each
and
then
insert
and populate.
Still the question,
Sizing of each cell..........
Is that determined by the data length?
 
B

bryan

This deletes the bookmark but does not create the bookmark at the same
position.
It deletes it from the attached document but, creates it at the beginning of
the template, not in the position of the bookmark in the document of which I
deleted.

I tried this with another bookmark which will change as well for each.
Here is the code where I attach the document. I am deleting HeaderA and used
Header11 to test. It will actually be "Header" and "unit number".

Dim p As Long
Dim Attachcnt As Integer
vehicles = getveh(stringy)
'vehicle start
docoa = "Attach1.doc"

Dim myDoc As Document
Dim docrange As Range
array1 = Split(vehicles, ",")
For Each Item In array1
strunit = Trim(Item)
strunit = Trim(Left(strunit, 3))
Attachcnt = Attachcnt + 1
Set myDoc = ActiveDocument
With myDoc
..Unprotect
With .Range
..Collapse wdCollapseEnd
..InsertBreak wdSectionBreakNextPage
..InsertFile stringfile & docoa
With .Sections.Last
For i = 1 To 3
With .Headers.Item(i)
If .LinkToPrevious Then
..LinkToPrevious = False
..Range.Delete
End If
End With
With .Footers.Item(i)
If .LinkToPrevious Then
..LinkToPrevious = False
..Range.Delete
End If
End With
Next i
End With
End With

'.Protect wdAllowOnlyFormFields, True
End With
'rename fields
With myDoc.Sections.Last.Range
For z = 1 To .FormFields.Count
.FormFields(z).Name = "At" & strunit & z
Next z
End With

'remove bookmark of header
Dim myrange As Range

With ActiveDocument
Set myrange = .Bookmarks("HeaderA").Range
myrange.Bookmarks(1).Delete
.Bookmarks.Add Name:="Header11"
End With

Set myrange = ActiveDocument.Bookmarks("Header11").Range
ActiveDocument.AttachedTemplate.AutoTextEntries(cologo).Insert
WHERE:=myrange, RichText:=True
ActiveDocument.Bookmarks.Add _
Name:="Header11", _
Range:=myrange

.....more stuff - sql files to get info and move to renamed form fields
myDoc.Protect wdAllowOnlyFormFields, NoReset

Next


Doug Robbins - Word MVP said:
You cannot rename a bookmark using VBA, but you can delete it and create a
new bookmark at the same location as follows:

Dim myrange As Range

With ActiveDocument
Set myrange = .Bookmarks("Test").Range
myrange.Bookmarks(1).Delete
.Bookmarks.Add Name:="Test1"
End With

You would have to do this every time that you insert the table (including
the first time)

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
bryan said:
In previous threads I indicated a need to position these tables.
I was able to accomplish this by inserting a bookmark inside a predefined
1
row table on the document which is added to the template. This worked
great
if there were 1 vehicle to add. With multiple I need to rename the
formfields
which I can do but, the question is:
Is there a way to rename a bokmark which has been inserted. This would be
ideal as then I can predefine each table cell format.

The work around I came up with was to have a formfield on the document as
a
reference point, able to rename that and position to to add the table.
Works
but, not the preferred method as I have less control of format of each row
cell.

Possible to rename an inserted bookmark?

Thanks,
Bryan

Doug Robbins - Word MVP said:
I do not see why you would need the bookmark. You already have a handle
to
the table by the use of the dtable = ActiveDocument.Tables.Add command.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
The tables are in a seperate document which I auto attached right away
after
combo box indicating which vehicle so the info of coverages,
leinholder,
and
endorsements would populate for that 1 vehicle.
A seperate document for a variety of reasons.
Like I said it works great but, now they would like to select multiple
vehicles. With that in mind I need to attach multiple doc's.
I have played with this a bit by having a form field at each spot where
I
want the table and then cursor to that field and then build the table
like:

ActiveDocument.Bookmarks("Cov").Range.Fields(1).Result.Select

Set dtable = ActiveDocument.Tables.Add(Range:=Selection.Range,
NumRows:=1, numcolumns:=4, DefaultTableBehavior:=wdWord9TableBehavior,
_
AutoFitBehavior:=wdAutoFitContent)
dtable.AutoFormat ApplyBorders:=False, ApplyShading:=False,
AutoFit:=False
Set drow = dtable.Rows(1)
etc.

Cov would be renamed with each insertion then positioned to to build
the
table.

This will work but, I was hoping to rename the actual bookmark within
the
table so that formatting of the cells I could set and space out better
using:

Set dtable = ActiveDocument.Bookmarks("Cov").Range.Tables(1)

Is it possible to rename the bookmark or do I have to use the other
option
of having a form field to position to and then create a table?


Thanks,
Bryan


:

I am trouble by your statement "I have the main template and then am
inserting a document which has all the
tables"

I would have everything set up in the template and if they want to
create
a
document for another unit, they should be creating a new document from
that
template.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
Hi Doug,
Works great but, now users want to add a twist and I am having a
problem
with this.
I have the main template and then am inserting a document which has
all
the
tables.
Initially it was set that I used a combo box at first to ge the unit
desired
and then inserted the doc and got the coverage, lienholder, and
endorsment
info for that unit and populated the table.

Now they want to have it work where they can select multiple units
and
do
the same.
The problem is that when the second unit adds a document and then
populates
table info, it just adds onto the first rather than on the new added
doc.
Set dtable = ActiveDocument.Bookmarks("Coverage").Range.Tables(1)

Is there a way to rename this bookmark?
The bookmark is inside the table of the document which gets
attached.

Thanks,
Bryan

:

It would be easiest just to have each one row table with the
column
heading
names inserted into the document/template and have it identified by
a
bookmark (rather than try and do something with autotext). Having
the
table
itself allows equally well the sizing of the columns.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of
my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
See if I have this striaght to get started:
For each table I will have a bookmark for placement. Am I
creating
the
table
as I go or do I want a 1 row table in autotext with the headings
to
insert
at
bookmark and then add rows to that?
Having the one row as autotext would allow for sizing of columns.
If
using
autotext for the 1 row table, do I want the headings in this row?

Again, I truly appreciate all the help on this.

Thanks,
Bryan



:

You could identify each of the one row tables by assigning a
bookmark
to
them - e.g. Coverage, Lienholders, etc

The use something like

Dim myDataBase As Database
Dim myActiveRecord As Recordset
Dim i As Long
Dim dtable As Table, drow As Row
'Open a database
Set myDataBase = OpenDatabase("[Databasepath\filename.mdb]")
'Access the first record from a particular table
Set myActiveRecord =
myDataBase.OpenRecordset("[tblNameforCoverageData]",
dbOpenForwardOnly)
'Add a table to the document with one row and as many fields as
there
are
in
the database table
Set dtable =
ActiveDocument.Bookmarks("Coverage").Range.Tables(1)
'Loop through all the records in the table until the end-of-file
marker
is
reached
Do While Not myActiveRecord.EOF
'Add a row to the table and populate the cells in that row
with
the
data
from the current
record
For i = 1 To myActiveRecord.Fields.Count
Set drow = dtable.Rows.Add
drow.Cells(i).Range.Text = myActiveRecord.Fields(i - 1)
Next i
myActiveRecord.MoveNext
Loop
'Then close the database
myActiveRecord.Close

'Access the first record from the next table
Set myActiveRecord =
myDataBase.OpenRecordset("[tblNameforLienHolderData]",
dbOpenForwardOnly)
'Add a table to the document with one row and as many fields as
there
are
in
the database table
Set dtable =
ActiveDocument.Bookmarks("Lienholders").Range.Tables(1)
'Loop through all the records in the table until the end-of-file
marker
is
reached
Do While Not myActiveRecord.EOF
'Add a row to the table and populate the cells in that row
with
the
data
from the current
record
For i = 1 To myActiveRecord.Fields.Count
Set drow = dtable.Rows.Add
drow.Cells(i).Range.Text = myActiveRecord.Fields(i - 1)
Next i
myActiveRecord.MoveNext
Loop
'Then close the database
myActiveRecord.Close

'etc.

Set myActiveRecord = Nothing
myDataBase.Close
Set myDataBase = Nothing


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself
of
my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via
msnews.microsoft.com
Hi Doug,
 
D

Doug Robbins - Word MVP

Sorry, the code should have been:

Dim myrange As Range
With ActiveDocument
Set myrange = .Bookmarks("Test").Range
myrange.Bookmarks(1).Delete
.Bookmarks.Add Name:="Test1", Range:=myrange
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
bryan said:
This deletes the bookmark but does not create the bookmark at the same
position.
It deletes it from the attached document but, creates it at the beginning
of
the template, not in the position of the bookmark in the document of which
I
deleted.

I tried this with another bookmark which will change as well for each.
Here is the code where I attach the document. I am deleting HeaderA and
used
Header11 to test. It will actually be "Header" and "unit number".

Dim p As Long
Dim Attachcnt As Integer
vehicles = getveh(stringy)
'vehicle start
docoa = "Attach1.doc"

Dim myDoc As Document
Dim docrange As Range
array1 = Split(vehicles, ",")
For Each Item In array1
strunit = Trim(Item)
strunit = Trim(Left(strunit, 3))
Attachcnt = Attachcnt + 1
Set myDoc = ActiveDocument
With myDoc
.Unprotect
With .Range
.Collapse wdCollapseEnd
.InsertBreak wdSectionBreakNextPage
.InsertFile stringfile & docoa
With .Sections.Last
For i = 1 To 3
With .Headers.Item(i)
If .LinkToPrevious Then
.LinkToPrevious = False
.Range.Delete
End If
End With
With .Footers.Item(i)
If .LinkToPrevious Then
.LinkToPrevious = False
.Range.Delete
End If
End With
Next i
End With
End With

'.Protect wdAllowOnlyFormFields, True
End With
'rename fields
With myDoc.Sections.Last.Range
For z = 1 To .FormFields.Count
.FormFields(z).Name = "At" & strunit & z
Next z
End With

'remove bookmark of header
Dim myrange As Range

With ActiveDocument
Set myrange = .Bookmarks("HeaderA").Range
myrange.Bookmarks(1).Delete
.Bookmarks.Add Name:="Header11"
End With

Set myrange = ActiveDocument.Bookmarks("Header11").Range
ActiveDocument.AttachedTemplate.AutoTextEntries(cologo).Insert
WHERE:=myrange, RichText:=True
ActiveDocument.Bookmarks.Add _
Name:="Header11", _
Range:=myrange

....more stuff - sql files to get info and move to renamed form fields
myDoc.Protect wdAllowOnlyFormFields, NoReset

Next


Doug Robbins - Word MVP said:
You cannot rename a bookmark using VBA, but you can delete it and create
a
new bookmark at the same location as follows:

Dim myrange As Range

With ActiveDocument
Set myrange = .Bookmarks("Test").Range
myrange.Bookmarks(1).Delete
.Bookmarks.Add Name:="Test1"
End With

You would have to do this every time that you insert the table (including
the first time)

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
bryan said:
In previous threads I indicated a need to position these tables.
I was able to accomplish this by inserting a bookmark inside a
predefined
1
row table on the document which is added to the template. This worked
great
if there were 1 vehicle to add. With multiple I need to rename the
formfields
which I can do but, the question is:
Is there a way to rename a bokmark which has been inserted. This would
be
ideal as then I can predefine each table cell format.

The work around I came up with was to have a formfield on the document
as
a
reference point, able to rename that and position to to add the table.
Works
but, not the preferred method as I have less control of format of each
row
cell.

Possible to rename an inserted bookmark?

Thanks,
Bryan

:

I do not see why you would need the bookmark. You already have a
handle
to
the table by the use of the dtable = ActiveDocument.Tables.Add
command.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
The tables are in a seperate document which I auto attached right
away
after
combo box indicating which vehicle so the info of coverages,
leinholder,
and
endorsements would populate for that 1 vehicle.
A seperate document for a variety of reasons.
Like I said it works great but, now they would like to select
multiple
vehicles. With that in mind I need to attach multiple doc's.
I have played with this a bit by having a form field at each spot
where
I
want the table and then cursor to that field and then build the
table
like:

ActiveDocument.Bookmarks("Cov").Range.Fields(1).Result.Select

Set dtable = ActiveDocument.Tables.Add(Range:=Selection.Range,
NumRows:=1, numcolumns:=4,
DefaultTableBehavior:=wdWord9TableBehavior,
_
AutoFitBehavior:=wdAutoFitContent)
dtable.AutoFormat ApplyBorders:=False, ApplyShading:=False,
AutoFit:=False
Set drow = dtable.Rows(1)
etc.

Cov would be renamed with each insertion then positioned to to build
the
table.

This will work but, I was hoping to rename the actual bookmark
within
the
table so that formatting of the cells I could set and space out
better
using:

Set dtable = ActiveDocument.Bookmarks("Cov").Range.Tables(1)

Is it possible to rename the bookmark or do I have to use the other
option
of having a form field to position to and then create a table?


Thanks,
Bryan


:

I am trouble by your statement "I have the main template and then
am
inserting a document which has all the
tables"

I would have everything set up in the template and if they want to
create
a
document for another unit, they should be creating a new document
from
that
template.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of
my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
Hi Doug,
Works great but, now users want to add a twist and I am having a
problem
with this.
I have the main template and then am inserting a document which
has
all
the
tables.
Initially it was set that I used a combo box at first to ge the
unit
desired
and then inserted the doc and got the coverage, lienholder, and
endorsment
info for that unit and populated the table.

Now they want to have it work where they can select multiple
units
and
do
the same.
The problem is that when the second unit adds a document and then
populates
table info, it just adds onto the first rather than on the new
added
doc.
Set dtable = ActiveDocument.Bookmarks("Coverage").Range.Tables(1)

Is there a way to rename this bookmark?
The bookmark is inside the table of the document which gets
attached.

Thanks,
Bryan

:

It would be easiest just to have each one row table with the
column
heading
names inserted into the document/template and have it identified
by
a
bookmark (rather than try and do something with autotext).
Having
the
table
itself allows equally well the sizing of the columns.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself
of
my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via
msnews.microsoft.com
See if I have this striaght to get started:
For each table I will have a bookmark for placement. Am I
creating
the
table
as I go or do I want a 1 row table in autotext with the
headings
to
insert
at
bookmark and then add rows to that?
Having the one row as autotext would allow for sizing of
columns.
If
using
autotext for the 1 row table, do I want the headings in this
row?

Again, I truly appreciate all the help on this.

Thanks,
Bryan



:

You could identify each of the one row tables by assigning a
bookmark
to
them - e.g. Coverage, Lienholders, etc

The use something like

Dim myDataBase As Database
Dim myActiveRecord As Recordset
Dim i As Long
Dim dtable As Table, drow As Row
'Open a database
Set myDataBase = OpenDatabase("[Databasepath\filename.mdb]")
'Access the first record from a particular table
Set myActiveRecord =
myDataBase.OpenRecordset("[tblNameforCoverageData]",
dbOpenForwardOnly)
'Add a table to the document with one row and as many fields
as
there
are
in
the database table
Set dtable =
ActiveDocument.Bookmarks("Coverage").Range.Tables(1)
'Loop through all the records in the table until the
end-of-file
marker
is
reached
Do While Not myActiveRecord.EOF
'Add a row to the table and populate the cells in that
row
with
the
data
from the current
record
For i = 1 To myActiveRecord.Fields.Count
Set drow = dtable.Rows.Add
drow.Cells(i).Range.Text = myActiveRecord.Fields(i -
1)
Next i
myActiveRecord.MoveNext
Loop
'Then close the database
myActiveRecord.Close

'Access the first record from the next table
Set myActiveRecord =
myDataBase.OpenRecordset("[tblNameforLienHolderData]",
dbOpenForwardOnly)
'Add a table to the document with one row and as many fields
as
there
are
in
the database table
Set dtable =
ActiveDocument.Bookmarks("Lienholders").Range.Tables(1)
'Loop through all the records in the table until the
end-of-file
marker
is
reached
Do While Not myActiveRecord.EOF
'Add a row to the table and populate the cells in that
row
with
the
data
from the current
record
For i = 1 To myActiveRecord.Fields.Count
Set drow = dtable.Rows.Add
drow.Cells(i).Range.Text = myActiveRecord.Fields(i -
1)
Next i
myActiveRecord.MoveNext
Loop
'Then close the database
myActiveRecord.Close

'etc.

Set myActiveRecord = Nothing
myDataBase.Close
Set myDataBase = Nothing


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail
yourself
of
my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via
msnews.microsoft.com
Hi Doug,
 
B

bryan

That works fantastic, just what I was looking for!

I appreciate all the jelp and examples.
This forum is the best, not only for information but, the responsiveness of
those willing to share their knowledge.

Thanks again,
Bryan
 

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