Question

Ok, so I have been given the task to create a script that can increase or decrease the volume. My problem is when I run it, and type in "Decrease" then type in "29" it goes down to 0 then starts to loop. Can you please tell me where the loop is and how to fix it?

set Keys = CreateObject("WScript.Shell") 'So The Script Can Simulate Key Presses
set oShell = CreateObject("WScript.Shell") 'So The Script Can Control The Master Volume

'Asks The User If They Wish To Increase Or Decrease The Volume
Answer = InputBox("Increase Or Decrease Volume?", "Increase/Decrease Volume:")

If Answer = "Increase" Then 'If The User Types In Increase The Following Happens
    'Runs The Master Volume App.
    oShell.run"%SystemRoot%\System32\SndVol.exe"

    'Stops The Program For # Milliseconds
    WScript.Sleep 1500

    'Asks How Much To Increase The Volume By
    Amount = InputBox("How Much Do You Want To Turn The Volume Up?", "Increment:")

    'Pushes the Up Arrow Key The Amount Of Which The User Entered
    For X = 0 To Amount Step 1
        'Simulates The Pushing Of The Up Arrow
        Keys.SendKeys("{Up}")

        X =+ 1 'Counter Increment
    Next
ElseIf Answer = "Decrease" Then 'If The User Types In Decrease The Following Happens
    'Runs The Master Volume App.
    oShell.run"%SystemRoot%\System32\SndVol.exe"

    'Stops The Program For # Milliseconds
    WScript.Sleep 1500

    'Asks How Much To Decrease The Volume By
    Amount = InputBox("How Much Do You Want To Turn The Volume Down?", "Decrement:")

    'Pushes the Down Arrow Key The Amount Of Which The User Entered
    For X = 0 To Amount Step 1
        'Simulates The Pushing Of The Down Arrow
        Keys.SendKeys("{Down}")

        X =+ 1 'Counter Increment
    Next
ElseIf Answer = "" Then 'If The User Pushes Cancel The Following Happens
    Question = MsgBox("Do You Wish To Quit?",vbYesNo,"Quit:")

    'If The User Pushes Yes Then The Script Will End
    If Question = vbYes Then
        WScript.Quit 0 'Stops The Script
    End if
Else
    MsgBox("The Values Allowed Are:" & vbNewLine & "Increase" & vbNewLine & "Decrease")
End If
Was it helpful?

Solution

Does VBScript have Increment Operators

X = X + 1 is the proper way of achieving what you're trying to do with X =+ 1 (which may just be setting X to 1 over and over again). In your usage however, you can take those lines out completely since the For X = 0 To Amount Step 1 should already be handling the increment for you.

OTHER TIPS

There is no =+ (add and assign) operator in VBScript. Your

X =+ 1 'Counter Increment

is seen as

X = +1 ' assign +1 to X

Evidence:

>> X = 10
>> X =+ 1
>> WScript.Echo X
>>
1

You should delete those lines as the loop variable in a For To statement updates automagically.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top