Get data from the last row of a table

J

J Whales

Hi!

In our documents we have a Version History table that lists all the
revisions for the current document (how, what, when...).

On the document's cover page, I would like to display some information
from the LAST row of the version history table.

Is there a way to that? I looked at fields, but what I found was mostly
for calculation...

Thanks to anyone that can help.

J Whales
 
J

J Whales

J said:
In our documents we have a Version History table that lists all the
revisions for the current document (how, what, when...).

On the document's cover page, I would like to display some information
from the LAST row of the version history table.

Is there a way to that? I looked at fields, but what I found was
mostly for calculation...

Yeah, but I would prefer not to rely on the user having to add a
bookmark whenever they add something in the table. I already have
enough difficulties having them use the proper styles...
 
G

Greg

Ok, you could use a macro to set docVariables to the value of the
table cells in the last row and then use DocVariable fields on page 1.

Sub ScratchMacro()
Dim oTable As Table
Dim oDoc As Document
Dim txtWho As String
Dim txtWhat As String
Dim txtWhere As String
Dim txtWhen As String
Dim txtWhy As String
Dim i As Integer

Set oDoc = ActiveDocument
Set oTable = oDoc.Tables(1)
i = oTable.Rows.Count

txtWho = oTable.Cell(i, 1).Range.Text
txtWho = Left(txtWho, Len(txtWho) - 2)
txtWhat = oTable.Cell(i, 2).Range.Text
txtWhat = Left(txtWhat, Len(txtWhat) - 2)
txtWhere = oTable.Cell(i, 3).Range.Text
txtWhere = Left(txtWhere, Len(txtWhere) - 2)
txtWhen = oTable.Cell(i, 4).Range.Text
txtWhen = Left(txtWhen, Len(txtWhen) - 2)
txtWhy = oTable.Cell(i, 5).Range.Text
txtWhy = Left(txtWhy, Len(txtWhy) - 2)

oDoc.Variables("Who").Value = txtWho
oDoc.Variables("What").Value = txtWhat
oDoc.Variables("Where").Value = txtWhere
oDoc.Variables("When").Value = txtWhen
oDoc.Variables("Why").Value = txtWhy

ActiveDocument.Fields.Update

End Sub
 
J

J Whales

Greg said:
Ok, you could use a macro to set docVariables to the value of the
table cells in the last row and then use DocVariable fields on page 1.

Sub ScratchMacro()
Dim oTable As Table
Dim oDoc As Document
Dim txtWho As String
Dim txtWhat As String
Dim txtWhere As String
Dim txtWhen As String
Dim txtWhy As String
Dim i As Integer

Set oDoc = ActiveDocument
Set oTable = oDoc.Tables(1)
i = oTable.Rows.Count

txtWho = oTable.Cell(i, 1).Range.Text
txtWho = Left(txtWho, Len(txtWho) - 2)
txtWhat = oTable.Cell(i, 2).Range.Text
txtWhat = Left(txtWhat, Len(txtWhat) - 2)
txtWhere = oTable.Cell(i, 3).Range.Text
txtWhere = Left(txtWhere, Len(txtWhere) - 2)
txtWhen = oTable.Cell(i, 4).Range.Text
txtWhen = Left(txtWhen, Len(txtWhen) - 2)
txtWhy = oTable.Cell(i, 5).Range.Text
txtWhy = Left(txtWhy, Len(txtWhy) - 2)

oDoc.Variables("Who").Value = txtWho
oDoc.Variables("What").Value = txtWhat
oDoc.Variables("Where").Value = txtWhere
oDoc.Variables("When").Value = txtWhen
oDoc.Variables("Why").Value = txtWhy

ActiveDocument.Fields.Update

End Sub

Thanks Greg!

I had something like that in mind; I was hoping there would be
something more 'automatic'.

Again, thanks for your help
 
Top