Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have some code that runs on workbook open that uses a form to request that the user select the drive to which a shared directory is mapped.

This is because the workbook uses VBA code to retrieve and save data to a shared workbook located in this shared directory, but the local drive changes by user, so they need to select it.

The problem I've run into occurs when the user has mapped multiple shared directories to their computer and thus have multiple drives... ex: 1 directory is on drive G: and the other is on X:.

If they select the drive for the shared directory in which the workbook resides, there is no problem. However, if they accidentally choose the drive for the other shared directory, the code hangs.

I have a loop setup that checks to see they've chosen the correct drive... IE: If they chose A: (a non-existent drive in my example), then the code will note that they chose the incorrect drive and prompt them again.

However, instead of creating an error when another shared directory is chosen, the code just hangs.

In the below code, cell AD3 on sheet one contains true or false (gets set to false in the beginning of the sub). It gets set to true if they've chosen correct drive as Module6.PipelineRefresh will no longer cause an error (this sub attempts to open the workbook in the shared drive... and if the chosen drive is incorrect it obviously returns an error)

Codes is as below:

Do While Sheet1.Range("ad3") = False
    On Error Resume Next
        Call Module6.PipelineRefresh  '~~ I'm guessing the code hangs here.  Instead of returning an error immediately, as it would if they simply chose a non-existant drive, it appears to get stuck trying to open the workbook, even though it's not located in the shared directory they've selected.
    If Err.Number = 0 Then
        Sheet1.Range("ad3") = True
        Err.Clear
    Else
        MsgBox "Invalid Network Drive."
        DriverSelectForm.Show
        Err.Clear
    End If
Loop

If anyone knows how to implement a timer so I can shutdown the code after some amount of time, that'd be great.

Alternatively, if you know how to get around this error, that'd also be great!

EDIT as per comment:

This is the specific code in Module6.PipelineRefresh that hangs. The DriverSelectForm (shown above) amends the value in cell o1 to the chosen drive string (ie: X:)

Dim xlo As New Excel.Application
Dim xlw As New Excel.Workbook
Dim xlz As String
xlz = Sheet1.Range("o1").Value & "\Region Planning\Created Pipeline.xlsx"
Dim WS As Worksheet
Dim PT As PivotTable

Application.DisplayAlerts = False
Set xlw = xlo.Workbooks.Open(xlz)
Application.DisplayAlerts = True

Note: As stated above, if the user selects a non-existent directory, the above code returns an error immediately because it cannot open the file... if they have a shared directory mapped to the chosen drive (but it's the wrong directory), the code will hang and does not appear to return an error.

share|improve this question
    
What code actually hangs when the wrong drive is selected? Aside from not having the expected directory are the other drives regular network shares? or are they mapped over a vpn, do they require a logon? –  Alex K. May 13 '13 at 10:36
    
Is DriverSelectForm opening the standard file dialog or is it some custom navigation? –  Skip Intro May 13 '13 at 12:25
    
Will answer both in one comment: The shared directory drives are mapped over a vpn (and require a login in that sense). I will edit the question to include the specific code that hangs in a moment. DriverSelectForm is a custom navigation that just allows them to select a letter (ex: 'X:') which then gets added onto the filepath used to open the workbook. –  Sam WB May 13 '13 at 12:46
    
I should also note, that when I select the wrong shared directory, task manager shows that a new instance of excel is opened and perhaps this is why it's hanging and no error is returned? Also, thinking on this, I'm wondering if I can just write some code to search for the name of the shared directory (the name will also be the same, it's just that the drive to which the directory is mapped will change by user). When I force close the new workbook instance (in task manager), the code continues and returns 'Invalid Network Drive' as it should... –  Sam WB May 13 '13 at 13:15

1 Answer 1

up vote 1 down vote accepted

I've answered my own question by working around the problem. Instead of checking that the user has selected the correct drive letter, I am now using the CreatObject function to find the drive letter associated with the drive name (as drive name will not change).

Example code for this:

Dim objDrv      As Object
Dim DriveLtr      As String

For Each objDrv In CreateObject("Scripting.FileSystemObject").Drives
    If objDrv.ShareName = "Shared Drive Name" Then
        DriveLtr = objDrv.DriveLetter
    End If
Next

If Not DriveLtr = "" Then
    MsgBox DriveLtr & ":"
Else
    MsgBox "Not Found"
End If
Set objDrv = Nothing
share|improve this answer
    
I would also be tempted to ucase$() them both while comparing –  Alex K. May 13 '13 at 15:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.