Help needed with loop and case statement

Discussion in 'Scripting' started by Steve Furniss, Aug 3, 2005.

  1. Hi, I have a script that is to be run by our admins once thay have joined a
    new computer to the domain. The script asks for the local server and set an
    environment variable, then it asks for the version of offiice and sets
    another environment variable. The script works fine apart for the input box
    menu for the office variable. When this box appears, the admin is given a
    choice of A, B, or C. If the admin puts either A, B,or C the script works
    fine, if thay put something like D in the box it put a popup on screen
    telling then thay have entered an invalid input and re-displays the Office
    menu input box. If they don't put a value in the input box and click OK or
    click the Cancel button the script ends.

    How do I get the script to do the same as if they enter an invalid input if
    they don't put any value in and click OK and how do I get the canel button to
    work correctly ???

    '---------------------
    'Constants & Variables
    '---------------------

    Option Explicit
    On Error Resume Next
    Dim WSHShell, strLocalsrv, intOffice, strLocalsrvKey, strLocalsrvValue,
    strOfficeKey, strOfficeValue, strMenu, strReply
    Dim strComputer
    Const HKEY_LOCAL_MACHINE = &H80000002

    '----------
    'Main Logic
    '----------

    strComputer = "."
    Set WSHShell = WScript.CreateObject("WScript.Shell")

    MsgBox("This script will ask you for the local Server name and the version
    of MS Office you are using." & _
    vbCrLf & "It will set the environment variables for localsrv and office,
    add the correct Domain groups to the local Administrators group")

    strLocalsrv = InputBox("Please enter the name of the local Server for this
    computer. e.g. TESTSRV1")

    Call SetLocalsrv

    Call OfficeMenu

    do
    strReply = Ucase(InputBox(strMenu, "MS Office Version Selection"))
    Select Case strReply
    Case "A"
    call CaseA
    Exit do
    Case "B"
    call CaseB
    Exit do
    Case "C"
    call CaseC
    Exit Do
    Case Else
    call InvalidInput
    End Select
    Loop While strReply <>""

    WScript.Quit

    '-------------------------
    'Function & Sub Procedures
    '-------------------------
    Sub SetLocalsrv()
    strLocalsrvKey =
    "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session
    Manager\Environment\localsrv"
    strLocalsrvValue = strLocalsrv
    WSHShell.RegWrite strLocalsrvKey, strLocalsrvValue
    End Sub

    Sub OfficeMenu()
    strMenu = "Please select the version of MS Office that has been installed
    on this computer." & vbCrLf & vbCrLf
    strMenu = strMenu & "A: Office 2000" & vbCrLf
    strMenu = strMenu & "B: Office XP (2002)" & vbCrLf
    strMenu = strMenu & "C: Office 2003" & vbCrLf
    End Sub

    Sub SetOfficeVar()
    strOfficeKey = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session
    Manager\Environment\office"
    strOfficeValue = intOffice
    WSHShell.RegWrite strOfficeKey, strOfficeValue
    End Sub

    Sub CaseA()
    intOffice = "9.0"
    Call SetOfficeVar
    call ScriptComplete
    End Sub

    Sub CaseB()
    intOffice = "10.0"
    Call SetOfficeVar
    call ScriptComplete
    End Sub

    Sub CaseC()
    intOffice = "11.0"
    Call SetOfficeVar
    call ScriptComplete
    End Sub

    Sub Quit()
    WScript.Quit
    End Sub

    Sub InvalidInput()
    WSHShell.Popup "Invalid Input. Please Try again"
    call OfficeMenu
    End Sub

    Sub ScriptComplete()
    WSHShell.Popup "All tasks completed"
    End Sub
    '-------------------------
     
    Steve Furniss, Aug 3, 2005
    #1
    1. Advertisements

  2. Hi,

    Just change
    Loop While strReply <>""
    to
    Loop While strReply = ""


    But why ask the admin about the Office version at all when you can
    obtain that information from registry?


    Try this script:

    '--------------------8<----------------------

    WScript.Echo "Office version: " & GetOfficeVersion


    Function GetOfficeVersion

    ' return the most recent Office version installed

    Dim objShell, arrOfficeVersions, strOfficeVersion
    Dim strRegValue, strRegData

    Set objShell = CreateObject("WScript.Shell")
    arrOfficeVersions = Array("11.0","10.0","9.0")

    ' value returned if no Office version is found
    GetOfficeVersion = ""

    ' find the most recent Office version installed
    On Error Resume Next
    For Each strOfficeVersion In arrOfficeVersions
    strRegData = "" ' init value
    strRegValue = "HKLM\SOFTWARE\Microsoft\Office\" _
    & strOfficeVersion & "\Common\InstallRoot\Path"
    strRegData = objShell.RegRead(strRegValue)
    If strRegData <> "" Then
    GetOfficeVersion = strOfficeVersion
    Exit Function ' ---->
    End If
    Next

    End Function

    '--------------------8<----------------------
     
    Torgeir Bakken \(MVP\), Aug 3, 2005
    #2
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.