[英]Convert Shared folder path to UNC path
I'm trying to convert current shared folder path to unc path by manipulating current path with computer name.我正在尝试通过使用计算机名称操作当前路径来将当前共享文件夹路径转换为 unc 路径。 However result in compile error: expected array on the line"elem = UBound(CurrentPathA)" in Public Function UNCpath().
但是导致编译错误:公共函数 UNCpath() 中的“elem = UBound(CurrentPathA)”行上的预期数组。 Can you guys tell me what seems to be the problem causing this issue?
你们能告诉我似乎是什么问题导致了这个问题吗? and perhaps share better idea to get unc path?
也许分享更好的想法来获得 UNC 路径?
Option Explicit
#If VBA7 Then
Private Declare PtrSafe Function fnGetComputerName Lib "kernel32" Alias "GetComputerNameW" (ByVal lpBuffer As LongPtr, ByRef nSize As Long) As Long
#Else
Private Declare Function fnGetComputerName Lib "kernel32" Alias "GetComputerNameW" (ByVal lpBuffer As Long, ByRef nSize As Long) As Long
#End If
Public Function GetComputerName() As String
Const MAX_COMPUTERNAME_LENGTH As Long = 31
Dim buf As String, buf_len As Long
buf = String$(MAX_COMPUTERNAME_LENGTH + 1, 0)
buf_len = Len(buf)
If (fnGetComputerName(StrPtr(buf), buf_len)) = 0 Then
GetComputerName = "ErrorGettingComputerName"
Else
GetComputerName = Left$(buf, buf_len)
End If
End Function
Public Function UNCpath() As String
Dim CompName As String, CurrentPath As String, CurrentPathA As String
CompName = GetComputerName()
CurrentPath = ThisWorkbook.Path
CurrentPathA = Split(CurrentPath, "\")
elem = UBound(CurrentPathA)
CurrentPath = CurrentPathA(elem)
UNCpath = "\\" & CompName & "\" & CurrentPath
End Function
Or use file system object:或者使用文件系统对象:
Function GetUNCLateBound(strMappedDrive As String) As String
Dim objFso As Object
Set objFso = CreateObject("Scripting.FileSystemObject")
Dim strDrive As String
Dim strShare As String
'Separate 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
'The Replace function allows for sub-folders
'of the mapped drive
GetUNCLateBound = Replace(strMappedDrive, strDrive, strShare)
Set objFso = Nothing 'Destroy the object
End Function
I just found and modified this, to which the credit is due, to be late-bound:我刚刚发现并修改了这个,归功于它,是迟到的:
http://pagecommunication.co.uk/2014/07/vba-to-convert-a-mapped-drive-letter-to-unc-path/ http://pagecommunication.co.uk/2014/07/vba-to-convert-a-mapped-drive-letter-to-unc-path/
I'm surprised that this line doesn't throw a Mismatch error:我很惊讶这一行没有抛出 Mismatch 错误:
CurrentPathA = Split(CurrentPath, "\")
The Split
function converts a string to an array. Split
函数将字符串转换为数组。 So, attempting to assign the result of Split
to a string variable should raise an error.因此,尝试将
Split
的结果分配给字符串变量应该会引发错误。
In any case, the Ubound
function requires an array.在任何情况下,
Ubound
函数都需要一个数组。 Thus, the error when you're doing Ubound(_string_)
instead of UBound(_array_)
.因此,当您执行
Ubound(_string_)
而不是UBound(_array_)
时会出现错误。
Dim CurrentPathA As Variant
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.