JavierLopezMCP
asked on
How to use VBS Methods
Hello everyone.. Its my first post.. So hopefully I won't sound stupid.. LOL
I would like to create method with the following lines and be able to reference the lines within an IF Then statement using VBS
For i = 0 To UBound(aryDeptPath)
If InStr(strGroup, "cn=" & Lcase(aryDeptPath(i,0))) then
objNetwork.MapNetworkDrive strDeptDriveLetter, aryDeptPath(i,1)
Exit For
End If
Next
The rest of my script work perfectly. I would just like to clean it up a little and I believe if I can create a method and reference it. I will save some lines of code.
Thanks in advance
I would like to create method with the following lines and be able to reference the lines within an IF Then statement using VBS
For i = 0 To UBound(aryDeptPath)
If InStr(strGroup, "cn=" & Lcase(aryDeptPath(i,0))) then
objNetwork.MapNetworkDrive
Exit For
End If
Next
The rest of my script work perfectly. I would just like to clean it up a little and I believe if I can create a method and reference it. I will save some lines of code.
Thanks in advance
Here's a good tutorial on VBScript Functions - http://www.tizag.com/vbscriptTutorial/vbscriptfunctions.php
A couple of additional useful info sources:
http://www.w3schools.com/vbscript/vbscript_procedures.asp
http://www.academictutorials.com/vbscript/vbscript-functions.asp
~bp
http://www.w3schools.com/vbscript/vbscript_procedures.asp
http://www.academictutorials.com/vbscript/vbscript-functions.asp
~bp
ASKER
That code i wrote is completely done and i just want to call it within a statement... how would i write it and call it??
Function myFuntion()
For i = 0 To UBound(aryDeptPath)
If InStr(strGroup, "cn=" & Lcase(aryDeptPath(i,0))) then
objNetwork.MapNetworkDrive strDeptDriveLetter, aryDeptPath(i,1)
Exit For
End If
Next
End Function
Call myFunction()???????
Function myFuntion()
For i = 0 To UBound(aryDeptPath)
If InStr(strGroup, "cn=" & Lcase(aryDeptPath(i,0))) then
objNetwork.MapNetworkDrive
Exit For
End If
Next
End Function
Call myFunction()???????
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Oh. I didn't see the strDeptDriveLetter. You should probably pass that in too. So line 1 would be
Sub mapDrives(theGroup, thePathArray, strDeptDriveLetter)
and line 12 would just add that too like
Call mapDrives(strGroup, aryDeptPath, strDeptDriveLetter)
This would work too
mapDrives(strGroup, aryDeptPath, "a")
Or any other string variable could go there. That's what makes functions and subs so useful.
Sub mapDrives(theGroup, thePathArray, strDeptDriveLetter)
and line 12 would just add that too like
Call mapDrives(strGroup, aryDeptPath, strDeptDriveLetter)
This would work too
mapDrives(strGroup, aryDeptPath, "a")
Or any other string variable could go there. That's what makes functions and subs so useful.
ASKER
This is what I ended up with..
Thanks .. it was a huge help
' This is sub procedure that checks the users department and set the path dependent on the dept the user belongs to
Sub subMapMyDeptDrive()
For i = 0 To UBound(aryDeptPath)
If InStr(strGroup, "cn=" & Lcase(aryDeptPath(i,0))) then
objNetwork.MapNetworkDrive strDeptDriveLetter, aryDeptPath(i,1)
Exit For
End If
Next
End Sub
' Map the departmental drive
If objFSO.DriveExists(strDept DriveLette r) then
objNetwork.RemoveNetworkDr ive strDeptDriveLetter, True, True
WScript.Sleep(1000)
Call subMapMyDeptDrive
Else
Call subMapMyDeptDrive
End If
Thanks .. it was a huge help
' This is sub procedure that checks the users department and set the path dependent on the dept the user belongs to
Sub subMapMyDeptDrive()
For i = 0 To UBound(aryDeptPath)
If InStr(strGroup, "cn=" & Lcase(aryDeptPath(i,0))) then
objNetwork.MapNetworkDrive
Exit For
End If
Next
End Sub
' Map the departmental drive
If objFSO.DriveExists(strDept
objNetwork.RemoveNetworkDr
WScript.Sleep(1000)
Call subMapMyDeptDrive
Else
Call subMapMyDeptDrive
End If
@JavierLopezMCP
And by the way WELCOME to Experts Exchange, glad you got some useful feedback on this question and hope you enjoy participating in future questions.
~bp
And by the way WELCOME to Experts Exchange, glad you got some useful feedback on this question and hope you enjoy participating in future questions.
~bp
ASKER
@billprew
Thanks Bill.. There is tons on great stuff here.. I've spend the better part of today just reading some great post.
Thanks Bill.. There is tons on great stuff here.. I've spend the better part of today just reading some great post.