Link to home
Start Free TrialLog in
Avatar of JavierLopezMCP
JavierLopezMCPFlag for United States of America

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
Avatar of g3nu1n3
g3nu1n3
Flag of United States of America image

Here's a good tutorial on VBScript Functions - http://www.tizag.com/vbscriptTutorial/vbscriptfunctions.php
Avatar of Bill Prew
Bill Prew

Avatar of JavierLopezMCP

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()???????
ASKER CERTIFIED SOLUTION
Avatar of TommySzalapski
TommySzalapski
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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(strDeptDriveLetter) then
      objNetwork.RemoveNetworkDrive strDeptDriveLetter, True, True
      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
@billprew

Thanks Bill.. There is tons on great stuff here.. I've spend the better part of today just reading some great post.