Solved

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

Posted on 2010-08-12
7
658 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:Fi69
  • 4
  • 2
7 Comments
 
LVL 11

Assisted Solution

by:JoeNuvo
JoeNuvo earned 75 total points
ID: 33427457
Try change following thing

Dim rw As Integer
.
Dim Col As Integer
.
.
.
'     DECLARE ROW AND COLUMN NUMBERS

rw = cl.RowIndex
Col = cl.ColumnIndex
.
.
.
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 175 total points
ID: 33427729
Your immediate problem is that you are trying to use the row and column objects.

You need to do:

    Dim r As Integer
    Dim c As Integer
 
Then

    r = cl.RowIndex
    c = cl.ColumnIndex

And

ActiveDocument.Tables(J).Cell(r, C).Range.Select
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 33427779
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

0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:Fi69
ID: 33428933
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.


0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 33429590
It worked in my test. What was the error message?
0
 

Author Comment

by:Fi69
ID: 33439788
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.
0
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 175 total points
ID: 33441042
Yes that would be a problem. The field would replace the contents of the bookmark. If the whole cell is bookmarked, the cell delimiter is also included, which cannot be replaced.

Try this instead.




Sub SettheTotalCells()
    Dim rng As Range
    
    Set rng = ActiveDocument.Bookmarks("TotalFee").Range
    rng.Collapse wdCollapseStart
    ActiveDocument.Fields.Add rng, wdFieldEmpty, "=SUM(D4, D5) \# ""#.##0,00;(#.##0,00)""", False
End Sub

Open in new window

0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now