Results 1 to 9 of 9
  1. #1
    PeterF
    Guest

    Selection is table, cell,row or column

    I need to test whether the selection is either:
    1. _In_a table.
    2. _An_entire table, or a table cell, row or column.

    I know how to do 1. - Selection.Information(wdWithInTable)

    Can anyone tell me how to test for any or all of 2.?

    Thanks in advance

    Peter Fagan

  2. #2
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Selection is table, cell,row or column

    Hi Peter,

    There's probably a much more elegant way, but the following will return the information you described in 2.
    It might not work right if there are merged cells, though.

    <pre>Sub GetTableSelectionDetails()

    Dim rngCurTable As Range
    Dim lngTblCellsCt As Long
    Dim lngTblRowsCt As Long
    Dim lngTblColsCt As Long
    Dim lngSelCellsCt As Long
    Dim lngSelRowsCt As Long
    Dim lngSelColsCt As Long

    Set rngCurTable = Selection.Tables(1).Range

    lngSelCellsCt = Selection.Cells.Count
    lngSelRowsCt = Selection.Rows.Count
    lngSelColsCt = Selection.Columns.Count

    lngTblCellsCt = rngCurTable.Cells.Count
    lngTblRowsCt = rngCurTable.Rows.Count
    lngTblColsCt = rngCurTable.Columns.Count

    If lngSelCellsCt = 1 Then
    MsgBox "Only one cell is selected"
    End If

    If lngSelCellsCt = lngTblCellsCt Then
    MsgBox "Entire table is selected"
    End If

    If lngSelColsCt = lngTblColsCt Then
    MsgBox "Entire row or rows are selected"
    End If

    If lngSelRowsCt = lngTblRowsCt Then
    MsgBox "Entire column or columns are selected"
    End If

    End Sub
    </pre>


  3. #3
    PeterF
    Guest

    Re: Selection is table, cell,row or column

    Thanks Gary

    This looks like a good option. I will try to get this running as soon as I get a chance, and let you know what happens.

    My other thought on this was that as the macro I need this for _follows_immediately a selection of text by the user, it might be possible to test on the last user action.

    Let me explain a little further. The purpose of the macro is to automate the process whereby a user who has selected a block of text places it in a special type of table. The macro cuts the selection, creates the table and pastes the selection into it.

    There is no problem when the selected text is outside a table. And I know how to test whether the selection is inside a table.

    The problem occurs when the user selects a table or table cell, row or column containing text _rather_than_just_text. As you can imagine, they attempt to paste a table into a table and the table the macro has created for them gets screwed up.

    We know that Word records previous actions - for Undo - so could I identify the last two actions? - and respond if they were any of the following pairs:

    Selection.SelectCell
    Selection.MoveRight Unit:=wdCharacter, Count:=1

    Selection.SelectColumn
    Selection.MoveLeft Unit:=wdCharacter, Count:=2

    Selection.SelectRow
    Selection.MoveLeft Unit:=wdCharacter, Count:=1

    Selection.Tables(1).Select
    Selection.MoveLeft Unit:=wdCharacter, Count:=1

    Any thoughts you or others have on this would be appreciated.

    Regards

    Peter Fagan

  4. #4
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Selection is table, cell,row or column

    I have had as-yet unresolved problems identifying ranges of cells example .

    I've found a case where I can't really identify a block of say, 3x3 cells in the interior of a 7x8 table.

  5. #5
    Star Lounger
    Join Date
    Mar 2001
    Location
    Cheltenham, Pennsylvania, USA
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Selection is table, cell,row or column

    The following is from the VB Help: then see suggestion following the quote.
    -----
    Use the Information property to return information about the selection. If the selection is in a table, the following example displays the number or rows and columns in the table.

    If Selection.Information(wdWithInTable) = True Then
    MsgBox "Columns = " _
    & Selection.Information(wdMaximumNumberOfColumns) _
    & vbCr & "Rows = " _
    & Selection.Information(wdMaximumNumberOfRows)
    End If
    ------

    If you can determine the number of columns/rows in your selected text, you could compare that to the numbers returned by the Information method of the Selection object. If they match, your selection is the entire table; if only the number of rows matches, your selection is an entire row of a table, etc.

    (Just a thought from a non-programming dabbler.)

  6. #6
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Selection is table, cell,row or column

    But see also <A target="_blank" HREF=http://www.wopr.com/cgi-bin/w3t/showthreaded.pl?Cat=&Board=vb&Number=25905&page=0& view=&sb=&part=&vc=1>http://www.wopr.com/cgi-bin/w3t/showthread...&sb=&part=&vc=1</A>

  7. #7
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Selection is table, cell,row or column

    I couldn't resist:<pre>Public Function lngTableSelectionDetails(sel As Selection) As Long
    ' <post#=25,299>post 25,299</post#> re: 25,065 from PeterF
    '''
    ''' Values returned when ascertaining a type of selection
    '''
    Public Const lngTableSelectionDetailsOneCell As Long = 1 ' MsgBox "Only one cell is selected"
    Public Const lngTableSelectionDetailsEntireTable As Long = 2 ' MsgBox "Entire table is selected"
    Public Const lngTableSelectionDetailsEntireRow As Long = 3 ' MsgBox "Entire row or rows are selected"
    Public Const lngTableSelectionDetailsEntireColumn As Long = 4 ' MsgBox "Entire column or columns are selected"
    Public Const lngTableSelectionDetailsSeveralCells As Long = 5 ' MsgBox "A number of cells are selected"
    Public Const lngTableSelectionDetailsSeveralTables As Long = 6 ' MsgBox "A number of tables are selected"
    Public Const lngTableSelectionDetailsTableAndText As Long = 7
    Dim rngCurTable As Range
    Set rngCurTable = sel.Tables(1).Range
    '
    Dim lngTblCellsCt As Long
    Dim lngTblRowsCt As Long
    Dim lngTblColsCt As Long
    Dim lngSelCellsCt As Long
    Dim lngSelRowsCt As Long
    Dim lngSelColsCt As Long
    '
    lngSelCellsCt = sel.Cells.Count
    lngSelRowsCt = sel.Rows.Count
    lngSelColsCt = sel.Columns.Count
    '
    lngTblCellsCt = rngCurTable.Cells.Count
    lngTblRowsCt = rngCurTable.Rows.Count
    lngTblColsCt = rngCurTable.Columns.Count
    If sel.Information(wdWithInTable) Then
    If lngSelCellsCt = 1 Then
    lngTableSelectionDetails = lngTableSelectionDetailsOneCell ' MsgBox "Only one cell is selected"
    ElseIf lngSelCellsCt = lngTblCellsCt Then
    lngTableSelectionDetails = lngTableSelectionDetailsEntireTable ' MsgBox "Entire table is selected"
    ElseIf lngSelColsCt = lngTblColsCt Then
    lngTableSelectionDetails = lngTableSelectionDetailsEntireRow ' MsgBox "Entire row or rows are selected"
    ElseIf lngSelRowsCt = lngTblRowsCt Then
    lngTableSelectionDetails = lngTableSelectionDetailsEntireColumn ' MsgBox "Entire column or columns are selected"
    Else
    lngTableSelectionDetails = lngTableSelectionDetailsSeveralCells ' MsgBox "A number of cells are selected"
    End If
    Else
    lngTableSelectionDetails = 0 ' Default result is failure
    End If
    'Sub TestlngTableSelectionDetails()
    ' MsgBox lngTableSelectionDetails(Selection)
    'End Sub
    End Function
    </pre>


  8. #8
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts

    Re: Selection is table, cell,row or column

    Hi Chris,

    There's some useful stuff here too: http://gregmaxey.mvps.org/Table_Cell_Data.htm

    Cheers
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  9. #9
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Selection is table, cell,row or column

    > There's some scary stuff here too!
    I thought I had modes of secltion nailed down (I allow the user to span several tables in a document for AutoFormat, AutoFit etc and consider fractional tables at each end of the selection, whether to include only the fraction selected or the entire table, and so on), but I hadn't started to get into merged/split cells.

    Thanks. I think. (grin!)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •