Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - "For Each - Next" problem

Reply
 
Old 05-22-2009   #1 (permalink)
lyngsie


 
 

"For Each - Next" problem

Hi All,

I get an "unexpexted Next" error in my script and cannot figure out where it
goes wrong. I've checked all End If's and cannot see that could be the
problem.

The script checks for Adobe versions and upgrade accordingly. If I comment
out the 2 "deep" If's the error goes away.

Can someone help me out?

CODE:
Dim objFSO
Dim objOutFile
Dim sWorkingFileName
Dim strLaunchCmd, errReturn, strUpgrade, strComputer
Const FOR_APPENDING = 8

sWorkingFileName = "%systemroot%\system32\adobe_update.log"
Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FileExists(sWorkingFileName) Then
Set objOutFile = objFSO.OpenTextFile(sWorkingFileName, FOR_APPENDING)
Else
Set objOutFile = objFSO.CreateTextFile(sWorkingFileName)
End If

strComputer = "."

Set objWMIService = GetObject("winmgmts:" & _
"{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colSoftware = objWMIService.ExecQuery _
("SELECT * FROM Win32_Product WHERE Name Like 'Adobe %'")

If colSoftware.Count > 0 Then

For Each objSoftware in colSoftware
If InStr(1,objSoftware.Caption,"Adobe Acrobat") <> 0 Then

If InStr(1,objSoftware.Version,"9.0") <> 0 Then
strLaunchCmd = "msiexec.exe /p
\\adserv1\Software\adobe-updates\AcroProStdUpd910_T1T2_incr.msp /qb"
strUpgrade = "Acrobat Pro 9.1.0"

Else If InStr(1,objSoftware.Version,"8.") <> 0 And
objSoftware.Version <> "8.1.5" Then
strLaunchCmd = "msiexec.exe /p
\\adserv1\Software\adobe-updates\AcrobatUpd815_all_incr.msp /qb"
strUpgrade = "Acrobat Pro 8.1.5"

Else If InStr(1,objSoftware.Version,"7.") <> 0 And
objSoftware.Version <> "7.1.2" Then
strLaunchCmd = "msiexec.exe /p
\\adserv1\Software\adobe-updates\AcrobatUpd712_all_incr.msp /qb"
strUpgrade = "Acrobat Pro 7.1.2"

Else If InStr(1,objSoftware.Version,"6.") <> 0 Then
MsgBox "Please ask IT-Support to upgrade your Adobe Acrobat"
End If
End If

' Launch upgrades

Set oShell = CreateObject("WScript.Shell")
oShell.CurrentDirectory = "\\adserv1\Software\adobe-updates\"
errReturn = oShell.Run(strLaunchCmd,1,True)

If errReturn = 0 Then
objOutFile.WriteLine "Upgrade was successful. Upgraded from " &
objSoftware.Caption & vbtab & objSoftware.Version & " to version " &
strUpgrade
Else
objOutFile.WriteLine "Upgrade failed - Error #" & errReturn
End if


' Check for 9.1.0 versions and upgrade to 9.1.1

If InStr(objSoftware.Version,"9.1.0") <> 0 Then
strLaunchCmd = "msiexec.exe /p
\\adserv1\Software\adobe-updates\AcrobatUpd911_all_incr.msp /qb"
End If

If InStr(objSoftware.Caption,"Adobe Reader") <> 0 Then

If InStr(1,objSoftware.Version,"9.") <> 0 And objSoftware.Version
<> "9.1.1" Then
strLaunchCmd = "msiexec.exe /p
\\adserv1\Software\adobe-updates\AdbeRdrUpd911_all_incr.msp /qb"
strUpgrade = "Acrobat Reader 9.1.1"

Else If InStr(1,objSoftware.Version,"8.") <> 0 And
objSoftware.Version <> "8.1.5" Then
strLaunchCmd = "msiexec.exe /p
\\adserv1\Software\adobe-updates\AdbeRdrUpd815_all_incr.msp /qb"
strUpgrade = "Acrobat Reader 8.1.5"

Else If InStr(1,objSoftware.Version,"7.") <> 0 And
objSoftware.Version <> "7.1.2" Then
strLaunchCmd = "msiexec.exe /p
\\adserv1\Software\adobe-updates\AdbeRdrUpd712_all_incr.msp /qb"
strUpgrade = "Acrobat Reader 7.1.2"

Else If InStr(1,objSoftware.Version,"6.0") <> 0 Then
MsgBox "Please ask IT-Support to upgrade your Adobe Reader"

End If

End If

errReturn = oShell.Run(strLaunchCmd,1,True)

If errReturn = 0 Then
objOutFile.WriteLine "Upgrade was successful. Upgraded from " &
objSoftware.Caption & vbtab & objSoftware.Version & " to version " &
strUpgrade
Else
objOutFile.WriteLine "Upgrade failed - Error #" & errReturn
End If
Next
End If


My System SpecsSystem Spec
Old 05-22-2009   #2 (permalink)
Pegasus [MVP]


 
 

Re: "For Each - Next" problem


"lyngsie" <lyngsie@xxxxxx> wrote in message
news:F2501DD8-8B62-4532-929D-762CADAB7DF0@xxxxxx
Quote:

> Hi All,
>
> I get an "unexpexted Next" error in my script and cannot figure out where
> it
> goes wrong. I've checked all End If's and cannot see that could be the
> problem.
>
> The script checks for Adobe versions and upgrade accordingly. If I comment
> out the 2 "deep" If's the error goes away.
>
> Can someone help me out?
>
> CODE:
> Dim objFSO
> Dim objOutFile
> Dim sWorkingFileName
> Dim strLaunchCmd, errReturn, strUpgrade, strComputer
> Const FOR_APPENDING = 8
>
> sWorkingFileName = "%systemroot%\system32\adobe_update.log"
> Set objFSO = CreateObject("Scripting.FileSystemObject")
>
> If objFSO.FileExists(sWorkingFileName) Then
> Set objOutFile = objFSO.OpenTextFile(sWorkingFileName, FOR_APPENDING)
> Else
> Set objOutFile = objFSO.CreateTextFile(sWorkingFileName)
> End If
>
> strComputer = "."
>
> Set objWMIService = GetObject("winmgmts:" & _
> "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
>
> Set colSoftware = objWMIService.ExecQuery _
> ("SELECT * FROM Win32_Product WHERE Name Like 'Adobe %'")
>
> If colSoftware.Count > 0 Then
>
> For Each objSoftware in colSoftware
> If InStr(1,objSoftware.Caption,"Adobe Acrobat") <> 0 Then
>
> If InStr(1,objSoftware.Version,"9.0") <> 0 Then
> strLaunchCmd = "msiexec.exe /p
> \\adserv1\Software\adobe-updates\AcroProStdUpd910_T1T2_incr.msp /qb"
> strUpgrade = "Acrobat Pro 9.1.0"
>
> Else If InStr(1,objSoftware.Version,"8.") <> 0 And
> objSoftware.Version <> "8.1.5" Then
> strLaunchCmd = "msiexec.exe /p
> \\adserv1\Software\adobe-updates\AcrobatUpd815_all_incr.msp /qb"
> strUpgrade = "Acrobat Pro 8.1.5"
>
> Else If InStr(1,objSoftware.Version,"7.") <> 0 And
> objSoftware.Version <> "7.1.2" Then
> strLaunchCmd = "msiexec.exe /p
> \\adserv1\Software\adobe-updates\AcrobatUpd712_all_incr.msp /qb"
> strUpgrade = "Acrobat Pro 7.1.2"
>
> Else If InStr(1,objSoftware.Version,"6.") <> 0 Then
> MsgBox "Please ask IT-Support to upgrade your Adobe Acrobat"
> End If
> End If
>
> ' Launch upgrades
>
> Set oShell = CreateObject("WScript.Shell")
> oShell.CurrentDirectory = "\\adserv1\Software\adobe-updates\"
> errReturn = oShell.Run(strLaunchCmd,1,True)
>
> If errReturn = 0 Then
> objOutFile.WriteLine "Upgrade was successful. Upgraded from " &
> objSoftware.Caption & vbtab & objSoftware.Version & " to version " &
> strUpgrade
> Else
> objOutFile.WriteLine "Upgrade failed - Error #" & errReturn
> End if
>
>
> ' Check for 9.1.0 versions and upgrade to 9.1.1
>
> If InStr(objSoftware.Version,"9.1.0") <> 0 Then
> strLaunchCmd = "msiexec.exe /p
> \\adserv1\Software\adobe-updates\AcrobatUpd911_all_incr.msp /qb"
> End If
>
> If InStr(objSoftware.Caption,"Adobe Reader") <> 0 Then
>
> If InStr(1,objSoftware.Version,"9.") <> 0 And objSoftware.Version
> <> "9.1.1" Then
> strLaunchCmd = "msiexec.exe /p
> \\adserv1\Software\adobe-updates\AdbeRdrUpd911_all_incr.msp /qb"
> strUpgrade = "Acrobat Reader 9.1.1"
>
> Else If InStr(1,objSoftware.Version,"8.") <> 0 And
> objSoftware.Version <> "8.1.5" Then
> strLaunchCmd = "msiexec.exe /p
> \\adserv1\Software\adobe-updates\AdbeRdrUpd815_all_incr.msp /qb"
> strUpgrade = "Acrobat Reader 8.1.5"
>
> Else If InStr(1,objSoftware.Version,"7.") <> 0 And
> objSoftware.Version <> "7.1.2" Then
> strLaunchCmd = "msiexec.exe /p
> \\adserv1\Software\adobe-updates\AdbeRdrUpd712_all_incr.msp /qb"
> strUpgrade = "Acrobat Reader 7.1.2"
>
> Else If InStr(1,objSoftware.Version,"6.0") <> 0 Then
> MsgBox "Please ask IT-Support to upgrade your Adobe Reader"
>
> End If
>
> End If
>
> errReturn = oShell.Run(strLaunchCmd,1,True)
>
> If errReturn = 0 Then
> objOutFile.WriteLine "Upgrade was successful. Upgraded from " &
> objSoftware.Caption & vbtab & objSoftware.Version & " to version " &
> strUpgrade
> Else
> objOutFile.WriteLine "Upgrade failed - Error #" & errReturn
> End If
> Next
> End If
>
There are two problems with the way you wrote your script:
- You do not use consistent indenting.
- Your code is so long that its structure is impossible to see for a human.
As a rule of thumb, a specific script component should span at most as many
lines as will fit on a screen, same as a paragraph in a printed book should
span than half a page at most. If you break this rule then you will get
lost, as demonstrated in your code.

Below is a modified version of your code. As you see in lines 20-26, there
is a clear structure for your main "If" and "For each" construct. There is
no doubt at all where the "end if" and "next" statements go. Note also:
- I changed several of your "else if" statements to "elseif".
- I did not run your code and I did not check if every variable is declared.
The "Option Explicit" statement will tell you at runtime.

[01] Option Explicit
[02] Dim objFSO, objOutFile, sWorkingFileName, strLaunchCmd,
[03] Dim errReturn, strUpgrade, strComputer
[04] Const FOR_APPENDING = 8
[05]
[06] sWorkingFileName = "%systemroot%\system32\adobe_update.log"
[07] Set objFSO = CreateObject("Scripting.FileSystemObject")
[08]
[09] If objFSO.FileExists(sWorkingFileName) Then
[10] Set objOutFile = objFSO.OpenTextFile(sWorkingFileName, FOR_APPENDING)
[11] Else
[12] Set objOutFile = objFSO.CreateTextFile(sWorkingFileName)
[13] End If
[14]
[15] Set objWMIService = GetObject("winmgmts:" & _
[16] "{impersonationLevel=impersonate}!\\.\root\cimv2")
[17] Set colSoftware = objWMIService.ExecQuery _
[18] ("SELECT * FROM Win32_Product WHERE Name Like 'Adobe %'")
[19]
[20] If colSoftware.Count > 0 Then
[21] For Each objSoftware In colSoftware
[22] CheckAcrobat
[23] LaunchUpgrades
[24] Upgrade91
[25] Next
[26] End If
[27] '----------------------
[28] 'Check Acrobat Upgrades
[29] '----------------------
[30] Sub CheckAcrobat
[31] If InStr(1,objSoftware.Caption,"Adobe Acrobat") <> 0 Then
[32] If InStr(1,objSoftware.Version,"9.0") <> 0 Then
[33] strLaunchCmd = "msiexec.exe /p
\\adserv1\Software\adobe-updates\AcroProStdUpd910_T1T2_incr.msp /qb"
[34] strUpgrade = "Acrobat Pro 9.1.0"
[35] ElseIf InStr(1,objSoftware.Version,"8.") <> 0 And
objSoftware.Version <> "8.1.5" Then
[36] strLaunchCmd = "msiexec.exe /p
\\adserv1\Software\adobe-updates\AcrobatUpd815_all_incr.msp /qb"
[37] strUpgrade = "Acrobat Pro 8.1.5"
[38] ElseIf InStr(1,objSoftware.Version,"7.") <> 0 And
objSoftware.Version <> "7.1.2" Then
[39] strLaunchCmd = "msiexec.exe /p
\\adserv1\Software\adobe-updates\AcrobatUpd712_all_incr.msp /qb"
[40] strUpgrade = "Acrobat Pro 7.1.2"
[41] ElseIf InStr(1,objSoftware.Version,"6.") <> 0 Then
[42] MsgBox "Please ask IT-Support to upgrade your Adobe Acrobat"
[43] End If
[44] End If
[45] End Sub
[46] '----------------
[47] 'Launch Upgrades
[48] '----------------
[49] Sub LaunchUpgrades
[50] Set oShell = CreateObject("WScript.Shell")
[51] oShell.CurrentDirectory = "\\adserv1\Software\adobe-updates\"
[52] errReturn = oShell.Run(strLaunchCmd,1,True)
[53] If errReturn = 0 Then
[54] objOutFile.WriteLine "Upgrade was successful. Upgraded from " & _
[55] objSoftware.Caption & vbTab & objSoftware.Version & " to version "
& strUpgrade
[56] Else
[57] objOutFile.WriteLine "Upgrade failed - Error #" & errReturn
[58] End If
[59] End Sub
[60] '-------------------
[61] 'Launch 9.1 Upgrades
[62] '-------------------
[63] Sub Upgrade91
[64] If InStr(objSoftware.Version,"9.1.0") <> 0 Then
[65] strLaunchCmd = "msiexec.exe /p
\\adserv1\Software\adobe-updates\AcrobatUpd911_all_incr.msp /qb"
[66] End If
[67]
[68] If InStr(objSoftware.Caption,"Adobe Reader") <> 0 Then
[69] If InStr(1,objSoftware.Version,"9.") <> 0 And objSoftware.Version
<> "9.1.1" Then
[70] strLaunchCmd = "msiexec.exe /p
\\adserv1\Software\adobe-updates\AdbeRdrUpd911_all_incr.msp /qb"
[71] strUpgrade = "Acrobat Reader 9.1.1"
[72] ElseIf InStr(1,objSoftware.Version,"8.") <> 0 And
objSoftware.Version <> "8.1.5" Then
[73] strLaunchCmd = "msiexec.exe /p
\\adserv1\Software\adobe-updates\AdbeRdrUpd815_all_incr.msp /qb"
[74] strUpgrade = "Acrobat Reader 8.1.5"
[75] ElseIf InStr(1,objSoftware.Version,"7.") <> 0 And
objSoftware.Version <> "7.1.2" Then
[76] strLaunchCmd = "msiexec.exe /p
\\adserv1\Software\adobe-updates\AdbeRdrUpd712_all_incr.msp /qb"
[77] strUpgrade = "Acrobat Reader 7.1.2"
[78] ElseIf InStr(1,objSoftware.Version,"6.0") <> 0 Then
[79] MsgBox "Please ask IT-Support to upgrade your Adobe Reader"
[80] End If
[81] End If
[82]
[83] errReturn = oShell.Run(strLaunchCmd,1,True)
[84] If errReturn = 0 Then
[85] objOutFile.WriteLine "Upgrade was successful. Upgraded from " & _
[86] objSoftware.Caption & vbTab & objSoftware.Version & " to version "
& strUpgrade
[87] Else
[88] objOutFile.WriteLine "Upgrade failed - Error #" & errReturn
[89] End If
[90] End Sub


My System SpecsSystem Spec
Old 05-23-2009   #3 (permalink)
lyngsie


 
 

Re: "For Each - Next" problem

Thanks for your help. The final script became this:

Option Explicit
Dim objFSO , objFile, strLaunchCmd, objWMIService, colSoftware, objSoftware
Dim errReturn, strUpgrade, strComputer, strFilePath, objShell
Const FOR_APPENDING = 8

' Setup log file ----------
strFilePath = "C:\Windows\system32\adobe_update.log"
'On Error Resume Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(strFilePath) Then
Set objFile = objFSO.OpenTextFile(strFilePath, FOR_APPENDING)
Else
Set objFile = objFSO.CreateTextFile(strFilePath)
End If
'--------------------------

Set objShell = CreateObject("WScript.Shell")

Set objWMIService = GetObject("winmgmts:" & _
"{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colSoftware = objWMIService.ExecQuery _
("SELECT * FROM Win32_Product WHERE Name Like 'Adobe %'")

strLaunchCmd = "none"

If colSoftware.Count > 0 Then
For Each objSoftware In colSoftware
CheckAcrobat
CheckReader
Next
Cleanup
End If

'----------------
'Acrobat Upgrades
'----------------
Sub CheckAcrobat
If InStr(1,objSoftware.Caption,"Adobe Acrobat") <> 0 Then

If InStr(1,objSoftware.Version,"9.0") <> 0 Then
strLaunchCmd = "msiexec.exe /p
\\adserv1\Software\adobe-updates\AcroProStdUpd910_T1T2_incr.msp /qb"
strUpgrade = "Acrobat Pro 9.1.0"

ElseIf InStr(1,objSoftware.Version,"8.") <> 0 And objSoftware.Version <>
"8.1.5" Then
strLaunchCmd = "msiexec.exe /p
\\adserv1\Software\adobe-updates\AcrobatUpd815_all_incr.msp /qb"
strUpgrade = "Acrobat Pro 8.1.5"

ElseIf InStr(1,objSoftware.Version,"7.") <> 0 And objSoftware.Version <>
"7.1.2" Then
strLaunchCmd = "msiexec.exe /p
\\adserv1\Software\adobe-updates\AcrobatUpd712_all_incr.msp /qb"
strUpgrade = "Acrobat Pro 7.1.2"

ElseIf InStr(1,objSoftware.Version,"6.") <> 0 Then
MsgBox "Please ask IT-Support to upgrade your Adobe Acrobat"
End If

UpgradeAdobe

'Upgrade 9.1.0 -> 9.1.1 .................
If InStr(1,objSoftware.Version,"9.1.0") Or strUpgrade = "Acrobat Pro 9.1.0"
Then
strLaunchCmd = "msiexec.exe /p
\\adserv1\Software\adobe-updates\AcrobatUpd911_all_incr.msp /qb"
strUpgrade = "Acrobat 9.1.0 -> 9.1.1"
UpgradeAdobe
End If
'........................................
End If
End Sub


'-------------
'Check Reader
'-------------

Sub CheckReader
If InStr(objSoftware.Caption,"Adobe Reader") <> 0 Then
If InStr(1,objSoftware.Version,"9.") <> 0 And objSoftware.Version <> "9.1.1"
Then
strLaunchCmd = "msiexec.exe /p
\\adserv1\Software\adobe-updates\AdbeRdrUpd911_all_incr.msp /qb"
strUpgrade = "Acrobat Reader 9.1.1"

ElseIf InStr(1,objSoftware.Version,"8.") <> 0 And objSoftware.Version <>
"8.1.5" Then
strLaunchCmd = "msiexec.exe /p
\\adserv1\Software\adobe-updates\AdbeRdrUpd815_all_incr.msp /qb"
strUpgrade = "Acrobat Reader 8.1.5"

ElseIf InStr(1,objSoftware.Version,"7.") <> 0 And objSoftware.Version <>
"7.1.2" Then
strLaunchCmd = "msiexec.exe /p
\\adserv1\Software\adobe-updates\AdbeRdrUpd712_all_incr.msp /qb"
strUpgrade = "Acrobat Reader 7.1.2"

ElseIf InStr(1,objSoftware.Version,"6.0") <> 0 Then
MsgBox "Please ask IT-Support to upgrade your Adobe Reader"
End If
UpgradeAdobe
End If
End Sub

'------------------
'Upgrade Adobe Apps
'------------------

Sub UpgradeAdobe
If strLaunchCmd <> "none" Then
objFile.WriteLine "Upgrade to " & strUpgrade & " " & "Started at: " & Date &
" : " & Time
errReturn = objShell.Run(strLaunchCmd,1,True)
If errReturn = 0 Then
objFile.WriteLine "Upgrade was successful. Upgraded from " & _
objSoftware.Caption & vbTab & "(v." & objSoftware.Version & ") " & " to
version " & strUpgrade
objFile.WriteLine "------------------------------------"
Else
objFile.WriteLine "Upgrade failed - Error #" & errReturn
objFile.WriteLine "------------------------------------"
End If
strLaunchCmd = "none"
End If
End Sub

Sub Cleanup
objFile.Close
Set objFile = Nothing
Set objFSO = Nothing
Set objShell = Nothing
Set objWMIService = Nothing
End Sub

"Pegasus [MVP]" wrote:
Quote:

>
> "lyngsie" <lyngsie@xxxxxx> wrote in message
> news:F2501DD8-8B62-4532-929D-762CADAB7DF0@xxxxxx
Quote:

> > Hi All,
> >
> > I get an "unexpexted Next" error in my script and cannot figure out where
> > it
> > goes wrong. I've checked all End If's and cannot see that could be the
> > problem.
> >
> > The script checks for Adobe versions and upgrade accordingly. If I comment
> > out the 2 "deep" If's the error goes away.
> >
> > Can someone help me out?
> >
> > CODE:
> > Dim objFSO
> > Dim objOutFile
> > Dim sWorkingFileName
> > Dim strLaunchCmd, errReturn, strUpgrade, strComputer
> > Const FOR_APPENDING = 8
> >
> > sWorkingFileName = "%systemroot%\system32\adobe_update.log"
> > Set objFSO = CreateObject("Scripting.FileSystemObject")
> >
> > If objFSO.FileExists(sWorkingFileName) Then
> > Set objOutFile = objFSO.OpenTextFile(sWorkingFileName, FOR_APPENDING)
> > Else
> > Set objOutFile = objFSO.CreateTextFile(sWorkingFileName)
> > End If
> >
> > strComputer = "."
> >
> > Set objWMIService = GetObject("winmgmts:" & _
> > "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
> >
> > Set colSoftware = objWMIService.ExecQuery _
> > ("SELECT * FROM Win32_Product WHERE Name Like 'Adobe %'")
> >
> > If colSoftware.Count > 0 Then
> >
> > For Each objSoftware in colSoftware
> > If InStr(1,objSoftware.Caption,"Adobe Acrobat") <> 0 Then
> >
> > If InStr(1,objSoftware.Version,"9.0") <> 0 Then
> > strLaunchCmd = "msiexec.exe /p
> > \\adserv1\Software\adobe-updates\AcroProStdUpd910_T1T2_incr.msp /qb"
> > strUpgrade = "Acrobat Pro 9.1.0"
> >
> > Else If InStr(1,objSoftware.Version,"8.") <> 0 And
> > objSoftware.Version <> "8.1.5" Then
> > strLaunchCmd = "msiexec.exe /p
> > \\adserv1\Software\adobe-updates\AcrobatUpd815_all_incr.msp /qb"
> > strUpgrade = "Acrobat Pro 8.1.5"
> >
> > Else If InStr(1,objSoftware.Version,"7.") <> 0 And
> > objSoftware.Version <> "7.1.2" Then
> > strLaunchCmd = "msiexec.exe /p
> > \\adserv1\Software\adobe-updates\AcrobatUpd712_all_incr.msp /qb"
> > strUpgrade = "Acrobat Pro 7.1.2"
> >
> > Else If InStr(1,objSoftware.Version,"6.") <> 0 Then
> > MsgBox "Please ask IT-Support to upgrade your Adobe Acrobat"
> > End If
> > End If
> >
> > ' Launch upgrades
> >
> > Set oShell = CreateObject("WScript.Shell")
> > oShell.CurrentDirectory = "\\adserv1\Software\adobe-updates\"
> > errReturn = oShell.Run(strLaunchCmd,1,True)
> >
> > If errReturn = 0 Then
> > objOutFile.WriteLine "Upgrade was successful. Upgraded from " &
> > objSoftware.Caption & vbtab & objSoftware.Version & " to version " &
> > strUpgrade
> > Else
> > objOutFile.WriteLine "Upgrade failed - Error #" & errReturn
> > End if
> >
> >
> > ' Check for 9.1.0 versions and upgrade to 9.1.1
> >
> > If InStr(objSoftware.Version,"9.1.0") <> 0 Then
> > strLaunchCmd = "msiexec.exe /p
> > \\adserv1\Software\adobe-updates\AcrobatUpd911_all_incr.msp /qb"
> > End If
> >
> > If InStr(objSoftware.Caption,"Adobe Reader") <> 0 Then
> >
> > If InStr(1,objSoftware.Version,"9.") <> 0 And objSoftware.Version
> > <> "9.1.1" Then
> > strLaunchCmd = "msiexec.exe /p
> > \\adserv1\Software\adobe-updates\AdbeRdrUpd911_all_incr.msp /qb"
> > strUpgrade = "Acrobat Reader 9.1.1"
> >
> > Else If InStr(1,objSoftware.Version,"8.") <> 0 And
> > objSoftware.Version <> "8.1.5" Then
> > strLaunchCmd = "msiexec.exe /p
> > \\adserv1\Software\adobe-updates\AdbeRdrUpd815_all_incr.msp /qb"
> > strUpgrade = "Acrobat Reader 8.1.5"
> >
> > Else If InStr(1,objSoftware.Version,"7.") <> 0 And
> > objSoftware.Version <> "7.1.2" Then
> > strLaunchCmd = "msiexec.exe /p
> > \\adserv1\Software\adobe-updates\AdbeRdrUpd712_all_incr.msp /qb"
> > strUpgrade = "Acrobat Reader 7.1.2"
> >
> > Else If InStr(1,objSoftware.Version,"6.0") <> 0 Then
> > MsgBox "Please ask IT-Support to upgrade your Adobe Reader"
> >
> > End If
> >
> > End If
> >
> > errReturn = oShell.Run(strLaunchCmd,1,True)
> >
> > If errReturn = 0 Then
> > objOutFile.WriteLine "Upgrade was successful. Upgraded from " &
> > objSoftware.Caption & vbtab & objSoftware.Version & " to version " &
> > strUpgrade
> > Else
> > objOutFile.WriteLine "Upgrade failed - Error #" & errReturn
> > End If
> > Next
> > End If
> >
>
> There are two problems with the way you wrote your script:
> - You do not use consistent indenting.
> - Your code is so long that its structure is impossible to see for a human.
> As a rule of thumb, a specific script component should span at most as many
> lines as will fit on a screen, same as a paragraph in a printed book should
> span than half a page at most. If you break this rule then you will get
> lost, as demonstrated in your code.
>
> Below is a modified version of your code. As you see in lines 20-26, there
> is a clear structure for your main "If" and "For each" construct. There is
> no doubt at all where the "end if" and "next" statements go. Note also:
> - I changed several of your "else if" statements to "elseif".
> - I did not run your code and I did not check if every variable is declared.
> The "Option Explicit" statement will tell you at runtime.
>
> [01] Option Explicit
> [02] Dim objFSO, objOutFile, sWorkingFileName, strLaunchCmd,
> [03] Dim errReturn, strUpgrade, strComputer
> [04] Const FOR_APPENDING = 8
> [05]
> [06] sWorkingFileName = "%systemroot%\system32\adobe_update.log"
> [07] Set objFSO = CreateObject("Scripting.FileSystemObject")
> [08]
> [09] If objFSO.FileExists(sWorkingFileName) Then
> [10] Set objOutFile = objFSO.OpenTextFile(sWorkingFileName, FOR_APPENDING)
> [11] Else
> [12] Set objOutFile = objFSO.CreateTextFile(sWorkingFileName)
> [13] End If
> [14]
> [15] Set objWMIService = GetObject("winmgmts:" & _
> [16] "{impersonationLevel=impersonate}!\\.\root\cimv2")
> [17] Set colSoftware = objWMIService.ExecQuery _
> [18] ("SELECT * FROM Win32_Product WHERE Name Like 'Adobe %'")
> [19]
> [20] If colSoftware.Count > 0 Then
> [21] For Each objSoftware In colSoftware
> [22] CheckAcrobat
> [23] LaunchUpgrades
> [24] Upgrade91
> [25] Next
> [26] End If
> [27] '----------------------
> [28] 'Check Acrobat Upgrades
> [29] '----------------------
> [30] Sub CheckAcrobat
> [31] If InStr(1,objSoftware.Caption,"Adobe Acrobat") <> 0 Then
> [32] If InStr(1,objSoftware.Version,"9.0") <> 0 Then
> [33] strLaunchCmd = "msiexec.exe /p
> \\adserv1\Software\adobe-updates\AcroProStdUpd910_T1T2_incr.msp /qb"
> [34] strUpgrade = "Acrobat Pro 9.1.0"
> [35] ElseIf InStr(1,objSoftware.Version,"8.") <> 0 And
> objSoftware.Version <> "8.1.5" Then
> [36] strLaunchCmd = "msiexec.exe /p
> \\adserv1\Software\adobe-updates\AcrobatUpd815_all_incr.msp /qb"
> [37] strUpgrade = "Acrobat Pro 8.1.5"
> [38] ElseIf InStr(1,objSoftware.Version,"7.") <> 0 And
> objSoftware.Version <> "7.1.2" Then
> [39] strLaunchCmd = "msiexec.exe /p
> \\adserv1\Software\adobe-updates\AcrobatUpd712_all_incr.msp /qb"
> [40] strUpgrade = "Acrobat Pro 7.1.2"
> [41] ElseIf InStr(1,objSoftware.Version,"6.") <> 0 Then
> [42] MsgBox "Please ask IT-Support to upgrade your Adobe Acrobat"
> [43] End If
> [44] End If
> [45] End Sub
> [46] '----------------
> [47] 'Launch Upgrades
> [48] '----------------
> [49] Sub LaunchUpgrades
> [50] Set oShell = CreateObject("WScript.Shell")
> [51] oShell.CurrentDirectory = "\\adserv1\Software\adobe-updates\"
> [52] errReturn = oShell.Run(strLaunchCmd,1,True)
> [53] If errReturn = 0 Then
> [54] objOutFile.WriteLine "Upgrade was successful. Upgraded from " & _
> [55] objSoftware.Caption & vbTab & objSoftware.Version & " to version "
> & strUpgrade
> [56] Else
> [57] objOutFile.WriteLine "Upgrade failed - Error #" & errReturn
> [58] End If
> [59] End Sub
> [60] '-------------------
> [61] 'Launch 9.1 Upgrades
> [62] '-------------------
> [63] Sub Upgrade91
> [64] If InStr(objSoftware.Version,"9.1.0") <> 0 Then
> [65] strLaunchCmd = "msiexec.exe /p
> \\adserv1\Software\adobe-updates\AcrobatUpd911_all_incr.msp /qb"
> [66] End If
> [67]
> [68] If InStr(objSoftware.Caption,"Adobe Reader") <> 0 Then
> [69] If InStr(1,objSoftware.Version,"9.") <> 0 And objSoftware.Version
> <> "9.1.1" Then
> [70] strLaunchCmd = "msiexec.exe /p
> \\adserv1\Software\adobe-updates\AdbeRdrUpd911_all_incr.msp /qb"
> [71] strUpgrade = "Acrobat Reader 9.1.1"
> [72] ElseIf InStr(1,objSoftware.Version,"8.") <> 0 And
> objSoftware.Version <> "8.1.5" Then
> [73] strLaunchCmd = "msiexec.exe /p
> \\adserv1\Software\adobe-updates\AdbeRdrUpd815_all_incr.msp /qb"
> [74] strUpgrade = "Acrobat Reader 8.1.5"
> [75] ElseIf InStr(1,objSoftware.Version,"7.") <> 0 And
> objSoftware.Version <> "7.1.2" Then
> [76] strLaunchCmd = "msiexec.exe /p
> \\adserv1\Software\adobe-updates\AdbeRdrUpd712_all_incr.msp /qb"
> [77] strUpgrade = "Acrobat Reader 7.1.2"
> [78] ElseIf InStr(1,objSoftware.Version,"6.0") <> 0 Then
> [79] MsgBox "Please ask IT-Support to upgrade your Adobe Reader"
> [80] End If
> [81] End If
> [82]
> [83] errReturn = oShell.Run(strLaunchCmd,1,True)
> [84] If errReturn = 0 Then
> [85] objOutFile.WriteLine "Upgrade was successful. Upgraded from " & _
> [86] objSoftware.Caption & vbTab & objSoftware.Version & " to version "
> & strUpgrade
> [87] Else
> [88] objOutFile.WriteLine "Upgrade failed - Error #" & errReturn
> [89] End If
> [90] End Sub
>
>
>
My System SpecsSystem Spec
Old 05-23-2009   #4 (permalink)
Pegasus [MVP]


 
 

Re: "For Each - Next" problem


"lyngsie" <lyngsie@xxxxxx> wrote in message
news:EC35CF38-8A1D-429A-A82D-93D375BFC506@xxxxxx
Quote:

> Thanks for your help. The final script became this:
I can see that you picked up my suggestion about giving your code a proper
structure. Unfortunately you chose to ignore my suggestion of indenting the
code, which would increase readability considerably. As a result you stepped
into a trap by adding one "endif" statement too many. You also invoke the
"UpgradeAdobe" subroutine twice, which is probably not what you intended.

About code indentation: This is your way of writing code -
If colSoftware.Count > 0 Then
For Each objSoftware In colSoftware
CheckAcrobat
CheckReader
Next
Cleanup
End If

and this is my way:
If colSoftware.Count > 0 Then
For Each objSoftware In colSoftware
CheckAcrobat
CheckReader
Next
Cleanup
End If

IMHO, code indentation gives you visual clues about the structure of the
code. Your way doesn't and it can lead to mistakes.


My System SpecsSystem Spec
Old 05-25-2009   #5 (permalink)
Al Dunbar


 
 

Re: "For Each - Next" problem


"Pegasus [MVP]" <news@xxxxxx> wrote in message
news:u7dsNd82JHA.6004@xxxxxx
Quote:

>
> "lyngsie" <lyngsie@xxxxxx> wrote in message
> news:EC35CF38-8A1D-429A-A82D-93D375BFC506@xxxxxx
Quote:

>> Thanks for your help. The final script became this:
>
> I can see that you picked up my suggestion about giving your code a proper
> structure. Unfortunately you chose to ignore my suggestion of indenting
> the code, which would increase readability considerably. As a result you
> stepped into a trap by adding one "endif" statement too many. You also
> invoke the "UpgradeAdobe" subroutine twice, which is probably not what you
> intended.
>
> About code indentation: This is your way of writing code -
> If colSoftware.Count > 0 Then
> For Each objSoftware In colSoftware
> CheckAcrobat
> CheckReader
> Next
> Cleanup
> End If
>
> and this is my way:
> If colSoftware.Count > 0 Then
> For Each objSoftware In colSoftware
> CheckAcrobat
> CheckReader
> Next
> Cleanup
> End If
>
> IMHO, code indentation gives you visual clues about the structure of the
> code. Your way doesn't and it can lead to mistakes.
Excellent advice, and certainly applicable to the OP's particular problem. I
would recommend indenting even the outermost elements, i.e.:

If colSoftware.Count > 0 Then
For Each objSoftware In colSoftware
CheckAcrobat
CheckReader
Next
Cleanup
End If

This will make non-indented comment lines stand out a mile, and make
linewrapping more obvious when including code in a ng post.

There are a few other equally simple techniques that, when combined with
consistent indenting, can make code much easier to read and maintain:

- vertical white space (i.e. blank lines). These should be used to break
script into "paragraphs" of code.

- add comments that define the intent of the code rather than describing
what the code does, i.e. this:

' next item
count = count + 1

not this:

' add one to the counter:
count = count + 1

- avoid mixing executable statements and comments on one line.

- use meaningful variable names, and don't use one variable for two
different purposes.


/Al


My System SpecsSystem Spec
Old 06-01-2009   #6 (permalink)
Petr Danes


 
 

Re: "For Each - Next" problem

My two cents worth on all this:

Lyngsie, I don't know if you picked up on it, but there is a HUGE difference
between
ELSEIF
and
ELSE IF (Extra spaces added to make the point clear)

This will work:
IF X then
Foo
ELSEIF Y then
Bar
END IF


This will not:
IF X then
Foo
ELSE IF Y then
Bar
END IF

The END IF is paired with the -second- IF, not the first IF, as it is in my
first example, and so the entire construct is unfinished, from the
interpreter's point of view. That's probably why you got your 'Unexpected
Next' error - the interpreter was looking for the closing END IF and
encountered the NEXT instead. The proper way to complete the second example
is like this:

IF X then
Foo
ELSE IF Y then
Bar
END IF
END IF

or better yet, from a formatting standpoint:

IF X then
Foo
ELSE
IF Y then
Bar
END IF
END IF

The first IF (with X) is paired with the second END IF, the second IF (with
Y) is paired with the first END IF. I didn't examine your initial script
very thoroughly, but am reacting to Pegasus' comment about having changed
such constructs in your script.

And obviously, proper indentation will help you keep track of such things,
as other responders here have pointed out.

Pete




"Al Dunbar" <alandrub@xxxxxx> píše v diskusním příspěvku
news:uJe89ba3JHA.1420@xxxxxx
Quote:

>
> "Pegasus [MVP]" <news@xxxxxx> wrote in message
> news:u7dsNd82JHA.6004@xxxxxx
Quote:

>>
>> "lyngsie" <lyngsie@xxxxxx> wrote in message
>> news:EC35CF38-8A1D-429A-A82D-93D375BFC506@xxxxxx
Quote:

>>> Thanks for your help. The final script became this:
>>
>> I can see that you picked up my suggestion about giving your code a
>> proper structure. Unfortunately you chose to ignore my suggestion of
>> indenting the code, which would increase readability considerably. As a
>> result you stepped into a trap by adding one "endif" statement too many.
>> You also invoke the "UpgradeAdobe" subroutine twice, which is probably
>> not what you intended.
>>
>> About code indentation: This is your way of writing code -
>> If colSoftware.Count > 0 Then
>> For Each objSoftware In colSoftware
>> CheckAcrobat
>> CheckReader
>> Next
>> Cleanup
>> End If
>>
>> and this is my way:
>> If colSoftware.Count > 0 Then
>> For Each objSoftware In colSoftware
>> CheckAcrobat
>> CheckReader
>> Next
>> Cleanup
>> End If
>>
>> IMHO, code indentation gives you visual clues about the structure of the
>> code. Your way doesn't and it can lead to mistakes.
>
> Excellent advice, and certainly applicable to the OP's particular problem.
> I would recommend indenting even the outermost elements, i.e.:
>
> If colSoftware.Count > 0 Then
> For Each objSoftware In colSoftware
> CheckAcrobat
> CheckReader
> Next
> Cleanup
> End If
>
> This will make non-indented comment lines stand out a mile, and make
> linewrapping more obvious when including code in a ng post.
>
> There are a few other equally simple techniques that, when combined with
> consistent indenting, can make code much easier to read and maintain:
>
> - vertical white space (i.e. blank lines). These should be used to break
> script into "paragraphs" of code.
>
> - add comments that define the intent of the code rather than describing
> what the code does, i.e. this:
>
> ' next item
> count = count + 1
>
> not this:
>
> ' add one to the counter:
> count = count + 1
>
> - avoid mixing executable statements and comments on one line.
>
> - use meaningful variable names, and don't use one variable for two
> different purposes.
>
>
> /Al
>
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Anno 1404 "save/load" problem and using "runas.exe" Gaming
Vista "Sleep" and "Hibernate" problem General Discussion
Unwanted Multiple contacts in "To","CC","BCC" of email send catago Vista mail
New Problem - "From" Address format in "Microsoft Communities" Live Mail
Vista not wotking with "My Computer" or "Control Panel", "Screen Saver" Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
Š Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46