Results 1 to 11 of 11
  1. #1
    Lounger
    Join Date
    Jul 2003
    Location
    Burnaby, Br. Columbia, Canada
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Network Drives (Word VBA)

    Hi all,

    I am wondering whether there is a way (using Word VBA) to check to see what network drives the user has access to. Either the drive letter or the share name (preferable). I'd like to check this out for the user and then I plan to list it in a combobox.

    Thanks! <img src=/S/confused3.gif border=0 alt=confused3 width=45 height=45>

  2. #2
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: Network Drives (Word VBA)

    Not what you asked, but is <!post=Don's post using BrowseForFolder,271140>Don's post using BrowseForFolder<!/post>, and one of the applicable shellspecialfolderconstants of use?
    -John ... I float in liquid gardens
    UTC -7ąDS

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Network Drives (Word VBA)

    I think we've had a post on this in the past few months, so try a search here. It's probably repeating myself to say: the FileSystemObject has a Drives collection which will include mapped drive letters.

  4. #4
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Network Drives (Word VBA)

    As noted in previous reply by jscher2000, the easy way to do this is use Scripting FileSystemObject (FSO) - just loop thru the Drives collection, test the DriveType and ShareName properties for each Drive object found. If DriveType is Remote (constant value 3) then it's a network drive. If network drive, ShareName property returns UNC path (assumes are connected to network). As alternative to FSO, you can use Windows API functions to do this. For sample, see attached text file, exported code module. GetLogicalDrives function returns all logical drives as a bitmask Long integer value. GetDriveType function gets drive type - note the Windows constants are not same as those used by FSO; user-defined GetDriveTypeString function translates return value to a text string. WNetGetConnection is used to get UNC path. Run EnumDrives() sub to list all drives found, drive type, UNC path (if not network drive, drive letter displayed).

    For FSO code examples, recommend search, many examples have been previously posted.

    HTH

  5. #5
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Network Drives (Word VBA)

    For the record, FSO version of routine to enumerate drives:

    <code>Public Sub EnumDrivesFSO()</code>

    <code> Dim fso As FileSystemObject</code>
    <code> Dim drv As Scripting.Drive</code>

    <code> Set fso = New FileSystemObject</code>

    <code> For Each drv In fso.Drives</code>
    <code> Debug.Print drv.DriveLetter & ": " & _</code>
    <code> GetDriveTypeStringFSO(drv.DriveType), _</code>
    <code> "UNC Path: " & drv.ShareName</code>
    <code> Next drv</code>

    <code> Set fso = Nothing</code>
    <code> Set drv = Nothing</code>

    <code>End Sub</code>

    User-defined function to return text string for drive type:

    <code>Private Function GetDriveTypeStringFSO _</code>
    <code> (ByVal lDriveType As Scripting.DriveTypeConst) As String</code>

    <code> Select Case lDriveType</code>
    <code> Case Removable '1</code>
    <code> GetDriveTypeStringFSO = "Removable"</code>
    <code> Case Fixed '2</code>
    <code> GetDriveTypeStringFSO = "Fixed"</code>
    <code> Case Remote '3</code>
    <code> GetDriveTypeStringFSO = "Network"</code>
    <code> Case CDRom '4</code>
    <code> GetDriveTypeStringFSO = "CD"</code>
    <code> Case RamDisk '5</code>
    <code> GetDriveTypeStringFSO = "RAM"</code>
    <code> Case Unknown '0</code>
    <code> GetDriveTypeStringFSO = "Unknown"</code>
    <code> Case Else</code>
    <code> GetDriveTypeStringFSO = "Unknown"</code>
    <code> End Select</code>

    <code>End Function</code>

    The above requires reference set to MS Scripting Runtime library. If this library not available, API methods can be used instead. On network system at work both API & FSO versions of EnumDrives() procedure returned correct results.

    HTH

  6. #6
    2 Star Lounger
    Join Date
    Sep 2003
    Location
    Juneau, Alaska, USA
    Posts
    110
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Network Drives (Word VBA)

    Is this scripting library the same VBScript that's the culprit in so many attacks? IIRC it's been recommended for at least a couple of years that it be removed/deactivated unless you use it. Here, of course, you're using it (if it's the same one) but how would the security advice affect things when you distribute the app?

    Perhaps the API routines would be more reliable?

  7. #7
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Network Drives (Word VBA)

    No, I think you're getting the terminology mixed up. VBScript (official name: "Microsoft

  8. #8
    2 Star Lounger
    Join Date
    Sep 2003
    Location
    Juneau, Alaska, USA
    Posts
    110
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Network Drives (Word VBA)

    Thanks for clarifying the difference. It doesn't sound like I'd be bypassing security policies-our policy is to disable VBScript to block the malware, not to block our capabilities-although it very effectively does that too. I'll check on this scripting library as it's either nothing I've ever heard of before or when I've heard of it in the past I've always confused it with VBScript. Trust Microsoft.

  9. #9
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Network Drives (Word VBA)

    There are a lot of pieces to the puzzle. There are two script execution engines (cscript.exe, wscript.exe) --VBA doesn't require these -- and a few code libraries (e.g., scrrun.dll, vbscript.dll). There are many ways to block scripts from running, so you'll probably have to do some testing to see how it has impacted your environment.

  10. #10
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Network Drives (Word VBA)

    Something like this:

    <pre>Option Explicit

    Private Declare Function GetDriveType Lib "kernel32" Alias _
    "GetDriveTypeA" (ByVal sDrive As String) As Long

    ' Const DRIVE_TYPE_UNDTERMINED = 0
    ' Const DRIVE_ROOT_NOT_EXIST = 1
    ' Const DRIVE_REMOVABLE = 2
    ' Const DRIVE_FIXED = 3
    ' Const DRIVE_REMOTE = 4
    ' Const DRIVE_CDROM = 5
    ' Const DRIVE_RAMDISK = 6

    Sub ShowNetworkedDrives()
    Dim iCounter As Integer
    For iCounter = 65 To 90
    If GetDriveType(Chr(iCounter) & ":") = "4" Then
    msgbox "Drive " & Chr(iCounter) & " is a network drive"
    End If
    Next
    End Sub
    </pre>

    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  11. #11
    2 Star Lounger
    Join Date
    Sep 2003
    Location
    Juneau, Alaska, USA
    Posts
    110
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Network Drives (Word VBA)

    Whew! Thanks-didn't realize there were so many pieces. I'll have to check it out but I believe that all they do here is delete/disable wscript.exe-but it's been a year or two since I was last involved in setting up the PC's. Now I'm more involved in the programming.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •