运行时间错误5

Function GetUNC(strMappedDrive As String) As String Dim objFso As FileSystemObject Set objFso = New FileSystemObject Dim strDrive As String Dim strShare As String 'Separated the mapped letter from 'any following sub-folders strDrive = objFso.GetDriveName(strMappedDrive) 'find the UNC share name from the mapped letter strShare = objFso.Drives(strDrive).ShareName '<<<< this is the line that the code fails on 'The Replace function allows for sub-folders 'of the mapped drive GetUNC = Replace(strMappedDrive, strDrive, strShare) Set objFso = Nothing 'Destroy the object End Function 

它可以在我的笔记本电脑和networking上正常工作,但是当同事在笔记本电脑和networking上使用相同的电子表格时,代码会在下面一行中引发运行时错误5exception“无效的过程调用或参数”:

 strShare = objFso.Drives(strDrive).ShareName 

当我hover在代码行上时,我看到:当我运行代码到这一点时,我看到一个文件path。

我的同事尝试在本地驱动器上运行代码以及没有成功的networking驱动器。 我们都select了相同的参考。 有谁知道我需要做什么才能让我的同事机器上工作?

不完全确定问题是什么,但可能值得使用API​​调用:

 #If Win64 Then Declare PtrSafe Function WNetGetConnection32 Lib "MPR.DLL" Alias "WNetGetConnectionA" (ByVal lpszLocalName As String, ByVal lpszRemoteName As String, lSize As Long) As Long #Else Declare Function WNetGetConnection32 Lib "MPR.DLL" Alias "WNetGetConnectionA" (ByVal lpszLocalName As String, ByVal lpszRemoteName As String, lSize As Long) As Long #End If Dim lpszRemoteName As String * lBUFFER_SIZE Dim lSize As Long Const NO_ERROR As Long = 0& Const lBUFFER_SIZE As Long = 255& Function GetUNC(ByRef strDriveLetter As String) As String strDriveLetter = UCase$(strDriveLetter) & ":" GetUNC = IIf(WNetGetConnection32(strDriveLetter, lpszRemoteName, lBUFFER_SIZE) = NO_ERROR, lpszRemoteName, "Error") End Function 

然后简单地使用像这样的:

 MsgBox GetUNC("S")