Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / April 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
isabel.hay@uk.fujitsu.com - 31 Mar 2006 16:22 GMT
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?
Doug Robbins - Word MVP - 31 Mar 2006 19:08 GMT
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.

Signature

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

> Hi,
> I'm creating a Word document using vba in Excel, and populating it with
[quoted text clipped - 20 lines]
> to help either.
> How do I move on to the next cell along, in the next column, please?
isabel.hay@uk.fujitsu.com - 03 Apr 2006 13:48 GMT
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.
isabel.hay@uk.fujitsu.com - 05 Apr 2006 10:49 GMT
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
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.