Thursday 21 July 2011

Installing msi with if condition using wise script - Important

Get Environment Variable programfiles into Variable PF
Get Registry Key SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{54DF35BD-4A36-35DA-B029-A0C083C88614} place in Variable DISPLAYNAME
If DISPLAYNAME Equals "Google Chrome" then
   Execute msiexec.exe /x {54DF35BD-4A36-35DA-B029-A0C083C88614} /qb! (Wait)
End
Execute %SYS32%\wscript.exe "%INST%\Delete profiles.vbs" (Wait)
Execute msiexec.exe /i "%INST%\Chrome Settings.msi" ALLUSERS="1" REBOOT="ReallySuppress" /qb! (Wait)
Execute %SYS32%\CMD.EXE /c  "%INST%\Chrome.bat" (Wait)
Delete File(s) %PF%\Google\Update\GoogleUpdate.exe

How to package Adobe Photoshopelements 8.0

  Modified "1053.mst":
1.Deleted Desktop Shortcut
2. Commented custom action for Launch Condition "Install [ProductName] by using the Setup.exe in the root folder."
3. Included "Elements.ini" to suppress select country window that will appear after launch of shortcut.
4. AgreeToLicense No to Yes
5. ACCEPTEDEULA 0 to 1
6. DISABLEOLSFEATURES 0 to 1
7. DISABLEEMSFEATURES 0 to 1

Created "application.xml.override" file and placed in Adobe folder with the following contents.
            <?xml version="1.0" encoding="UTF-8"?>
            <Configuration>
            <Payload>
            <Data key="EULA">Suppress</Data>
            <Data key="Serial" protected="0">105716589549352762799634 </Data>
            <Data key="Registration">Suppress</Data>
            <Data key="Updates">Suppress</Data>
            </Payload>
            </Configuration>

Added custom action “CA_DEL_REG” to remove registry entry during uninstall.
 "HKCU\Software\Adobe\Photoshop Elements"
"HKCU\Software\Adobe\Elements Organizer"
"HKCU\Software\Classes\PhotoshopElements.Application.8"
"HKLM\Software\Adobe\Elements Organizer"
Added custom action "CA_DEL_Folders" to remove Folders  "Photoshop Elements" and " Elements Organizer" during uninstall.
    

How to package AdobePhotoshop elements 9 using wise script

Install:
-------
Execute msiexec.exe /i "%INST%\Adobe_Help\setup.msi" TRANSFORMS="%INST%\Adobe_Help\setup.mst" /qb! (Wait)
Set Variable EO to %INST%\ELEMENTSORGANIZER
Execute msiexec.exe /i "%INST%\Adobe Photoshop Elements 9.msi" TRANSFORMS="%INST%\1033.mst" REBOOT="ReallySuppress" /qb! (Wait)
Execute msiexec.exe /i "%EO%\Elements 9 Organizer.msi" TRANSFORMS="%EO%\Elements 9 Organizer.mst" REBOOT="ReallySuppress" /qb! (Wait)
Execute "%INST%\ElementsSTIInstaller\Set-up.exe" --mode=silent --deploymentFile="%INST%\ElementsSTIInstaller\deploy\ElementsSTIInstaller.install.xml" (Wait)

Uninstall:
----------
Execute msiexec.exe /x {007F778D-F15C-4EAB-AE92-071D21FAF632} NOT_STANDALONE=1 /qb! (Wait)
Execute msiexec.exe /x {433EACD8-4747-4A6A-826A-FFA9F39B0D40} NOT_STANDALONE=1 /qb! (Wait)
Get Environment Variable CommonProgramFiles into Variable CPF
Get Environment Variable APPDATA into Variable AD
Get Environment Variable ALLUSERSPROFILE into Variable AUP
Execute "%CPF%\Adobe\OOBE\PDApp\DWA\Setup.exe" --mode=silent --deploymentFile="%CPF%\Adobe\Elements9STIForPSE\ElementsSTIInstaller.remove.xml" (Wait)
Execute cmd.exe /c rmdir /s /q "%CPF%\Adobe\Elements9STIForPSE" (Wait)
Execute cmd.exe /c rmdir /s /q "%AD%\Adobe\Elements Organizer" (Wait)
Execute cmd.exe /c rmdir /s /q "%AD%\Adobe\ExtendScript Toolkit" (Wait)
Execute cmd.exe /c rmdir /s /q "%AUP%\Adobe\Photoshop Elements" (Wait)
Execute cmd.exe /c rmdir /s /q "%AUP%\Adobe\Elements Organizer" (Wait)
Execute cmd.exe /c reg delete "HKEY_CURRENT_USER\Software\Adobe\Elements Organizer" /f (Wait)
Edit 4 registry keys

Installing adobereader apps with multiple msp files

 1. Downloaded "AdbeRdrUpd942_all_incr.msp" from the internet. "AdbeRdrUpd943_all_incr.msp" has been given in the source media.

2. Modified the setup.ini with the patch 9.4.2 and 9.4.3 applied along with 9.4.1 in the [Product] section.
[Product]
PATCH=AdbeRdrUpd941_all_incr.msp;AdbeRdrUpd942_all_incr.msp;AdbeRdrUpd943_all_incr.msp

3. Created sms file with the Installation command line as
CommandLine=Setup.exe /rs /sPB(Which will install 9.4.0,9.4.1,9.4.2 and 9.4.3)

4. Created sms file with the Uninstallation and repair command line as
CommandLine=Msiexec /x {AC76BA86-7AD7-1033-7B44-A94000000001} /qb!    

Install and uninstall AdobePhtotoshop elements CS5 using wise script and packdoc on how to do the same


Install:
-------
Get Environment Variable commonprogramfiles into Variable CPF
Execute "%INST%\Set-up.exe" --mode=silent --deploymentFile="%INST%\Deploy\en_US_Deployment.xml" overrideFile="%INST%\Deploy\application.xml.override" (Wait)
Pause 10000 Milliseconds
Delete File(s) %CPF%\Adobe\Adobe PCD\cache\cache.db
Pause 1000 Milliseconds
Install File .\cache.db to %CPF%\Adobe\Adobe PCD\cache\cache.db
Execute msiexec.exe /i "%INST%\Settings\PhotoshopCS5 Settings.msi" ALLUSERS="1" REBOOT="ReallySuppress" /qb! (Wait)

Uninstall:
----------
Execute "%INST%\Set-up.exe" --mode=silent --deploymentFile="%INST%\Deploy\Uninstall.xml" (Wait)
Execute MsiExec.exe /x {DE3A9DC5-9A5D-6485-9662-347162C7E4CA} /Qb! (Wait)
Execute MsiExec.exe /x {0D2DBE8A-43D0-7830-7AE7-CA6C99A832E7} /Qb! (Wait)
Execute MsiExec.exe /x {CB2B43AD-1E1C-4C6F-BD2D-ABBB81CD30E3} /Qb! (Wait)
Execute %SYS32%\wscript.exe "DeleteFolder.vbs" (Wait)

Packdoc:
----------
1. Created "application.xml.override" file to add serial key, to support silent installation and placed in "Deploy" folder
2. Created "PhotoshopCS5 Settings.msi" to suppress the automatic updates in  the applications installed.

-Added "AdobeUpdaterAdminPrefs.dat" file to C:\ Program Files\Common Files\Adobe\AAMUpdaterInventory\1.0\ to suppress the updates in photoshop CS5 application.
-The automatic update in MEDIA PLAYER is under Options->Updates->Automatically check for Updates.
prefs.xml  added to %appdata%\Adobe\Adobe Media Player\Local Store\settings\

-Added "Bridge.reg"(Placed in Analyse Folder) file to suppress the dialog box which ask to run Bridge automatically to start at  logon.

-For Device central, GOTO "Help" Menu, click on updates,a Preferences dilog box will be opened, uncheck the option for "Notify me of NEw Updates" and "Updates for products".

-Adobe extension Manager, Update option is under the File-> Preferences->Updates
Unchecked, Check for Extension Updates

3.Created Install PhotoshopCS5.EXE using wise script editor.
-to install the application.
-to install the "PhotoshopCS5 Settings.msi".
-to replace the file "cache.db" file from from C:\Program Files\Commom Files\Adobe\  to suppress the registration screen for photoshop application.

4. Created "Uninstall PhotoshopCS5.EXE" to uninstall the application.  
-Repair of the application is not suported.

-The warnings and ICE03 is not resolved in "PhotoshopCS5 Settings.msi" as it’s a setting package.

-The ARP entry of "PhotoshopCS5 Settings.msi" is suppressed as it is a setting package.

-The Adobe Air 2.5 is given to install before installation of this application, as PhotoshopCS5 will install Adobe Air 1.5.

-When we launch the shortcut Adobe help, a dialog box pops up to update community help, this could not be suppressed as it comes everytime we launch the shortcut.
   Under Edit->Preferences, download preferences,  updater settings are done to disable.

-Clean uninstallation  is not supported. 

Packaging AdobeCaptivate Application

Adjustments/Notes
1. Installed Adobe Air application prior to installing this application.

2. Created "Install_Captivate5.EXE" for wrapping the Set-up.exe.

3. Extracted msi files which install Adobe Captivate Quiz Results Analyzer, Adobe Captivate Reviewer 2.0 and Adobe Community Help and created individual transforms for these msi's.

- In the transform file of Quiz Results Analyzer, deleted desktop shortcut; changed the INSTALLDIR of the package and added a property ARPSYSTEMCOMPONENT=1 since it was putting in two add/remove entries.

- In the tranform file of Captivate Reviewer, deleted desktop shortcut and added a cutom action(CA_SetINSTALLDIR) to change the INSTALLDIR.

- In the transform file of Adobe Help, deleted desktop shortcut; added a customaction (CA_SetINSTALLDIR) to change the INSTALLDIR; Added a file appDB.db to "%Appdata%\chc.4875E02D9FB21EE389F73B8D1702B320485DF8CE.1\Localstore\" to suppress the update of the local help content; Added a customAction to delete the file "[ProgramFilesFolder]Adobe\Adobe Help\updateConfig.xml" to suppress automatic updates of the application and added active setup to copy the user specific files to all the users.

4. Added scripts in the wse to install these msi files.

5. Installation of the Captivate 5 package is performed using the below parameters:

Set-up.exe --mode=silent --deploymentFile="deploy\AdobeCaptivate5.0AllLocales.install.xml" overrideFile="deploy\override.xml".

When the shortcut is launched it pops up a screen as below
"Some Adobe Captivate features such as screen capture and audio might not work as intended without admin privileges". Tried to suppress the screen by selecting "Do not show this message again". But the settings corresponding to this is fixed through settings.msi file which is installed by the wrapper exe.

The settings.msi has ICE 91 warnings which is achieved through active setup.

6. Created Uninstall_Captivate5.EXE to uninstall Adobe Captivate Quiz Results Analyzer, Adobe Captivate Reviewer 2.0 and Adobe Community Help and uninstallation of captivate 5 is performed by using the below parameter:
Set-up.exe --mode=silent --deploymentFile="deploy\AdobeCaptivate5.0AllLocales.remove.xml". Also added scripts in it to remove the files and registries remaining after uninstallation of the application.



Malfunctions/Known errors
1. Repair of the application is not supported.

2. Clean un-installation is not supported since Adobe Photoshop CS also shares same files/folders

Packaging AdobeAIR application

1. Executed the media and extracted files from Temp folder.

2. Created transform file for the extracted "setup.msi".

3. Added a file (eulaAccepted) to [AppDataFolder]Adobe\AIR to support installation of the transform.

4. The application was not downloading .air files from adobe air market place site. Hence added two files (digest.s and airappinstaller.exe) to "[AppDataFolder]Macromedia\Flash Player\www.macromedia.com\bin\airappinstaller".

5. Added the below registry key to suppress automatic updates.
(link: http://help.adobe.com/en_US/air/admin/air_admin_guide.pdf)

[HKEY_LOCAL_MACHINE\Software\Policies\Adobe\AIR]
"UpdateDisabled"=dword:00000001

6. Added active setup to repair the application since user specific entries are added in the package.

7. Added the below custom actions in the package to support clean uninstallation:

CA_CleanUp - to remove [AIR1]
CA_CleanUp1 - to remove [LocalAppDataFolder]Adobe\AIR
CA_DelReg - to remove "HKEY_LOCAL_MACHINE\SOFTWARE\Adobe\Adobe AIR"

Installing msi, msi and mst and exe using wise script

Execute msiexec.exe /i "%INST%\Captivate5QRA\setup.msi" TRANSFORMS="%INST%\Captivate5QRA\setup.mst" /qb! (Wait)
Execute msiexec.exe /i "%INST%\Captivate5Reviewer\setup.msi" TRANSFORMS="%INST%\Captivate5Reviewer\setup.mst" /qb! (Wait)
Execute msiexec.exe /i "%INST%\Adobe_Help\setup.msi" TRANSFORMS="%INST%\Adobe_Help\setup.mst" /qb! (Wait)
Set Variable DEP to %INST%\Deploy
Execute "%INST%\Set-up.exe" --mode=silent --deploymentFile="%DEP%\AdobeCaptivate5.0AllLocales.install.xml" --overrideFile="%DEP%\override.xml" (Wait)
Execute msiexec.exe /i "%INST%\Settings.msi" /qb! (Wait)

Uninstalling hotfix exe with the silent parameters using wise script

Execute %INST%\lotus_notes852FP1_win.exe /d /s -s -a /v"HOTFIXACTION=UNINSTALL /qb!" (Wait)

Installing hotfix exe with the silent parameters using wise script

Execute %INST%\lotus_notes852FP1_win.exe /d /s -s -a /v"HOTFIXACTION=INSTALL /qb!" (Wait)

Uninstalling msi using wise script

Execute msiexec.exe /x {54DF35BD-4A36-35DA-B029-A0C083C88614} /qb! (Wait)
Pause 50000 Milliseconds
Execute msiexec.exe /x {F280F9F0-1B94-4278-9424-34B7C82A0440} /qb! (Wait)

Installing msi with if condition using wise script

Get Environment Variable programfiles into Variable PF
Get Registry Key SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{54DF35BD-4A36-35DA-B029-A0C083C88614} place in Variable DISPLAYNAME
If DISPLAYNAME Equals "Google Chrome" then
   Execute msiexec.exe /x {54DF35BD-4A36-35DA-B029-A0C083C88614} /qb! (Wait)
End
Execute %SYS32%\wscript.exe "%INST%\Delete profiles.vbs" (Wait)
Execute msiexec.exe /i "%INST%\Chrome Settings.msi" ALLUSERS="1" REBOOT="ReallySuppress" /qb! (Wait)
Execute %SYS32%\CMD.EXE /c  "%INST%\Chrome.bat" (Wait)
Delete File(s) %PF%\Google\Update\GoogleUpdate.exe

How to package google chrome

 The media given is an exe but I downloaded "GoogleChromeStandaloneEnterprise.msi" as setup capture generates many ICE errors.

The product code of previous version of google chrome and this version is same, So used wise script to support upgrade.

 Created "Chrome Settings.msi" for the settings of the package.
- As the product codes are same..If we add the settings files which stores in user profile folder and add active set-up to get the settings for every user, the active setup does not work as it would have ran active setup for previous version of application.
Tried to remove active setup registry from every user if it had ran with other user, but could not achive through VBscript also.

1. Created transform file for GoogleChromeStandaloneEnterprise.msi 
- Added CA_Uninstall1 to uninstall the application.
-Added custome actions  CA_Delete_Folder, CA_Delete_Folder2, CA_Delete_Folder3, CA_Del_Reg2,  and CA_remove2.
- Disabled the custom action " CallUninstaller ".
2. Created Chrome Settings.msi
- Added files to [LocalAppDataFolder]Google\Chrome\
(-The files First Run , service State and Local State under Local Settings\Application Data\Google\Chrome\User Data which contains the settings as per the given document
- Added file "Preferences" to choose Google as “Choose a search engine” under Local Settings\Application Data\Google\Chrome\User Data\Default.)

-Added active setup key
-the application is made to hide ARP entry as it is a settings msi

3. Created  Install Google Chrome.EXE using wise script editor add added
-Commnad to check if previous verion is installed.
-Command to uninstall the previous verion of google chorme.
- Command to execute "Delete profiles.vbs" which deletes folder   "[LocalAppDataFolder]Google\Chrome"  from every user.
(the settings for the application resides in the files from this location)
- Command line to install batch file which installs msi and mst.
(The msi and mst files could not be run directly from wisescript as the characters of file name exceeds 32 character)
-Commnad line to install Chrome settings.msi.
- Commnad to delete GoogleUpdate.exe from C:\ProgramFiles\Google\Update
(Which suppresse to check for update when you click on "About Google Chrome" in "Settings").

4. Created Uninstall Google Chrome.EXE to uninstalla the application and chrome settings application.  

 - The keys HKLM\Software\Google\Update and HKCU\Software\Google keys are not deleted as the sub keys under these keys were not specific to Chrome application and may share with other applications also.

-Repair of the application is not supported.

VBScript To delete desktop shortcut and Start Menu Shortcut duirng Installation in Windows Vista

set fso =createobject("scripting.filesystemobject")
set wshshell = createobject("wscript.shell")
Desktop = wshshell.expandenvironmentstrings("%PUBLIC%")
StartMenu = wshshell.expandenvironmentstrings("%ProgramData%")
 
If fso.FolderExists(StartMenu   & "\Microsoft\Windows\Start Menu\Programs\Nortel VPN Client") Then
   fso.DeleteFile(StartMenu & "\Microsoft\Windows\Start Menu\Programs\Nortel VPN Client\Uninstall Nortel VPN Client.lnk")
fso.DeleteFile(Desktop  & "\Desktop\Nortel VPN Client.lnk")
End If

Disable License Agreement,Help assistance and auto Updates in Itunes


Prerequisite for Installing the Application
1. AppleApplicationSupport.msi
2. AppleMobileDeviceSupport.msi
3. QuickTime.msi

Post Installation
1. After installing iTunes.msi you need to install     MobileMe.msi

Go to Start Menu and launch Itunes


Shows License Agreement Window 

Click on Agree

if the User dont want  
1. Add MP3 and AAC files
2.Add WMA files (As Default)
then Uncheck the Checkbox and Click Next.


Select "No i'll Change the file and folder names myself"
then Click Next.


Click on Finish the application is launched.

Click on Edit->Preferences


UnCheck "Check for updates automatically"
close the application and launch again.


Help Assistance,license Agreement and Updates are disabled.

The Below files will have modified  information.

1.C:\Documents and Settings\[USERPROFILE]\Application Data\apple Computer\itunes\iTunesPrefs.xml

2.C:\Documents and Settings\[USERPROFILE] LocalSettings\Application Data\apple computer\itunes\iTunesPrefs.xml 

Copy these two files to  C:\Documents and Settings\[USERPROFILE]\Application Data\apple Computer\itunes

and

 C:\Documents and Settings\[USERPROFILE] LocalSettings\Application Data\apple computer\itunes   using Active setup

VB Script for Deleting Registry entry with subkeys using Functions

On Error Resume Next
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
strKeyPath  = "SOFTWARE\Classes\CLSID\{6B5B1E2A-014B-11D5-A66A-001083345DAA}"
strKeyPath1 = "SOFTWARE\Classes\CLSID\{A77B8FE1-63A8-427E-B880-E3FFAEF9689D}"
strKeyPath3 = "SOFTWARE\Classes\FactSet.DownloadSite"
strKeyPath4 = "SOFTWARE\Classes\FactSet.UploadSite"
strKeyPath5 = "SOFTWARE\Classes\TypeLib\{FD26D8D4-FC65-11D0-A209-080009AC118F}"
strKeyPath6 = "SOFTWARE\Classes\Interface\{030D99A0-F656-11D1-A23C-080009AC118F}"
strKeyPath7 = "SOFTWARE\Classes\Interface\{FD26D8E1-FC65-11D0-A209-080009AC118F}"
Set objRegistry = GetObject("winmgmts:\\" & _
    strComputer & "\root\default:StdRegProv")
DeleteSubkeys HKEY_LOCAL_MACHINE, strKeypath
DeleteSubkeys HKEY_LOCAL_MACHINE, strKeypath1
DeleteSubkeys HKEY_LOCAL_MACHINE, strKeypath2
DeleteSubkeys HKEY_LOCAL_MACHINE, strKeypath3
DeleteSubkeys HKEY_LOCAL_MACHINE, strKeypath4
DeleteSubkeys HKEY_LOCAL_MACHINE, strKeypath5
DeleteSubkeys HKEY_LOCAL_MACHINE, strKeypath6
DeleteSubkeys HKEY_LOCAL_MACHINE, strKeypath7 
Sub DeleteSubkeys(HKEY_LOCAL_MACHINE, strKeyPath)
    objRegistry.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubkeys
    If IsArray(arrSubkeys) Then
        For Each strSubkey In arrSubkeys
            DeleteSubkeys HKEY_LOCAL_MACHINE, strKeyPath & "\" & strSubkey
        Next
    End If
    objRegistry.DeleteKey HKEY_LOCAL_MACHINE, strKeyPath
End Sub

Visua basic Scripting basics and Concepts

VBScript- VBScript (visual Basic Scripting) is scripting language developed by Microsoft for windows operating systems.
A VBScript code must be executed within a host environment. It allows you to interact with the host environment to perform some Programming tasks.
Internet Explorer (IE) - allows you to include VBScript code in HTML documents to be executed while IE is rendering HTML Documents.
Windows Script Host (WSH) - Allows you to include VB Script code in Script files to be executed directly on the windows operating system.
VBScript is actually is a limited variation of Microsoft's Visual Basic programming language. Therefore VBScript shares the same language syntax as Visual Basic

VARIABLES
Like many other programming languages, Visual Basic (VB) uses variables to reserve memory to store data and to name that memory location.
A variable must be declared with a name and a specific data type.

Example.
1. Explicit Declaration - A variable is declared with the "Dim" statement in the following syntax:
   Dim variable_name As data_type
Where “Variable_name” is  name of the variable and data_type data type for variable.
2. Implicit Declaration - A variable name is used on the left side of an assignment statement without
Being declared previously. The data type of an implicitly declared variable is "Variant

Assigning Values to Variables
variable_name = data_value. Value=8;
Variable_name  is name of the variable
Data value is  value assigned to the variable.



Numeric Comparisons
             Numeric comparisons are operations that:
Operates on two numeric operands.
             Produces a Boolean value by applying the comparison operation specified
a.     Equal to (=): Resulting (True) if two operands are numerically equal.
b.    Not equal to (<>): Resulting (True) if two operands are numerically not equal.
c.     Greater than (>): Resulting (True) if the first operand is numerically greater than the second operand.
d.    Less than (<): Resulting (True) if the first operand is numerically less than the second operand.
e.    Greater than or equal to (>=): Resulting (True) if the first operand is numerically greater than or equal to the second operand.
f.        Less than or equal to (<=): Resulting (True) if the first operand is numerically less than or equal to the second operand  .


Math Built-In Functions
Abs(vNumber) - Returns the absolute value of a specified number
Atn(vNumber) - Returns the arctangent of a specified number
Cos(vNumber) - Returns the cosine of a specified number (angle)
Exp(vPower) - Returns e raised to a power
Hex(vNumber) - Returns the hexadecimal value of a specified number
Int(vNumber) - Returns the integer part of a specified number
Fix(vNumber) - Returns the integer part of a specified number
Log(vNumber) - Returns the natural logarithm of a specified number








                                     Conditional Statements
Using conditional statements, you can write VBScript code that makes decisions and repeats actions. The following conditional statements are available in VBScript:
Making Decisions Using If...Then...Else
The If...Then...Else statement is used to evaluate whether a condition is True or False and, depending on the result, to specify one or more statements to run. Usually the condition is an expression that uses a comparison operator to compare one value or variable with another. For information about comparison operators, see Comparison Operators. If...Then...Else statements can be nested to as many levels as you need.
Running Statements if a Condition is True
To run only one statement when a condition is True, use the single-line syntax for the If...Then...Else statement. The following example shows the single-line syntax. Notice that this example omits the Else keyword.
Sub FixDate()
    Dim myDate
    myDate = #2/13/95#
    If myDate < Now Then myDate = Now
End Sub
To run more than one line of code, you must use the multiple-line (or block) syntax. This syntax includes the End If statement, as shown in the following example:
Sub AlertUser(value)
   If value = 0 Then
      AlertLabel.ForeColor = vbRed
      AlertLabel.Font.Bold = True
      AlertLabel.Font.Italic = True
   End If
End Sub
Running Certain Statements if a Condition is True and Running Others if a Condition is False
You can use an If...Then...Else statement to define two blocks of executable statements: one block to run if the condition is True, the other block to run if the condition is False.
Sub AlertUser(value)
   If value = 0 Then
      AlertLabel.ForeColor = vbRed
      AlertLabel.Font.Bold = True
      AlertLabel.Font.Italic = True
   Else
      AlertLabel.Forecolor = vbBlack
      AlertLabel.Font.Bold = False
      AlertLabel.Font.Italic = False
   End If
End Sub




Looping Through Code
Looping allows you to run a group of statements repeatedly. Some loops repeat statements until a condition is False; others repeat statements until a condition is True. There are also loops that repeat statements a specific number of times.
The following looping statements are available in VBScript:
  • Do...Loop: Loops while or until a condition is True.
  • While...Wend: Loops while a condition is True.
  • For...Next: Uses a counter to run statements a specified number of times.
  • For Each...Next: Repeats a group of statements for each item in a collection or each element of an array.
 Using Do Loops
You can use Do...Loop statements to run a block of statements an indefinite number of times. The statements are repeated either while a condition is True or until a condition becomes True.
Repeating Statements While a Condition is True
Use the While keyword to check a condition in a Do...Loop statement. You can check the condition before you enter the loop (as shown in the following ChkFirstWhile example), or you can check it after the loop has run at least once (as shown in the ChkLastWhile example). In the ChkFirstWhile procedure, if myNum is set to 9 instead of 20, the statements inside the loop will never run. In the ChkLastWhile procedure, the statements inside the loop run only once because the condition is already False.
Sub ChkFirstWhile()
   Dim counter, myNum
   counter = 0
   myNum = 20
   Do While myNum > 10
      myNum = myNum - 1
      counter = counter + 1
   Loop
   MsgBox "The loop made " & counter & " repetitions."
End Sub

Sub ChkLastWhile()
   Dim counter, myNum
   counter = 0
   myNum = 9
   Do
      myNum = myNum - 1
      counter = counter + 1
   Loop While myNum > 10
   MsgBox "The loop made " & counter & " repetitions."
End Sub
Repeating a Statement Until a Condition Becomes True
There are two ways to use the Until keyword to check a condition in a Do...Loop statement. You can check the condition before you enter the loop (as shown in the following ChkFirstUntil example), or you can check it after the loop has run at least once (as shown in the ChkLastUntil example). As long as the condition is False, the looping occurs.
Sub ChkFirstUntil()
   Dim counter, myNum
   counter = 0
   myNum = 20
   Do Until myNum = 10
      myNum = myNum - 1
      counter = counter + 1
   Loop
   MsgBox "The loop made " & counter & " repetitions."
End Sub

Sub ChkLastUntil()
   Dim counter, myNum
   counter = 0
   myNum = 1
   Do
      myNum = myNum + 1
      counter = counter + 1
   Loop Until myNum = 10
   MsgBox "The loop made " & counter & " repetitions."
End Sub
Exiting a Do...Loop Statement from Inside the Loop
You can exit a Do...Loop by using the Exit Do statement. Because you usually want to exit only in certain situations, such as to avoid an endless loop, you should use the Exit Do statement in the True statement block of an If...Then...Else statement. If the condition is False, the loop runs as usual.
In the following example, myNum is assigned a value that creates an endless loop. The If...Then...Else statement checks for this condition, preventing the endless repetition.
Sub ExitExample()
   Dim counter, myNum
      counter = 0
      myNum = 9
      Do Until myNum = 10
         myNum = myNum - 1
         counter = counter + 1
         If myNum < 10 Then Exit Do
      Loop
      MsgBox "The loop made " & counter & " repetitions."
End Sub
 Using While...Wend
The While...Wend statement is provided in VBScript for those who are familiar with its usage. However, because of the lack of flexibility in While...Wend, it is recommended that you use Do...Loop instead.
 Using For...Next
You can use For...Next statements to run a block of statements a specific number of times. For loops, use a counter variable whose value increases or decreases with each repetition of the loop.
The following example causes a procedure called MyProc to execute 50 times. The For statement specifies the counter variable x and its start and end values. The Next statement increments the counter variable by 1.
Sub DoMyProc50Times()
   Dim x
   For x = 1 To 50
      MyProc
   Next
End Sub
Using the Step keyword, you can increase or decrease the counter variable by the value you specify. In the following example, the counter variable j is incremented by 2 each time the loop repeats. When the loop is finished, the total is the sum of 2, 4, 6, 8, and 10.
Sub TwosTotal()
   Dim j, total
   For j = 2 To 10 Step 2
      total = total + j
   Next
   MsgBox "The total is " & total
End Sub
To decrease the counter variable, use a negative Step value. You must specify an end value that is less than the start value. In the following example, the counter variable myNum is decreased by 2 each time the loop repeats. When the loop is finished, total is the sum of 16, 14, 12, 10, 8, 6, 4, and 2.
Sub NewTotal()
   Dim myNum, total
   For myNum = 16 To 2 Step -2
      total = total + myNum
   Next
   MsgBox "The total is " & total
End Sub
You can exit any For...Next statement before the counter reaches its end value by using the Exit For statement. Because you usually want to exit only in certain situations, such as when an error occurs, you should use the Exit For statement in the True statement block of an If...Then...Else statement. If the condition is False, the loop runs as usual.
 Using For Each...Next
A For Each...Next loop is similar to a For...Next loop. Instead of repeating the statements a specified number of times, a For Each...Next loop repeats a group of statements for each item in a collection of objects or for each element of an array. This is especially helpful if you don't know how many elements are in a collection.
Sub cmdChange_OnClick
   Dim d   'Create a variable
   Set d = CreateObject("Scripting.Dictionary")
   d.Add "0", "Athens"   'Add some keys and items
   d.Add "1", "Belgrade"
   d.Add "2", "Cairo"

   For Each I in d
      Document.frmForm.Elements(I).Value = D.Item(I)
   Next
End Sub