Selection Question

V

vqthomf

I ahve merge cell in a table and I am trying to select the frist row and add
text but I get this error 'Can Not Access Individual Rows In This Collection
Because The Table Has Vertically Merge Cells' How Can I put text into the
cells?.
TIA
Charles
 
H

Helmut Weber

Hi,

assuming that you are not a beginner in WordVBA,
I dare to post some thoughts on merged cells,
rather then providing a ready to use solution.

Even if there are merged cells,
each cell has still a rowindex and a columnindex.

To select a row, I'd first determine the number
of cells in the rows.

Like this in pseudocode:

for each cell in table.cells
get rowindex, get columnindex
next

If you get 5 times rowindex 1,
then there are 5 cells in row 1.

You can then define a range from the first
cell in row 1 to the last cell in row 1
and select that range.

But it is really tricky.

E.g. you may get (2,1) (2,2) (2,4) (2,5)
for the four cells in row 2, when it
contains one merged cell, as cell (2,3) is missing.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
T

Tony Jollans

It is awkward - but not as awkward as that.

To select a row with vertically merged cells you only need to identify a
single cell in that row. In the case of the first row, that can be the first
cell. From that cell you can manipulate the Selection ...

ActiveDocument.Tables(1).Range.Cells(1).Select
myRow = Selection.Cells(1).RowIndex
Selection.HomeKey Unit:=wdRow
While myRow <> Selection.Cells(1).RowIndex
Selection.MoveRight Unit:=wdCell, Count:=1
Wend
Selection.EndKey Unit:=wdRow, Extend:=True

In the case of the first row you don't actually need to do the rowindex
checking but, in the general case, it is needed in case the merged cells are
in one or more left hand columns.

To work with a different row change the cell number in the first line.

Unfortunately this only works with the Selection and not with other Ranges.
 
H

Helmut Weber

Hi Tony,
ActiveDocument.Tables(1).Range.Cells(1).Select
myRow = Selection.Cells(1).RowIndex
Selection.HomeKey Unit:=wdRow
While myRow <> Selection.Cells(1).RowIndex
Selection.MoveRight Unit:=wdCell, Count:=1
Wend
Selection.EndKey Unit:=wdRow, Extend:=True

Yes, very clever.
Using _your_ approach,
it looks as it could be done
even without the while [...] wend
and without the homekey:

Sub SelectRow4()
With ActiveDocument.Tables(1)
.Cell(4, 1).Select
Selection.EndKey Unit:=wdRow, Extend:=True
End With
End Sub

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
T

Tony Jollans

Hi Helmut,

It depends. If you know your table has a cell(4,1) that will work but if
cells(3,1) and (4,1) are merged there won't be a cell(4,1) - and row(4) will
start in cell(4,2). I believe what I posted will work with all combinations
of merged cells but I've just realised there is an even easier way ...

ActiveDocument.Tables(1).Range.Cells(15).Select
CommandBars.FindControl(ID:=801).Execute

or, for a cell you know exists ...

ActiveDocument.Tables(1).Cell(4,2).Select
CommandBars.FindControl(ID:=801).Execute

(Control 801 is Table > Select > Row)

--
Enjoy,
Tony


Helmut Weber said:
Hi Tony,
ActiveDocument.Tables(1).Range.Cells(1).Select
myRow = Selection.Cells(1).RowIndex
Selection.HomeKey Unit:=wdRow
While myRow <> Selection.Cells(1).RowIndex
Selection.MoveRight Unit:=wdCell, Count:=1
Wend
Selection.EndKey Unit:=wdRow, Extend:=True

Yes, very clever.
Using _your_ approach,
it looks as it could be done
even without the while [...] wend
and without the homekey:

Sub SelectRow4()
With ActiveDocument.Tables(1)
.Cell(4, 1).Select
Selection.EndKey Unit:=wdRow, Extend:=True
End With
End Sub

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
H

Helmut Weber

Hi Tony,

thanks, thrilling.
CommandBars.FindControl(ID:=801).Execute

There is a built in macro TableSelectRow,
which I can overwrite, but I cannot call it.
If I record a macro, I get:

Selection.SelectRow

Though,

CommandBars.FindControl(ID:=801).Execute

is impressing.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 

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