【发布时间】:2014-03-06 22:53:57
【问题描述】:
我维护的一个 Excel 工作簿中有这个 VBA 代码:
Function GetDrivePath(DriveLetter As String) As String
Dim DrivePath As String
Dim DriveLen As Long
'DrivePath = Space(260)
DriveLen = Len(DrivePath)
If WNetGetConnection(DriveLetter, DrivePath, DriveLen) = error_success Then
GetDrivePath = DrivePath
Else
GetDrivePath = DriveLetter
End If
End Function
直到最近这段代码运行良好,但现在它失败了。即使在它工作的时间段的 excel 工作表的存档版本中,它也不再工作。
如果在excel工作簿中使用这种格式的路径,则不调用:
\\servername\filepath
但是当路径有驱动器号时调用它:
X:\filepath
现在无论何时调用它都会失败。以前从来没有这个问题。
代码应该返回驱动器号所指的服务器路径。
它返回的错误不是特别有用;这是一个简单的类型不匹配错误。
典型输入:
X:
其中“X”是驱动器号。
典型输出:
\\servername\path-to-folder-that-X-is-mapped-to
声明声明:
Declare Function WNetGetConnection Lib "mpr.dll" Alias "WNetGetConnectionA" (ByVal lpszLocalName As String, ByVal lpszRemoteName As String, cbRemoteName As Long) As Long
【问题讨论】:
-
WNetGetConnection是做什么的?你能把这段代码也给我们看看吗? -
@aphoria 这是微软的一个功能。
-
这里是Microsoft's info 。
-
您能否详细说明您提供的输入以及您期望的输出与您获得的输出?
-
向我们展示定义此对 VBA 的外部库调用的
Declare Function .. Lib语句。