Hi, I have a small problem and mostly because I don't know VB very well. I am trying to run the script below which does some work on printers for me. I know the sub routine runs properly but I need to take a system variable and call a sub-routine so it maps the right printer for the department. Now I can't get the call command right, it grabs the variable - named local - but the when i use the 'call local' command it bombs out. Can someone tell me what I am doing wrong? Code: Dim Local Dim objNetwork Dim limeSCR Dim yellowSCR Set oshell = CreateObject( "WScript.Shell" ) Local = oshell.ExpandEnvironmentStrings("%Local%") Call Local() Sub SCR() limeSCR = "\\lime\SCR" yellowSCR = "\\yellow\SCR" Set objNetwork = CreateObject("WScript.Network") objNetwork.RemovePrinterConnection limeSCR objNetwork.SetDefaultPrinter yellowSCR End Sub WScript.Quit cheers guys
My VB is rather rusty, but surely you want to call the SCR sub, and pass it the Local variable, something like this: Code: Dim Local Dim objNetwork Dim limeSCR Dim yellowSCR Set oshell = CreateObject( "WScript.Shell" ) Local = oshell.ExpandEnvironmentStrings("%Local%") Call SCR(Local) Sub SCR(Local As SomeDataType) limeSCR = "\\lime\SCR" yellowSCR = "\\yellow\SCR" Set objNetwork = CreateObject("WScript.Network") objNetwork.RemovePrinterConnection limeSCR objNetwork.SetDefaultPrinter yellowSCR End Sub WScript.Quit
maybe i didn't explain myself. what I want to do it call a variable which is part of the local machine environment variables. this needs to be read by the vbscript and then set to a variable in the script and then i need to call that variable. this will then call a sub routine for that particular location.
I'm afraid I dont understand either... This makes no sense to me and probably why trigger failed to answer you properly. Your code failed as you are calling a function/sub called "Local" but you ahve not defined it anywhere. I think that you are trying to: 1. Get the value from an environment string called "local" 2. Wish to get the value of this environment variable into a VBScript variable. 3. Use the new variable as a switch to call specific functions. e.g. Code: Set oshell = CreateObject( "WScript.Shell" ) Local = oshell.ExpandEnvironmentStrings("%Local%") if Local = "xxx" then call SCR() end if You cannot normally call a variable... However in VBScript - it allows us to cheat and use the "EVAL" key word... so if you really want to execute the contents of a variable your code would read: Code: Set oshell = CreateObject( "WScript.Shell" ) Local = oshell.ExpandEnvironmentStrings("%Local%") ' Not this: ' Call Local() ' but this: eval (local)