keeping document steady during macro

  • Thread starter Thread starter Dirk van Deun
  • Start date Start date
D

Dirk van Deun

Hi,

I have made a word macro that takes information from several check
boxes in a longish document and then calculates some values and puts
them into text boxes. These elements don't all fit on the screen
at the same time. The macro works nicely, except for one thing: it
makes the document scroll. Is there a way to keep the document
steady while the macro is executing ?

Dirk van Deun
 
Until someone VBA-literate checks in with the actual code you need, I'll
just assure you that there is a way to do this: there's a specific line of
code that prevents screen refreshes while the code is running.

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA
http://word.mvps.org
 
I think I know which one you are refering to:
Application.ScreenUpdating = False. But that only stops the changes
in the scroll position of the document from being shown during
execution of the macro: so when the macro finishes, the scrolling
still happens (albeit in one jump instead of in several little jumps).

It's close, but not exactly what I mean. And whatever keywords I
tried in google, I always got that same line of code...

Suzanne S. Barnhill ([email protected]) wrote:
: Until someone VBA-literate checks in with the actual code you need, I'll
: just assure you that there is a way to do this: there's a specific line of
: code that prevents screen refreshes while the code is running.

: --
: Suzanne S. Barnhill
: Microsoft MVP (Word)
: Words into Type
: Fairhope, Alabama USA
: http://word.mvps.org

: : > Hi,
: >
: > I have made a word macro that takes information from several check
: > boxes in a longish document and then calculates some values and puts
: > them into text boxes. These elements don't all fit on the screen
: > at the same time. The macro works nicely, except for one thing: it
: > makes the document scroll. Is there a way to keep the document
: > steady while the macro is executing ?
: >

Dirk van Deun
 
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.

-- 
Stefan Blom
Microsoft Word MVP




---------------------------------------------
"Dirk van Deun" wrote in message

I think I know which one you are refering to:
Application.ScreenUpdating = False. But that only stops the changes
in the scroll position of the document from being shown during
execution of the macro: so when the macro finishes, the scrolling
still happens (albeit in one jump instead of in several little jumps).

It's close, but not exactly what I mean. And whatever keywords I
tried in google, I always got that same line of code...

Suzanne S. Barnhill ([email protected]) wrote:
: Until someone VBA-literate checks in with the actual code you need, I'll
: just assure you that there is a way to do this: there's a specific line of
: code that prevents screen refreshes while the code is running.

: --
: Suzanne S. Barnhill
: Microsoft MVP (Word)
: Words into Type
: Fairhope, Alabama USA
: http://word.mvps.org

: : > Hi,
: >
: > I have made a word macro that takes information from several check
: > boxes in a longish document and then calculates some values and puts
: > them into text boxes. These elements don't all fit on the screen
: > at the same time. The macro works nicely, except for one thing: it
: > makes the document scroll. Is there a way to keep the document
: > steady while the macro is executing ?
: >

Dirk van Deun
 
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
 
Back
Top