Trap Drive Offline Condition With VBA To Create A Directory On That Drive

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,650
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
How can I best adapt the following code to trap when the destination drive is unavailable? Sometimes, this drive (O:\) goes offline so if this code is run, an error is triggered.

Code:
Public Sub open_folder(txt_model As String)
    Dim modelini As String, fldini As String
    Dim fldMaster As String, fldPath As String
    Dim ffound ' As String
    Dim response

    fldMaster = "O:\IFM\"
    If Dir(fldMaster, vbDirectory) = "" Then
        MkDir fldMaster
    End If
    
    If Right(txt_model, 1) = "." Then txt_model = Left(txt_model, (Len(txt_model) - 1))
    modelini = Left(txt_model, 1)
    fldini = fldMaster & modelini & "\"
    If Dir(fldini, vbDirectory) = "" Then
        MkDir fldini
    End If
    fldPath = fldini & txt_model & "\"
    If Dir(fldPath, vbDirectory) = "" Then
        MkDir fldPath
        MsgBox "Folder for " & txt_model & " created." & Chr(13) & fldPath, vbInformation, "SUCCESS"
    End If
    Shell "explorer.exe /root," & fldPath, vbNormalFocus
End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Just test for O: ?

VBA Code:
If dir("O:") = "" Then 'off line, so do something else
Note, no backslash, else you'll likely return an empty string even if connected.
 
Upvote 0
Solution
Ah! Didn't think it would be that easy, but makes sense now!
Thank you!
 
Upvote 0

Forum statistics

Threads
1,225,738
Messages
6,186,728
Members
453,368
Latest member
positivemind

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top