Determining whether I'm in a table,and moving to the line above or below the table

S

Sean Farrow

Hi:

I'm currently involved in a project in which I need to determine whether the
line I'm currently on is in a table, and if so determine where the table
starts/ends and move to the line above/below the table.
Any help with this is appreciated.
Sean.



__________ Information from ESET NOD32 Antivirus, version of virus signature database 4077 (20090514) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
M

macropod

Hi Sean,

At a fairly basic level:

Sub Demo1()
With Selection
If .Information(wdWithInTable) = True Then
.Tables(1).Range.Characters.Last.Select
Selection.MoveRight wdCharacter, 1
End If
End With
End Sub

or

Sub Demo2()
With Selection
If .Information(wdWithInTable) = True Then
.Tables(1).Range.Characters.First.Select
Selection.MoveLeft wdCharacter, 1
End If
End With
End Sub
 
S

Sean Farrow

Hi:
Thanks if wdWithInTable is rue, how would I find out the current table?
Chers
Sean.
macropod said:
Hi Sean,

At a fairly basic level:

Sub Demo1()
With Selection
If .Information(wdWithInTable) = True Then
.Tables(1).Range.Characters.Last.Select
Selection.MoveRight wdCharacter, 1
End If
End With
End Sub

or

Sub Demo2()
With Selection
If .Information(wdWithInTable) = True Then
.Tables(1).Range.Characters.First.Select
Selection.MoveLeft wdCharacter, 1
End If
End With
End Sub

--
Cheers
macropod
[Microsoft MVP - Word]


Sean Farrow said:
Hi:

I'm currently involved in a project in which I need to determine whether
the line I'm currently on is in a table, and if so determine where the
table starts/ends and move to the line above/below the table.
Any help with this is appreciated.
Sean. __________ Information from ESET NOD32 Antivirus, version of virus
signature database 4077 (20090514) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

__________ Information from ESET NOD32 Antivirus, version of virus
signature database 4080 (20090515) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com



__________ Information from ESET NOD32 Antivirus, version of virus signature database 4080 (20090515) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
J

Jean-Guy Marcil

Sean Farrow was telling us:
Sean Farrow nous racontait que :
Hi:
Thanks if wdWithInTable is rue, how would I find out the current
table?

The code macropod posted does exactly what you asked, so what else do you
want to do?

What do you mean by "find out the current table"?

If the selection is in a table, then the current table is the one the cursor
is in... no?
What else do you mean?
 
M

macropod

Hi Sean,

It seems your requirements are suffering from scope creep.

Just in case you need the cell address too:

Sub Demo()
Dim i As Integer
With Selection
If .Information(wdWithInTable) = True Then
For i = 1 To ActiveDocument.Tables.Count
If (.Range.Start >= ActiveDocument.Tables(i).Range.Start) And _
(.Range.End <= ActiveDocument.Tables(i).Range.End) Then
Exit For
End If
Next i
If .Cells(1).ColumnIndex > 26 Then
MsgBox "The selection is in Table " & i & vbCrLf & "Cell Address: " & _
Chr(64 + Int(.Cells(1).ColumnIndex / 26)) & _
Chr(64 + (.Cells(1).ColumnIndex Mod 26)) & .Cells(1).RowIndex
Else
MsgBox "The selection is in Table " & i & vbCrLf & "Cell Address: " & _
Chr(64 + .Cells(1).ColumnIndex) & .Cells(1).RowIndex
End If
End
End If
End With
MsgBox "The selection is not in a table!"
End Sub

--
Cheers
macropod
[Microsoft MVP - Word]


Sean Farrow said:
Hi:
Thanks if wdWithInTable is rue, how would I find out the current table?
Chers
Sean.
macropod said:
Hi Sean,

At a fairly basic level:

Sub Demo1()
With Selection
If .Information(wdWithInTable) = True Then
.Tables(1).Range.Characters.Last.Select
Selection.MoveRight wdCharacter, 1
End If
End With
End Sub

or

Sub Demo2()
With Selection
If .Information(wdWithInTable) = True Then
.Tables(1).Range.Characters.First.Select
Selection.MoveLeft wdCharacter, 1
End If
End With
End Sub

--
Cheers
macropod
[Microsoft MVP - Word]


Sean Farrow said:
Hi:

I'm currently involved in a project in which I need to determine whether
the line I'm currently on is in a table, and if so determine where the
table starts/ends and move to the line above/below the table.
Any help with this is appreciated.
Sean. __________ Information from ESET NOD32 Antivirus, version of virus
signature database 4077 (20090514) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

__________ Information from ESET NOD32 Antivirus, version of virus
signature database 4080 (20090515) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com



__________ Information from ESET NOD32 Antivirus, version of virus signature database 4080 (20090515) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
S

Sean Farrow

I need to find out the tablenumber 9in the tables collection) or the name of
the table.
Also how would I move to the next table. If ther a focus method or some such
on the table object?
Cheers
Sean.
Cheers
Jean-Guy Marcil said:
Sean Farrow was telling us:
Sean Farrow nous racontait que :


The code macropod posted does exactly what you asked, so what else do you
want to do?

What do you mean by "find out the current table"?

If the selection is in a table, then the current table is the one the
cursor is in... no?
What else do you mean?

--
______________________________
Jean-Guy Marcil
Montreal, Canada


__________ Information from ESET NOD32 Antivirus, version of virus
signature database 4080 (20090515) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com



__________ Information from ESET NOD32 Antivirus, version of virus signature database 4080 (20090515) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
J

Jean-Guy Marcil

Sean Farrow was telling us:
Sean Farrow nous racontait que :
I need to find out the tablenumber 9in the tables collection) or the
name of the table.
Also how would I move to the next table. If ther a focus method or
some such on the table object?
Cheers

Tables in Word do not have names (The name property can only be used in HTML
code) and they do not have an Index property either...

So you have to count the number of tables in the document up to the current
position to find out the table count...

Play around with the following code to get a better idea of what is going
on...

---------------------------------------
Sub Test()

Dim i As Long
Dim rgeDoc As Range
Dim rgeCurrent As Range

Set rgeCurrent = Selection.Range

With rgeCurrent
.Collapse wdCollapseStart
If .Information(wdWithInTable) = True Then
'Use Cells(1).Range.End otherwise if selection is first _
character in first cell of a table, the current _
table will not be included in the count...
Set rgeDoc = ActiveDocument.Range(0, .Cells(1).Range.End)
i = rgeDoc.Tables.Count 'i = number of current table
'Make sure we are not in last table of doc...
If i < ActiveDocument.Range.Tables.Count Then
'Select next table
ActiveDocument.Range.Tables(i + 1).Range.Select
Selection.Collapse wdCollapseStart
Else
'Place cursor immediately after current table
.Tables(1).Range.Characters.Last.Select
Selection.MoveRight wdCharacter, 1
End If
Else
MsgBox "Cursor not in a table!", vbExclamation, "Cancelled"
End If
End With

End Sub
 

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