Link to home
Start Free TrialLog in
Avatar of Fi69
Fi69

asked on

Help declaring a table cell's row and column position in Word

Hi experts

I want to goto a bookmark in my document called "Total" Fee which is in a table. Then I want to return the position of the cell, ie it's row and column numbers. There are several tables in my document.

I'm been looking around trying to work out how to do it. I've managed to return the table number. I thought I was setting the row and column position in my code, but I keep getting an error when I try to go to that cell.

Once I've declared the row position I want to do a calculation. I think I'll be able to do that when I declare the row correctly.

Can someone please help me declare the cell row and column position because I give up!



Sub SettheTotalCells()


    Dim oTbl As Table
    Dim rw As Row
    Dim cl As Cell
    Dim Col As Column
    Dim J As Integer
    Dim iTableNum As Integer
   
 
    For J = 1 To ActiveDocument.Tables.Count
        Set oTbl = ActiveDocument.Tables(J)
        oTbl.Select
        If Selection.Bookmarks.Exists("TotalFee") Then
            iTableNum = J
            Exit For
        End If
    Next J
    
    Set oTbl = ActiveDocument.Tables(J)
    
    ActiveDocument.Bookmarks("TotalFee").Range.Select
    Set cl = Selection.Cells(1)
    cl.Select
    
    '  DECLARE ROW AND COLUMN NUMBERS
    
    Set rw = oTbl.Rows(cl.RowIndex)
    Set Col = oTbl.Columns(cl.ColumnIndex)
    

    

     ActiveDocument.Tables(J).Cell(Row:=4, Column:=4).Range.Select 'this works
     ActiveDocument.Tables(J).Cell(rw, Col).Range.Select 'this doesn't work
     oTbl.Cell(rw, Col).Range.Select 'this doesn't work


    Selection.InsertFormula Formula:="=d4+d5", NumberFormat:="#,##0.00;(#,##0.00)"

End Sub

Open in new window

SOLUTION
Avatar of JoeNuvo
JoeNuvo
Flag of Viet Nam image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
However, you can do it in one line:
Sub SettheTotalCells()
  ActiveDocument.Fields.Add ActiveDocument.Bookmarks("TotalFee").Range, wdFieldEmpty, "=SUM(D4, D5) \# ""#.##0,00;(#.##0,00)"""
End Sub

Open in new window

Avatar of Fi69
Fi69

ASKER

Hi Joe and Graham

Thanks for the clarification on the correct declaration of the variable.  That did the trick!

Graham, I tried your code to insert the formula in one line but it errored for some reason.


It worked in my test. What was the error message?
Avatar of Fi69

ASKER

Hi Graham

It is a run-time error no. 4605. The command is not available. Would that have something to do with what is bookmarked? I selected the whole cell and inserted my bookmark rather than selecting the formula/field and applying the bookmark. I've just found that is more stable (less easily deleted). The formula is sitting in that cell.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial