0
votes

So I'm trying to do the unthinkable and actually do some basic calculations in a word table, without having to resort to Excel. It's for the invoice generator I've been building for a while, and this is pretty much the last thing I have left.

The questions is simple.. how do you multiply a number from one cell with a number from a second cell, in order to get a result in the third cell? I just can't figure it out, yet it sounds very simple. Ideally it would execute after refocusing from one cell to another.

Any ideas?

EDIT:

Well, that really was easy. All I needed was the Val function. Here's the code:

Dim qty1 As Integer
Dim qty2 As Integer
Dim qty3 As Integer

Dim price1 As Integer
Dim price2 As Integer
Dim price3 As Integer

qty1 = Val(ActiveDocument.Tables(5).Cell(2, 2).Range.Text)
qty2 = Val(ActiveDocument.Tables(5).Cell(3, 2).Range.Text)
qty3 = Val(ActiveDocument.Tables(5).Cell(4, 2).Range.Text)

price1 = Val(ActiveDocument.Tables(5).Cell(2, 3).Range.Text)
price2 = Val(ActiveDocument.Tables(5).Cell(3, 3).Range.Text)
price3 = Val(ActiveDocument.Tables(5).Cell(4, 3).Range.Text)

ActiveDocument.Tables(5).Cell(2, 4).Range.Text = qty1 * price1
ActiveDocument.Tables(5).Cell(3, 4).Range.Text = qty2 * price2
ActiveDocument.Tables(5).Cell(4, 4).Range.Text = qty3 * price3
2
You can put your edit as an answer to you own question and accept it so future searchers can find it. You can remove your additional question about updating automatically and ask a new one. It is not really that strict but "This site is all about getting answers. It's not a discussion forum. There's no chit-chat." stackoverflow.com/tourniton
My bad. Gonna do that now.blackwind

2 Answers

0
votes

Word is also a spreadsheet. If you type Perform Calculation in a Table in Help you'll see how to do it.

Word's spreadsheet is better than an Excel table inserted as formatting works.

Basically Equation fields can refer to tables and table cells.

0
votes
  • select values from ranges in text or tables using appropriate selectors
  • use Val() function to convert numbers in text range to numeric value of appropriate type
  • to multiply numbers use a Multiplication Operator *
  • output result in another a cell or other word range
  • format as needed
  • execute your macro on some button click

    Sub multiply_some_cells()
    
    Dim t1, t2 As table
    Dim a, b, m As Double
    
    ' first table
    Set t1 = ActiveDocument.Tables(1)
    
    ' last table
    Set t2 = ActiveDocument.Tables(ActiveDocument.Tables.Count)
    
    ' note: first row starts at 1, first column at 1
    ' we pick text from 1st row 2nd column and 
    ' use Val method to convert to appropriate type
    a = Val(t1.Cell(1, 2).Range.Text)
    
    ' then form some other cell
    b = Val(t1.Cell(2, 2).Range.Text)
    
    ' note Val stops converting at the first 
    ' character that cannot be interpreted
    ' as a numeric digit, numeric modifier, numeric punctuation, 
    ' or white space.
    
    ' To multiply a number use a Multiplication Operator *
    m = a * b
    
    t2.Cell(1, 1).Range.Text = "multiplication " & a & " x " & b & " =" & m
    
    End Sub
    

If you decide to continue your VBA quest, please read this article: http://visualbasic.about.com/od/learnvba/a/wdvbatble.htm

Seems it is about same challenging invoice task :-)