ExtendMode and Selection Object

S

Stephen English

Hi
I am using Cindy Meister's code from MSDN (below) for selecting a range of
cells in a table.
I am doing this from an Access 2003 module with all objects dimensioned as
Word objects (Word.table, Word.selection etc).
sel = SelectCells(tblClub, 1, 2, 10, 2)
When it returns from the function, it fails because it it says sel is set to
nothing.
However, when I step through the function it seems to be setting it and sel
shows as "black dot".
Any ideas please?
Regards
Stephen English
Set tblClub = docClub.Tables(1)
With tblClub
.Columns(1).PreferredWidthType = wdPreferredWidthPoints
.Columns(1).PreferredWidth = CentimetersToPoints(9.03)
.Columns(2).PreferredWidthType = wdPreferredWidthPoints
.Columns(2).PreferredWidth = CentimetersToPoints(3.5)
sel = SelectCells(tblClub, 1, 2, 10, 2)
If Not sel Is Nothing Then
sel.Cells.Merge
End If
End With

Function SelectCells(ByRef tbl As Word.Table, _
rowStart As Integer, ByVal colStart As Integer, _
ByVal rowEnd As Integer, ByVal colEnd As Integer) _
As Word.Selection

Dim sel As Word.Selection
Dim i As Long
Dim nrRows As Long
Dim nrCols As Long

Set sel = Nothing
nrRows = tbl.Rows.Count
nrCols = tbl.Columns.Count

'Make sure the start cell exists in the table
If rowStart > nrRows Then Exit Function
If colStart > nrCols Then Exit Function

'Make sure the end point exists in the table
'If it does not, set the last row/column as end points
If rowEnd >= nrRows Then rowEnd = nrRows - rowStart + 1
If colEnd >= nrCols Then colEnd = nrCols - colStart + 1

'Select the start cell
tbl.Cell(rowStart, colStart).Select
Set sel = Selection

'Make sure the selection will extend
sel.ExtendMode = True

'First select the start cell
sel.Expand Unit:=wdCell

'Now extend across the columns
'-1 because first row and col are already selected
sel.MoveRight Unit:=wdCharacter, Count:=colEnd - 1, Extend:=True

'And now extend down the rows
sel.MoveDown Unit:=wdLine, Count:=rowEnd - 1, Extend:=True
Set SelectCells = sel
End Function
 
D

Doug Robbins - Word MVP

Use the Range object to define the range that you want to select (though it
may not be necessary to actually select it and better not to if you don't
have to.

Start by defining the range as the first cell

Dim myrange as Range
Set myrange = tblClub.Cell(1, 2).Range
myrange.End = tblClub.Cell(10, 2).Range.End

then to select the range, use

myrange.Select

but, to merge the cells, just use

myrange.Cells.Merge

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

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