Workbooks.open在特定的USB驱动器上

时间:2017-03-30 08:44:22

标签: excel vba excel-vba

我想知道如何在打开它之前测试它。 例如,如果USB在E:或F上: 像这样:

if (?DriveExist?) 
   Workbooks.Open Filename:="E:"
else 
   Workbooks.Open Filename:="F:"

感谢您的帮助

1 个答案:

答案 0 :(得分:6)

这样就可以了:

Function driveexists(DriveName As String)
    Dim objDrv      As Object
    driveexists = False

    For Each objDrv In CreateObject("Scripting.FileSystemObject").Drives
       If DriveName = objDrv.DriveLetter Then driveexists = True
    Next

    Set objDrv = Nothing
End Function

所以你将它用作

IF driveexists("F") then...