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?
 
D

Doug Robbins - Word MVP

The following will split the fourth cell in the first row of a table into
two and populate each of those cells with some text and then split what was
originally the fifth cell (it's the sixth after the split of the fourth)
into two and populate each of those cells with text.

Dim rngword As Range
With ActiveDocument.Tables(1)
Set rngword = .Cell(1, 4).Range
rngword.Cells(1).Split numcolumns:=2
Set rngword = .Cell(1, 4).Range
rngword.Text = "one"
Set rngword = .Cell(1, 5).Range
rngword.Text = "two"
Set rngword = .Cell(1, 6).Range
rngword.Cells(1).Split numcolumns:=2
Set rngword = .Cell(1, 6).Range
rngword.Text = "three"
Set rngword = .Cell(1, 7).Range
rngword.Text = "four"
End With

I am not really sure what your rngword represents.

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

isabel.hay

Doug, hi, thanks for that.

My rngWord starts off as:
Set rngWord = docWord.Tables(1).Cell(1,
docWord.Tables(1).Columns.Count).Range

(last cell of the first row (which is originally the only row in the
table)
I then loop round, adding rows to the table as I get data from an Excel
spreadsheet.
As I fill in each cell I 'rngWord.Move wdCell' to get to the next cell
in the row.
I guess what I need is to get the 'co-ordinates' of the cell I'm in
with rngWord (how do I do this?), to tell me which cell and row I'm in
so I can then say "Set rngWord = .Cell(x,y) to move to the next column
(I've tried using 'rngWord.Move wdColumn' and collapsing the range but
that doesn't work either.

Any suggestions?
Thanks.
 
I

isabel.hay

I've worked it out so I thought I'd post the answer myself:

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