Finding horizontal position of a table

K

Ken

I want to find the horizontal position of a selected table.

The code: Selection.Tables(1).Rows.HorizontalPosition

gives the correct position in points provided that the table's
position has not been set with wdTableRight, wdTableCenter or
wdTableLeft.

I can handle wdTableLeft by:

if Selection.Tables(1).Rows.HorizontalPosition = wdTableLeft then
tPosition=0

but to use this technique with the other settings I would need to
determine the width of the table - difficult if there are merged
cells.

Is there a way to get the position in points when the position has
been set with wdTableRight or wdTableCenter ?
 
J

Jean-Guy Marcil

Ken was telling us:
Ken nous racontait que :
I want to find the horizontal position of a selected table.

The code: Selection.Tables(1).Rows.HorizontalPosition

gives the correct position in points provided that the table's
position has not been set with wdTableRight, wdTableCenter or
wdTableLeft.

I can handle wdTableLeft by:

if Selection.Tables(1).Rows.HorizontalPosition = wdTableLeft then
tPosition=0

but to use this technique with the other settings I would need to
determine the width of the table - difficult if there are merged
cells.

Is there a way to get the position in points when the position has
been set with wdTableRight or wdTableCenter ?

Try this:

Public Sub GetTableWidth()

Dim tblCurrent As Table
Dim i As Long
Dim sngTPos As Single
Dim lngCellLeft As Long
Dim lngLeftMax As Long

lngLeftMax = 1000

If Selection.Information(wdWithInTable) Then
Set tblCurrent = Selection.Tables(1)
With tblCurrent
If .Rows.HorizontalPosition = wdTableLeft Then
sngTPos = 0
Else
For i = 1 To .Rows.Count
With .Rows(i)
lngCellLeft =
..Range.Cells(1).Range.Information(wdHorizontalPositionRelativeToPage)
If lngCellLeft <= lngLeftMax Then
lngLeftMax = lngCellLeft
End If
End With
Next
sngTPos = CSng(lngLeftMax) -
Selection.Sections(1).PageSetup.LeftMargin
End If
End With
sngTPos = Format(PointsToInches(sngTPos), "###0.00")
MsgBox "The current table is " & sngTPos & " inches from the left
margin", _
vbInformation, "Table position"
Else
MsgBox "Position the cursor in a table first. ", vbExclamation, "No
table"
Exit Sub
End If

End Sub


As an exercise for you, you have to manage the cell padding (LeftPadding) in
order to get a more accurate result...
 
K

Ken

Jean-Guy Marcil

Thank you for your suggested code. It works fine for simple tables but
gives error 5991 if the table contains vertically merged cells.
Do you have any suggestions for overcoming this?

Ken
Melbourne, Australia
 
J

Jean-Guy Marcil

Ken was telling us:
Ken nous racontait que :
Jean-Guy Marcil

Thank you for your suggested code. It works fine for simple tables but
gives error 5991 if the table contains vertically merged cells.
Do you have any suggestions for overcoming this?

I guess you could go through all the cells in the table and find the one
with the lowest left position.
You can set a range to the table, and then scan all the cells in that range.
If you stay away from rows and columns, you should be OK, it just may take
longer to run...
 

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