Consulting

Results 1 to 5 of 5

Thread: Cell position on page

  1. #1
    VBAX Regular jfdawson's Avatar
    Joined
    Jan 2023
    Location
    Birmingham
    Posts
    7
    Location

    Cell position on page

    I need to find the cell position in points relative to the top of the page. My problem is that the table spans multiple pages and has vertically merged cells which may cross page boundaries. The pages also have a header and footer on them. For a "non-complex" table (no merging of cells), the following gives me what I want:

    Dim myTable As Table
    Dim myCell As Cell
    Dim iRow, iCol, myPosition As Integer
    Set myTable = ActiveDocument.Tables(1)
    Set myCell = myTable.Cell(iRow, iCol)
    myCell.Range.Select
    Set myRange = Selection.Range
    Selection.Collapse Direction:=wdCollapseStart
    myPosition = myRange.Information(wdVerticalPositionRelativeToPage)
    But if my table has vertically merged cells, then the information inquiry gives erroneous results. Example - the table below is split across 2 pages where rows 1 through 5 are on the first page and rows 6 and 7 are on the second page. I need to find the vertical position in points relative to the top of the page of a cell in row 6. Even if I select an unmerged cell, say row 6, col 3 I still get bogus results. Any way around this?

    Horizontally merged header: Row 1, Column 1
    R2, C1

    Row 2, Col 2
    Row 2, Col 3
    R2, C4
    R2, C5
    R2, C6
    Row 2, Col 7 - Horizontally merged
    Row 3,
    Col 7
    R3, C8
    R3, C9
    R3,C10
    R3, C11
    R3, C12
    R4,
    C1
    V
    E
    R
    T
    R4, C2 Row 4, Col 3 R4C4
    Row 5, Col 2 Row 5, Col 3 R5C4 Last Unsplit LAST Split
    R5, C12
    Row 6, Col 3 R6C4
    Notes: Horizontally merged cells: Row 7, Col 1

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,136
    Location
    Please use Code tags when submitting code to the forum as this improves the readability for others. Any chance of submitting a sample worksheet for us to review? I know you went to a lot of trouble to display your example in the first post but it is rather meaningless. To attach a workbook, click on Go advanced, Manage Attachments and upload your file.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  3. #3
    VBAX Regular jfdawson's Avatar
    Joined
    Jan 2023
    Location
    Birmingham
    Posts
    7
    Location

    Example Table for Cell position w/vertically merged cells

    Thanks for the quick response. Being a newbie, I'm still learning this forum. I haven't discovered how to paste a table with the cell lines showing in this window, so used your direction to attach my Word example document. Since the first post of this thread, I have tried moving the cursor from the cell to the end of the row and outside of the table since I'm mainly interested in the vertical position of the row. I get more sensical results but not all that accurate:

    Example for finding vertical page position of cell (row 6, col 3):

             Dim myTable As Table
             Dim myCell As Cell
             Dim myPosition As Integer
    
            Set myTable = ActiveDocument.Tables(1)
            Set myCell = myTable.Cell(6, 3)
            myCell.Range.Select
            Selection.MoveEnd unit:=wdRow, Count:=1
            Selection.Collapse Direction:=wdCollapseEnd     'Place cursor outside Table (in margin area)
            Set myRange = Selection.Range 'pick up current cursor position (merged cells will mess up position inquiry otherwise)
            myPosition = myRange.Information(wdVerticalPositionRelativeToPage)
    The Header is 1 inch (72 points), so I am expecting myPosition (of the 6th row) to be a little bit larger than 72 but it comes back as 95.5
    How can I get this closer to my expectations?
    Thanks for any recommendations!
    Attached Files Attached Files

  4. #4
    VBAX Regular jfdawson's Avatar
    Joined
    Jan 2023
    Location
    Birmingham
    Posts
    7
    Location
    So, playing around with it some more, it looks like I can control the results by utilizing the cell height rule before selecting the range (of the text inside the cell). For my example table in row 6, column 3, myPosition comes out at 72 points (right on the Header border), the cell height is 23.7; row 7 inquiry returns vertical position of 97.5 (which is approximately previous row vertical page position [72] + cell height [23.7] = 95.7 (off by 2 points). Cell (row) height of row 7 = 44.4 points and bottom vertical of table comes out to 72 + 23.7 + 44.4 = 139.1, much closer to my expectation of table bottom vertical of 142.5 points (when I added a row 8 and queried its wdVerticalPositionRelativeToPage:

    Option Explicit
    
    
    Dim myTable As Table
    Dim myRange As Range
    Dim myCell As Cell
    Dim myPosition, myHeight As Integer
    
    
    Sub CellPosition()
           
            Set myTable = ActiveDocument.Tables(1)
            Set myCell = myTable.Cell(6, 3)
            myCell.HeightRule = wdRowHeightExactly
            myHeight = myCell.Height
            myCell.Range.Select
            Set myRange = Selection.Range 'pick up current cursor position before Fragment start(merged cells will mess up position inquiry otherwise)
            Selection.Collapse Direction:=wdCollapseEnd
            myPosition = myRange.Information(wdVerticalPositionRelativeToPage)
              
            Set myCell = myTable.Cell(7, 1)
            myCell.HeightRule = wdRowHeightExactly
            myHeight = myCell.Height
            myCell.Range.Select
            Set myRange = Selection.Range 
            Selection.Collapse Direction:=wdCollapseEnd
            myPosition = myRange.Information(wdVerticalPositionRelativeToPage)
    Elsewhere in my code I was using myCell.HeightRule = wdRowHeightAtLeast and had that snippet of code after the cell range selection which would have been looking at the text inside the cell (I'm guessing)? Anyways, I'm much more confident in these results for the cell vertical position relative to the page. Am I going down a rabbit trail or am I on (or close) to the right path?

    Comments appreciated!
    Last edited by jfdawson; 06-20-2023 at 11:12 AM. Reason: clarification

  5. #5
    VBAX Regular jfdawson's Avatar
    Joined
    Jan 2023
    Location
    Birmingham
    Posts
    7
    Location

    More on cell vertical position relative to page

    Using the attached file example, I need to determine any given cell vertical position relative to the page. In the case of the cell at row 6, column 1 (not vertically merged - but others in the row are, column 1, 2 and 12), I would expect the following code to produce a position slightly higher than the 1 inch header (72 points), when in fact , it produces a value of 97.5:

    Option Explicit
    
    
    Public myTable As Table
    Public myRange As Range
    Public myCell As Cell
    Public myPosition6 As Variant
    Public myHeight6 As Variant
    
    
    Sub CellPosition()
            Set myTable = ActiveDocument.Tables(1)
            Set myCell = myTable.Cell(6, 3)
            myCell.HeightRule = wdRowHeightExactly
            myHeight6 = myCell.Height
            myCell.Select
            Selection.MoveEnd unit:=wdRow, Count:=1     'Put cursor outside of Table
            Selection.Collapse Direction:=wdCollapseEnd
            Set myRange = Selection.Range
            myPosition6 = myRange.Information(wdVerticalPositionRelativeToPage)
    End Sub
    The 97.5 seems to be at the bottom of the row and not the cell top edge (97.5 - 23.7 cell height = 73.8 slightly greater than the 1 inch header). Instead of putting the cursor outside the table row, keeping the range inside the cell produces a value of 79:

    Option Explicit
    
    
    Public myTable As Table
    Public myRange As Range
    Public myCell As Cell
    Public myPosition6 As Variant
    Public myHeight6 As Variant
    
    
    Sub CellPosition()
            Set myTable = ActiveDocument.Tables(1)
            Set myCell = myTable.Cell(6, 3)
            myCell.HeightRule = wdRowHeightExactly
            myHeight6 = myCell.Height
            myCell.Range.Select
            Selection.Collapse Direction:=wdCollapseEnd
            Set myRange = Selection.Range
            myPosition6 = myRange.Information(wdVerticalPositionRelativeToPage)
    End Sub
    The 79 value is closer but inspection of the table by double clicking the header shows row 6 right at the 72 point header demarcation line. Anyone see an error in the approach here?

    Thanks!
    Attached Files Attached Files

Tags for this Thread

Posting Permissions

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