Scope problem in VBA?

C

Chris Watts

I am having problems with what I think is a variable scope problem in a VBA
macro for PPT 2007 under Windows XP that I am writing - code is at the end
of this message.
I wish to add some code containing the line
oShp.Top = oShp.Top - Shift
within the procedure PauseMove().
Naturally it works within the procedure in which these variables are
declared, namely MoveUp().
I have tried declaring both oShp and Shift as Module-wide and as Public -
but still neither variable seems to be recognised within PauseMove.
What am I doing wrong?
Advice welcomed.
TIA
cheers
Chris

====================
Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long)
As Integer
Private Const KEY_PRESSED As Integer = &H1000

Sub MoveUp(oShp As Shape)
'Scroll an image upwards within the limits of its _
original top point and the bottom of the screen
'Includes provison for stopping and restarting the scroll _
Q key to Halt; R key to Resume.

'Determine the endpoint for the motion.
'In this case when the bottom edge of the image lines _
up with starting point for the top of the image.
Endpoint = oShp.Top - oShp.Height

'Defines the bottom of the screen where the bottom of the imgae should
stop
Offset = 475

'Define amount to move image each time
Shift = 20

While oShp.Top > Endpoint + Offset

'Move the picture a little upwards
oShp.Top = oShp.Top - Shift

'Re-render the screen
DoEvents

'Pause scrolling on pressing Q key
If GetKeyState(vbKeyQ) And KEY_PRESSED Then PauseMove

'Loop back if not done
Wend
End Sub

Sub PauseMove()
PauseMore:
'Resume scrolling on pressing Q key
If GetKeyState(vbKeyR) And KEY_PRESSED Then GoTo PauseEnd

DoEvents
GoTo PauseMore
PauseEnd:
End Sub
====================================================
"Steve Rindsberg" <[email protected]
 
C

Chris Watts

I have made some progress with this having realised that scope of variables
and of object properties are defined fifferently!

Add the following Declarations:

Dim Shift As Integer
Private oShp As Shape
Public Property Get Top() As Integer
Top = oShp.Top
End Property

makes the variable Shift totally visible in MoveUp.
It also makes the object oShp visible there too but I cannot read from, or
write to, its property oShp.Top

Again, suggestions welcome.
cheers
Chris
 
D

David Marcovitz

I think I'll need more details on what you tried. Because oShp is a
parameter of MoveUp, a module-wide declaration of oShp will be overridden by
the parameter. You need to choose one or the other (but not both) of
declaring a variable module-wide and passing that variable as a parameter.
--David
--
David M. Marcovitz
Author of _Powerful PowerPoint for Educators_
http://www.PowerfulPowerPoint.com/
Microsoft PowerPoint MVP
Associate Professor, Loyola University Maryland
 
D

David Marcovitz

I'm not sure what you're trying to do with the Property. Just because you
declare a variable doesn't mean that it is assigned a value. After
declaring, have you Set oShp to actually point to a specific shape? As I
said in my earlier post, you can't do that by passing oShp as a parameter
because the passed oShp will be a different variable than the declared oShp.
--David
--
David M. Marcovitz
Author of _Powerful PowerPoint for Educators_
http://www.PowerfulPowerPoint.com/
Microsoft PowerPoint MVP
Associate Professor, Loyola University Maryland
 
C

Chris Watts

Thanks Steve,
Your suggestions, and correction of my poor technique, are much welcomed.
I ceased regular everyday programming just as OOP came in!!!

As a result of your input, I have now got it to do pretty much what I want -
just some minor logic to tidy up.
Many thanks.

Bear with me for a short while (we are in different timezones - GMT in my
case) whilst I do some tidying up and compress the image that I have been
using down to something reasonable . I will then send a PPT to you.

Chris
 
C

Chris Watts

Thanks David,
Your input is much welcomed.
As a result of yours and Steve's, I have now got it to do pretty much what I
want - and am much wiser..
Many thanks.
Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top