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 - Sub problem

Reply
 
Old 08-10-2009   #1 (permalink)
James


 
 

Sub problem

I am trying to put all of my existing vbs into a sub. I need an exit point
in my script if a certain parameter is not met. But, when I do this, my
code will not execute.

Sub ProfileRestore

Code here

End Sub

However, if I remove the lines (Sub ProfileRestore & End Sub) then all of my
code executes perfectly. But, I need to put my code into a sub because i
need an exit statement if a certain parameter is not met. I cannot do that
w/o a sub. This was as simple as pie in VB6 but not so straight forward in
vbs. Any reason why nothing happens?

James



My System SpecsSystem Spec
Old 08-10-2009   #2 (permalink)
James


 
 

Re: Sub problem

Never mind...found it

WScript.Quit [exitcode]

James

"James" <dontemailme@xxxxxx> wrote in message
news:%23UvA43bGKHA.4436@xxxxxx
Quote:

>I am trying to put all of my existing vbs into a sub. I need an exit point
>in my script if a certain parameter is not met. But, when I do this, my
>code will not execute.
>
> Sub ProfileRestore
>
> Code here
>
> End Sub
>
> However, if I remove the lines (Sub ProfileRestore & End Sub) then all of
> my code executes perfectly. But, I need to put my code into a sub because
> i need an exit statement if a certain parameter is not met. I cannot do
> that w/o a sub. This was as simple as pie in VB6 but not so straight
> forward in vbs. Any reason why nothing happens?
>
> James
>

My System SpecsSystem Spec
Old 08-10-2009   #3 (permalink)
Paul Randall


 
 

Re: Sub problem

I think you found how to make your code work the way you want, but not the
reasone adding Sub and End Sub statements caused "But, when I do this, my
code will not execute".

In a .VBS file, all statements are part of the main program unless they are
enclosed within a Sub/End Sub or Function/End Function pair of statements.
Here is a short example of poor programming techniques:

Sub S1
MsgBox "S1 message only occurs if S1 is called."
End Sub
MsgBox "Line 1 of main has executed."
Function F1
MsgBox "F1 message only occures if F1 is called."
End function
MsgBox "Line 2 of main has executed."
S1
F1

-Paul Randall

"James" <dontemailme@xxxxxx> wrote in message
news:eJtBx7bGKHA.3392@xxxxxx
Quote:

> Never mind...found it
>
> WScript.Quit [exitcode]
>
> James
>
> "James" <dontemailme@xxxxxx> wrote in message
> news:%23UvA43bGKHA.4436@xxxxxx
Quote:

>>I am trying to put all of my existing vbs into a sub. I need an exit
>>point in my script if a certain parameter is not met. But, when I do
>>this, my code will not execute.
>>
>> Sub ProfileRestore
>>
>> Code here
>>
>> End Sub
>>
>> However, if I remove the lines (Sub ProfileRestore & End Sub) then all of
>> my code executes perfectly. But, I need to put my code into a sub
>> because i need an exit statement if a certain parameter is not met. I
>> cannot do that w/o a sub. This was as simple as pie in VB6 but not so
>> straight forward in vbs. Any reason why nothing happens?
>>
>> James
>>
>
>

My System SpecsSystem Spec
Old 08-10-2009   #4 (permalink)
James


 
 

Re: Sub problem

I was looking for some comments on my script anyway so maybe you or others
can offer up some advice for improvements...

You will notice where I used WScript.Quit [exitcode]. My original plan was
to use Sub RestoreProfile() then and end sub and then just use and exit sub
where I put WScript.Quit [exitcode]. But like I said the script just did
nothing when I tried it. That has never happend to me using VB6...but
anyway...

Set objNetwork = CreateObject("Wscript.Network")
Set objSHApp = CreateObject("Shell.Application")
Set WshShell = WScript.CreateObject("WScript.Shell")
Set filesys=CreateObject("Scripting.FileSystemObject")
Const FOF_SIMPLEPROGRESS = 256 '(&H100)
Const HKEY_CURRENT_USER = &H80000001

'EnumValues Method of the StdRegProv Class (change as needed)
'HKEY_CLASSES_ROOT = &H80000000
'HKEY_CURRENT_USER = &H80000001
'HKEY_LOCAL_MACHINE = &H80000002
'HKEY_USERS = &H80000003
'HKEY_CURRENT_CONFIG = &H80000004
'HKEY_DYN_DATA = &H80000005

'Check if ProfileBackup Folder exists on G:\
If Not filesys.FolderExists("g:\ProfileBackup") Then
msgbox "Please Perform a Backup First..."
WScript.Quit [exitcode]
End If

'Set registry strings
strComputer = "."
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &
strComputer & "\root\default:StdRegProv")
strKeyPath = "Control Panel\Desktop"
strKeyName = "Wallpaper"
strKeyValue = "C:\WINDOWS\Web\Wallpaper\Bliss.bmp"

'Return User Name
strUserName = objNetwork.UserName
If filesys.FolderExists("c:\documents and settings\" & strUserName &
".KROLL")Then
strUserName = objNetwork.UserName & ".KROLL"
Else
strUserName = objNetwork.UserName
End if

'Set the source path strings
strSrcDesktop = "g:\ProfileBackup\Desktop\*.*"
strSrcFavs = "g:\ProfileBackup\Favorites\*.*"
strSrcMyDocs = "g:\ProfileBackup\My Documents\*.*"
strSrcPsiData = "g:\ProfileBackup\PsiData\*.*"
strSrcEmailSig = "g:\ProfileBackup\Signatures\*.*"
strSrcWallpaper = "g:\ProfileBackup\Wallpaper\*.*"
strSrcBliss = "h:\VbScripts\Bliss.bmp"

'Set the destination path strings
strDestDesktop = "c:\documents and settings\" & strUserName & "\Desktop\"
strDestFavs = "c:\documents and settings\" & strUserName & "\Favorites\"
strDestMyDocs = "c:\documents and settings\" & strUserName & "\My
Documents\"
strDestPsiData = "c:\documents and settings\" & strUserName & "\PsiData\"
strDestEmailSig = "c:\documents and settings\" & strUserName & "\Application
Data\Microsoft\Signatures\"
strDestWallpaper = "C:\WINDOWS\Web\Wallpaper\"

' Using shell.application to copy since faster and displays status/progress

'Restore Wallpaper
objSHApp.Namespace(strDestWallpaper).CopyHere strSrcWallpaper
objSHApp.Namespace(strDestWallpaper).CopyHere strSrcBliss

'Restore User Desktop
objSHApp.Namespace(strDestDesktop).CopyHere strSrcDesktop

'Restore User Favorites
objSHApp.Namespace(strDestFavs).CopyHere strSrcFavs

'Restore User My Documents
objSHApp.Namespace(strDestMyDocs).CopyHere strSrcMyDocs

'Restore User PsiData
If Not filesys.FolderExists (strDestPsiData) Then
filesys.CreateFolder (strDestPsiData)
objSHApp.Namespace(strDestPsiData).CopyHere strSrcPsiData
End If

'Restore User Email Signatures
If Not filesys.FolderExists (strDestEmailSig) Then
filesys.CreateFolder (strDestEmailSig)
objSHApp.Namespace(strDestEmailSig).CopyHere strSrcEmailSig
End If

' Set registry value for wallpaper
Return =
objReg.SetStringValue(HKEY_CURRENT_USER,strKeyPath,strKeyName,strKeyValue)
If (Return <> 0) And (Err.Number <> 0) Then
'Wscript.Echo Err.Number
End If

Set objSHApp = nothing

BtnPress = WshShell.Popup("Profile Restore Successfully ",10,
"Progress", 0 + 64)

James

"Paul Randall" <paulr901@xxxxxx> wrote in message
news:uYL5YHdGKHA.4620@xxxxxx
Quote:

>I think you found how to make your code work the way you want, but not the
>reasone adding Sub and End Sub statements caused "But, when I do this, my
>code will not execute".
>
> In a .VBS file, all statements are part of the main program unless they
> are enclosed within a Sub/End Sub or Function/End Function pair of
> statements. Here is a short example of poor programming techniques:
>
> Sub S1
> MsgBox "S1 message only occurs if S1 is called."
> End Sub
> MsgBox "Line 1 of main has executed."
> Function F1
> MsgBox "F1 message only occures if F1 is called."
> End function
> MsgBox "Line 2 of main has executed."
> S1
> F1
>
> -Paul Randall
>
> "James" <dontemailme@xxxxxx> wrote in message
> news:eJtBx7bGKHA.3392@xxxxxx
Quote:

>> Never mind...found it
>>
>> WScript.Quit [exitcode]
>>
>> James
>>
>> "James" <dontemailme@xxxxxx> wrote in message
>> news:%23UvA43bGKHA.4436@xxxxxx
Quote:

>>>I am trying to put all of my existing vbs into a sub. I need an exit
>>>point in my script if a certain parameter is not met. But, when I do
>>>this, my code will not execute.
>>>
>>> Sub ProfileRestore
>>>
>>> Code here
>>>
>>> End Sub
>>>
>>> However, if I remove the lines (Sub ProfileRestore & End Sub) then all
>>> of my code executes perfectly. But, I need to put my code into a sub
>>> because i need an exit statement if a certain parameter is not met. I
>>> cannot do that w/o a sub. This was as simple as pie in VB6 but not so
>>> straight forward in vbs. Any reason why nothing happens?
>>>
>>> James
>>>
>>
>>
>
>

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
the problem with ndis.sys Blue Screen of Death problem solved? Vista General
Re: Windows Mail Attachement Problem and Adobe Player Problem with IE8 Vista mail
Multi-select problem in Windows Explorer BIG PROBLEM!!!!!! Vista General
Generic McAfee Problem Message in Vista Problem Reports Vista performance & maintenance
Vista Upgrade Problem - Windows Explorer Loop problem Vista installation & setup


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