Stefan Blom (
[email protected]) wrote:
: Since the document scrolls, I guess you are using the Selection object in
: your code? Working with Range objects should be safer.
: If you showed us your code, someone might be able to suggest improvements.
It's all fiddling with ActiveDocument.FormFields(...).Result on lots
of separate elements. I'll include the code, but don't blame me if it
makes your eyes hurt. It's my first attempt at scripting in Word.
Anyway, it's the g (get) and s (set) functions that actually interface
with the document, the rest is just calculations.
Sub calc()
Dim res
Application.ScreenUpdating = False
If (valid("a", "12345", "ovzg")) Then
res = 2 * bigsteps("a", "2345", "v") + 3 * bigsteps("a", "2345", "g") + 4 * bigsteps("a", "2345", "z")
Call s("totA", res * (g("a1z") * 20 + g("a1g") * 18 + g("a1v") * 16 + g("a1o") * 14) / 16)
Call s("totA2", g("totA") / 2)
Else
Call s("totA", "***")
Call s("totA2", "***")
End If
If (valid("b", "12345", "ovzg")) Then
res = 2 * bigsteps("b", "12345", "v") + 3 * bigsteps("b", "12345", "g") + 4 * bigsteps("b", "12345", "z")
Call s("totB", res)
Call s("totB2", g("totB") * 0.15)
Else
Call s("totB", "***")
Call s("totB2", "***")
End If
Application.ScreenUpdating = True
End Sub
Function g(pos)
g = ActiveDocument.FormFields(pos).Result * 1
End Function
Function s(pos, val)
ActiveDocument.FormFields(pos).Result = val
End Function
Sub AutoClose()
calc
End Sub
Function bigsteps(table, posrange, score)
bigsteps = 0
For i = 1 To Len(posrange)
bigsteps = bigsteps + g(table + Mid(posrange, i, 1) + score)
Next
End Function
Function smallsteps(table, pos, scorerange)
smallsteps = 0
For i = 1 To Len(scorerange)
smallsteps = smallsteps + g(table + pos + Mid(scorerange, i, 1))
Next
End Function
Function valid(table, posrange, scorerange)
valid = True
For i = 1 To Len(posrange)
valid = valid And (smallsteps(table, Mid(posrange, i, 1), scorerange) = 1)
Next
End Function
Dirk van Deun