mercredi 25 février 2015

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. However result in compile error: expected array on the line"elem = UBound(CurrentPathA)" in Public Function UNCpath(). Can you guys tell me what seems to be the problem causing this issue? and perhaps share better idea to get unc path?



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

Aucun commentaire:

Enregistrer un commentaire