Merged Cell Navigation .. Help!!!

V

VJ

Hello everybody,

I am using VBA (embedded in IBM Domino LotusScript) to generate a Word
2K document. The template has several tables that are populated with
backend data. Each table in the template(2X5), has two rows, the
column header and an empty data row which I call the template row .
The first column of the templaterow has a bookmark.

My pseudocode looks like this:

Goto bookmark
Selection.InsertRowsbelow(1)
'move up 1 row so now focus is on the row to be written to
Selection.Moveup(1)

For every row in the resultset,
if rowType = A then
mergecells (2,3 and 4) and write the value
Else IF rowType = B then
write to cells 1,2,3,4 and 5
End IF
' move to the 5 columnar template row below
Selection.moveDown(1) ' INCONSISTENT BEHAVIOUR
'insert from here so that word inserts a 5 columnar row instead of
copying the merged row
Selection.InsertRowsBelow(1)
Selection.HomeKey
Selection.MoveUp(1)
' ready to write another row of data
Next row



The problem I find is that, the Selection.moveDown does not seem to
work the same way in a merged cell as it would from a non-merged cell.

I have tried moveUp, moveleft and moveright. they all seem to have the
same problem. It works great if I am writing data to all the cells i.e
without merging. Once I merge I can't seem to get to the row below by
doing a Selection.MoveDown(1). I am still in the same row...


Is this a bug or am I missing something?.. Would someone please share
any suggestions, code or ideas on how to work around this?..

Many Thanks,
VJ
 
H

Helmut Weber

Hi VJ *?,
I don't quite understand all details of your question.
Anyway, if using moveup, movedown etc. seems to be
erratic, problably the result of a macro recording,
avoid the problem rather then solving the riddles.
Try to address cells and rows by their index
values. A bookmark, that is always in the first column
of the second row is also unnecessary.
I would suggest ....Cell(2,1).select.
For merging cells I would define a range, like that:
Dim arng As Range
Set arng = Selection.Range
with activedocument.tables(1)
arng.Start = .Cell(2, 2).Range.Start
arng.End = .Cell(2, 4).Range.End
arng.Select
Selection.Cells.Merge
Usually range is faster then selection.
Though, in tables, selection seems often to be the
better choice. Therefore the mixed selection/range
approach.

Greetings from Bavaria, Germany
Helmut Weber
"red.sys" & chr$(64) & "t-online.de"
Word 97, W98
 
V

VJ

Helmut,
Thank you for your response. Perhaps I should better explain what I am
attempting to do. I am adding a variable number of rows to a table.
Depending on the data, some of the rows will need to have horizontally
merged cells and some won't.


Conside a 5 columnar table. The first row has all 5 cells populated.
the second row has cells 2,3,4 and 5 merged. The next row has 5 cells
like the first row. The following row will have cells 2,3, 4 and 5
merged and so on.. So this table would have alternating rows with
columns merged and not-merged and so forth..

Once I merge the cells of a row and I append a row to the table by
using the Selection.insertRowsBelow call, the newly inserted row takes
on the attributes of the row the focus is on when the insertion is
made.

If I have 5 columnar rows then I could easily merge them, however if I
have merged columns it is difficult to unmerge them while trying to
retain the original width of the merged columns. So I decided that I
would have always have a five columnar row as the last row of the
table. So I can set focus to that row before inserting rows below. In
other words,


I would start with 2 rows with unmerged columns.

1. Merge the cells of row1 and write to it.
2. Selection.MoveDown(1) to navigate to the nextrow (now in row2)
3. Selection.insertRowsBelow(1) to insert an unmerged row (now in
row3)
4. Selection.moveup (now in row2) and continue to write

repeat steps 2-4 above or 1-4 above and loop.


This logic seems to work fine so long as I don't do a merge. Once
cells are merged as in Step1, Step2 does not really work. The cursor
still remains in row 1.

So this is my problem. Selection.MoveDown works fine if the cell it is
being called from is not merged.

I hope I have better explained the problem. Would you elaborate on
your earlier example and advise if there is a better way of writing to
these tables and still achieving the desired effect.


I am really at the end of my rope here and would much appreciate some
assistance from some of the gurus on this newsgroup.. Please help!!!!


Thanks,
VJ


P.S: Oh and one more thing, I am using the bookmark only so that I
know which table I am in. There are several tables in this document,
and a bookmark seemed to be the easiest way for me to get to that part
of the document and start writing.
 
M

Mark Tangard

Hi VJ:

Often when you're adding bulk to tables and the cell structure is
non-uniform, the easiest thing to do is create AutoTexts for the
various rows, already merge/unmerged and sized as you want them,
then insert the AutoTexts rather than explicitly adding table rows
and manipulating the cells via code. For example, if the AutoTexts
are in Normal.dot:

NormalTemplate.AutoTextEntries("FunnyRow").Insert _
Where:=Selection.Range, RichText:=True

Of if they're in another template:

ThisDocument.AttachedTemplate.AutoTextEntries("FunnyRow").Insert _
Where:=Selection.Range, RichText:=True

Assuming you're adding cells to the end of the table, you'd only
need to be there (at the paragraph mark following the table.
And you could select the cell you need to type into simply by
addressing it:
.Cell(4, 1)

Apparently Helmut didn't quote your original post, so I don't see
your mention of bookmarks, but if all you need is a way to refer
to the table you're in, just use Selection.Tables(1), so the
expression just above would be Selection.Tables(1).Cell(4, 1)
 
W

Word Heretic

G'day (e-mail address removed) (VJ),

I'd be doing the add rows first, to maintain conformity, and then
merhing afterwards OR writing to a whole new table set up correctly
(or even just tab delimited text - much better, then when you are done
you converttexttotable.) that you then append onto the first table by
deleting intervening space.


(e-mail address removed) (VJ) was spinning this yarn:
Helmut,
Thank you for your response. Perhaps I should better explain what I am
attempting to do. I am adding a variable number of rows to a table.
Depending on the data, some of the rows will need to have horizontally
merged cells and some won't.


Conside a 5 columnar table. The first row has all 5 cells populated.
the second row has cells 2,3,4 and 5 merged. The next row has 5 cells
like the first row. The following row will have cells 2,3, 4 and 5
merged and so on.. So this table would have alternating rows with
columns merged and not-merged and so forth..

Once I merge the cells of a row and I append a row to the table by
using the Selection.insertRowsBelow call, the newly inserted row takes
on the attributes of the row the focus is on when the insertion is
made.

If I have 5 columnar rows then I could easily merge them, however if I
have merged columns it is difficult to unmerge them while trying to
retain the original width of the merged columns. So I decided that I
would have always have a five columnar row as the last row of the
table. So I can set focus to that row before inserting rows below. In
other words,


I would start with 2 rows with unmerged columns.

1. Merge the cells of row1 and write to it.
2. Selection.MoveDown(1) to navigate to the nextrow (now in row2)
3. Selection.insertRowsBelow(1) to insert an unmerged row (now in
row3)
4. Selection.moveup (now in row2) and continue to write

repeat steps 2-4 above or 1-4 above and loop.


This logic seems to work fine so long as I don't do a merge. Once
cells are merged as in Step1, Step2 does not really work. The cursor
still remains in row 1.

So this is my problem. Selection.MoveDown works fine if the cell it is
being called from is not merged.

I hope I have better explained the problem. Would you elaborate on
your earlier example and advise if there is a better way of writing to
these tables and still achieving the desired effect.


I am really at the end of my rope here and would much appreciate some
assistance from some of the gurus on this newsgroup.. Please help!!!!


Thanks,
VJ


P.S: Oh and one more thing, I am using the bookmark only so that I
know which table I am in. There are several tables in this document,
and a bookmark seemed to be the easiest way for me to get to that part
of the document and start writing.

Steve Hudson

Word Heretic, Sydney, Australia
Tricky stuff with Word or words for you.
Email (e-mail address removed)
Products http://www.geocities.com/word_heretic/products.html

Replies offlist may require payment.
 
H

Helmut Weber

Hi VJ *,
how about that, if it has to be vba.
For a simple approach i'd recommend Mark's solution.
' given a table of 2 rows and 5 columns
' collapsed cursor is somewhere in table
Dim i As Integer
Dim j As Integer
Dim l As Integer ' loop
l = 8 ' results in 8 rows
Dim oRng As Range
Set oRng = Selection.Range
With Selection.Tables(1)
For i = 1 To l Step 2
.Cell(.Rows.Count, 1).Select ' last row, first cell
Selection.HomeKey
oRng.Start = .Cell(i, 2).Range.Start
oRng.End = .Cell(i, 5).Range.Start
oRng.Cells.Merge
.Cell(i, 1).Range.Text = "1x"
.Cell(i, 2).Range.Text = "1x"
For j = 1 To 5
.Cell(i + 1, j).Range.Text = "2x"
Next
If i < l - 1 Then Selection.InsertRowsBelow 2
Next
End With
You see, if it work's, that the cursor is moved
only once per loop. More moves aren't necessary.
Greetings from Bavaria, Germany
Helmut Weber
"red.sys" & chr$(64) & "t-online.de"
Word XP, NT 4.0
 

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