setting up table cell alignments based on data type

P

Piet Linden

I am creating nested Word tables on the fly from ADO recordsets passed
from Access. That part works fine (Thanks for the tip, Doug!)

I am trying to automatically format the contents of the nested table
based on the field type of the data that I'm stuffing into them.
(basically, based on whether they're Text or Numeric). Basically, so
I can right-align the numeric data and left align all the text data...

Can I do this in Word, or would I need to grab field types from my
recordset that it's generating the data? If I can do it in Word,
would anybody have a code snippet?

The basic idea is to loop over the rows of a table and format them all
at once. Something (sort of )
like this:

' loop through the columns of the table
for lngColumn=1 to docNestedTable.Columns.count

if IsNumeric(rst.Fields(intCounter)) Then

' loop through the rows of the table, skipping the first one (because
it's a header row)

for lngRow = 2 to docNestedTable.Rows.Count

docNestedTable.Cells(lngRow,
lngColumn).ParagraphAlignment=wdParagraphAlignLeft

next lngRow

next lngColumn

thanks
Pieter
 
D

Doug Robbins - Word MVP

I would do it as the tables are created as I think going back and trying to
identify/modify the nested tables will be a bit hairy

Something like

Private Sub WriteWord(ByVal strDoc As String, ByVal lngRow As Long,
ByVal lngColumn As Long, ByVal varWriteSomething)
Dim appWord As Word.Application
Dim docWord As Word.Document
Dim docRange as Word.Range
Set appWord = New Word.Application
Set docWord = appWord.Documents.Open(strDoc)
Dim drange as Range, acell as Cell, apara as Paragraph

With docWord.Tables(1).Cell(lngRow, lngColumn)
.Range.Text = varWriteSomething
Set docRange = .Range
With docRange
.End = .End - 1
.ConverttoTable
Set drange = .Tables(1).Cell(1, 1).Range
drange.End = drange.End - 1
If IsNumeric(drange.Text) then
For each acell in drange.Tables(1).Range.Cells
For each apara in acell.Range.Paragraphs
apara.Alignment = wdAlignParagraphRight
Next
Next
End If
End With
End With

docWord.Close True
Set docWord = Nothing
appWord.Quit
Set appWord = Nothing
End Sub


--
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
 
P

Paul Shapiro

Instead of using IsNumeric(drange.Text) to decide the paragraph alignment,
you might do better to use the recordset's field type. That's guaranteed to
be the same for every row, whereas the actual text could be alpha in one row
and digits in another.
 
P

Piet Linden

Paul,
yes, I realized that after messing with the thing for a while... it
breaks down to type=202 or type=3 (if I remember right), which I can
easily use to change the aligments...

This works, unless I have a nested table.

Public Sub AlignCellContents()
'Dim aPara As Word.Paragraph
Dim lRow As Long, lCol As Long
Dim aTable As Word.Table

Set aTable = ActiveDocument.Tables(1)

For lRow = 1 To aTable.Rows.Count
For lCol = 1 To aTable.Columns.Count
If IsNumeric(Left$(aTable.Cell(lRow, lCol).Range.Text, Len
(aTable.Cell(lRow, lCol).Range.Text) - 2)) Then
aTable.Cell(lRow, lCol).Range.Paragraphs.Alignment =
wdAlignParagraphRight
Else
aTable.Cell(lRow, lCol).Range.Paragraphs.Alignment =
wdAlignParagraphLeft
End If
Next lCol
Next lRow
End Sub

I'm just trying to isolate the source of my problem. The alignment
question is pretty much sorted out. In this case, I'd just use the
rs.fields(n).Type and convert it to an alignment using an If
statement... that's cake.

The problem now is recognizing, in code, if a cell contains a table.
Would I check a cell's Range property for a Table object? (In case you
didn't notice, I'm TOTALLY guessing!)

Is there a good tutorial somewhere on the Word object model, so I can
figure out what I'm doing without fumbling around so long in the dark?

Thanks!

Pieter
 
D

Doug Robbins - Word MVP on news.microsoft.com

It sounds like you are trying to do the alignment after having created the
nested tables, which I suggested would get pretty hairy and I think that you
should be setting the alignment at the time that each piece of data is
entered into the table, based on the type of data that it is - one cell at a
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
 
P

Piet Linden

Okay, how about this to simplify things... Since I can't (at least
anyway I know of!) manipulate the cells in a nested table
individually, how about if I create a "temp" document with which I do
the following:

1. delete the entire contents (so I know it's empty)
2. run the CreateTableFromRecordset function so I turn my recordset
into a table. (basically just point it at a new file)
3. loop through the cells of my table (in the new file), formatting as
I go. The alignment rules are pretty stupid:
If a cell's contents are numeric, align right
else align left.
4. select the entire table
5. Cut it
6. Go to the proper cell in the "final" document and paste the table
into the chosen cell.

I'll see how that works...

Thanks for the feedback. Was wondering why I couldn't find the nested
table in the hierarchy... maybe it's not just me..
 
D

Doug Robbins - Word MVP on news.microsoft.com

Going back to the code that I provided for you in your earlier post, if it
is modified as follows, the alignment can be dealt with immediately the
inserted data is converted into a table

Private Sub WriteWord(ByVal strDoc As String, ByVal lngRow As Long, _
ByVal lngColumn As Long, ByVal varWriteSomething)
Dim appWord As Word.Application
Dim docWord As Word.Document
Dim docRange As Word.Range
Dim dRange as Word.Range
Set appWord = New Word.Application
Set docWord = appWord.Documents.Open(strDoc)

With docWord.Tables(1).Cell(lngRow, lngColumn)
.Range.Text = varWriteSomething
Set docRange = .Range
With docRange
.End = .End - 1
.ConvertToTable
'New code to align contents of cells in nested table
With .Tables(1) 'This is the newly created nested table. Do
what you want with it while you can.
For i = 1 To .Rows.Count
For j = 1 To .Columns.Count
Set drange = .Cell(i, j).Range
drange.End = drange.End - 1
If IsNumeric(drange.Text) Then
drange.Paragraphs(1).Alignment =
wdAlignParagraphRight
Else
drange.Paragraphs(1).Alignment =
wdAlignParagraphCenter
End If
Next j
Next i
End With
'End of new code
End With
End With

docWord.Close True
Set docWord = Nothing
appWord.Quit
Set appWord = 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
 

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