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 2007

Tip: Looking for answers? Try searching our database.

ExtendMode and Selection Object

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Stephen English - 31 Mar 2007 23:40 GMT
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
Doug Robbins - Word MVP - 01 Apr 2007 06:55 GMT
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

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 am using Cindy Meister's code from MSDN (below) for selecting a range of
[quoted text clipped - 64 lines]
>    Set SelectCells = sel
> End Function
 
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.