Splitting cells in a Word table and then moving to the next cell

I

isabel.hay

Hi,
I'm creating a Word document using vba in Excel, and populating it with
data from the spreadsheets.

I have table with 5 columns.
In each row, I may need to split the cell in columns 4 & 5 several
times.

I split the cell in column 4 and populate the new cells as follows (the
'original' cell already has some text in it):
For Each varRole In arrRoles
intRoles = intRoles + 1
rngWord.Cells.Split 2, 1, False
rngWord.Move wdCell, 2 '2seems to be needed
to move 1 cell due to the post-split selection by excel
rngWord.Text = CStr(varRole)
Next
My problem is that I then want to move to the next cell in the row, in
column 5, then split and populate that. I've tried various ways of
moving ('rngWord.Move wdColumn, 1' and 'rngWord.Move wdCell, 1' (or 2),
but they don't seem to work from a split cell (so the splits for the
next column end up in this one) ('move wdcell, 1' works for a non-split
cell).

Prefixing the Move with 'rngWord.Collapse wdCollapseEnd' doesn't seem
to help either.

How do I move on to the next cell along, in the next column, please?
 
C

Cindy M -WordMVP-

I split the cell in column 4 and populate the new cells as follows (the
'original' cell already has some text in it):
For Each varRole In arrRoles
intRoles = intRoles + 1
rngWord.Cells.Split 2, 1, False
rngWord.Move wdCell, 2 '2seems to be needed
to move 1 cell due to the post-split selection by excel
rngWord.Text = CStr(varRole)
Next
My problem is that I then want to move to the next cell in the row, in
column 5, then split and populate that. I've tried various ways of
moving ('rngWord.Move wdColumn, 1' and 'rngWord.Move wdCell, 1' (or 2),
but they don't seem to work from a split cell (so the splits for the
next column end up in this one) ('move wdcell, 1' works for a non-split
cell).

Prefixing the Move with 'rngWord.Collapse wdCollapseEnd' doesn't seem
to help either.

How do I move on to the next cell along, in the next column, please?
What kind of object is rngWord? You don't show us how you declare and
assign it...

You seem to be using the Selection object, which is about as inefficient
as you can get, but see if this helps at all:
Dim rw as Word.Row
Set rw = Selection.Rows(1)
rw.Cells(rw.Cells.Count).Select

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
I

isabel.hay

I finally worked it out so here's the answer:

You can find out what cell you're in using the "Information" property,
so
I save the row and column info for a cell before I start splitting it:

nRow = rngWord.Information(wdEndOfRangeRowNumber)
nCol = rngWord.Information(wdEndOfRangeColumnNumber)
rngWord.Collapse wdCollapseEnd

then I can split the cell into more rows and move around in it.
To move to the next column I use:

rngWord.Collapse wdCollapseEnd
Set rngWord = docWord.Tables(1).Cell(nRow, nCol +
1).Range
rngWord.Collapse wdCollapseStart

(wdcollapseEnd takes you too far, and you have to use Set to change the
range this way)
And then back at the start of the loop, to move to the next new row:

docWord.Tables(1).Rows.Add
Set rngWord =
docWord.Tables(1).Cell(docWord.Tables(1).Rows.Count, 1).Range
rngWord.Collapse wdCollapseStart
 

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