Guest User

OneDriveLocalPath

a guest
Dec 20th, 2023
651
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VisualBasic 7.06 KB | Source Code | 0 0
  1. '@Folder "Lib"
  2. Option Explicit
  3. ' Compiled code from : https://stackoverflow.com/questions/65605296/return-excel-vba-macro-onedrive-local-path-possible-lead
  4.  
  5. Private Const ONEDRIVE_TENANTS_REGISTRY_FOLDER As String = "Software\Microsoft\OneDrive\Accounts\Business1\Tenants\"
  6. Private Const ONEDRIVE_TOTAL_VERSIONS As Long = 3
  7. Private Const ONEDRIVE_PATH_SLASHES As Long = 4
  8. Const HKEY_CURRENT_USER = &H80000001
  9.  
  10. Public Function GetLocalWorkbookName(ByVal fullName As String, Optional ByVal PathOnly As Boolean = False) As String
  11.     ' Credits: https://stackoverflow.com/a/57040668/1521579
  12.    'returns local wb path or empty string if local path not found
  13.  
  14.     Dim localFolders As Collection
  15.     Dim localFolder As Variant
  16.    
  17.     Dim evalPath As String
  18.     Dim Result As String
  19.    
  20.     Dim isOneDrivePath As Boolean
  21.    
  22.     'Check if it looks like a OneDrive location
  23.    isOneDrivePath = InStr(1, fullName, "https://", vbTextCompare) > 0
  24.    
  25.     If isOneDrivePath = False Then
  26.         Result = fullName
  27.     Else
  28.         Set localFolders = GetLocalFolders
  29.        
  30.         evalPath = RemoveTopFoldersByQty(fullName, ONEDRIVE_PATH_SLASHES)
  31.         For Each localFolder In localFolders
  32.             Result = GetFilePathByRootFolder(localFolder, evalPath)
  33.             If Result <> vbNullString Then Exit For
  34.         Next localFolder
  35.     End If
  36.     If PathOnly Then
  37.         GetLocalWorkbookName = RemoveFileNameFromPath(Result)
  38.     Else
  39.         GetLocalWorkbookName = Result
  40.     End If
  41.    
  42. End Function
  43. Private Function GetLocalFolders() As Collection
  44.    
  45.     Dim tempCollection As Collection
  46.     Dim tenantFolders As Variant
  47.     Dim localFolders As Variant
  48.    
  49.     Dim tenantCounter As Long
  50.  
  51.     Set tempCollection = New Collection
  52.    
  53.     ' Look in onedrive for business tenant's folders
  54.    tenantFolders = GetRegistrySubKeys(ONEDRIVE_TENANTS_REGISTRY_FOLDER)
  55.    
  56.     For tenantCounter = 0 To UBound(tenantFolders)
  57.         localFolders = GetRegistryValues(ONEDRIVE_TENANTS_REGISTRY_FOLDER & "\" & tenantFolders(tenantCounter) & "\")
  58.         AddArrayItemsToCollection tempCollection, localFolders
  59.     Next tenantCounter
  60.    
  61.     ' Add the onedrive consumer folder
  62.    tempCollection.Add Environ$("OneDriveConsumer")
  63.    
  64.     Set GetLocalFolders = tempCollection
  65.    
  66. End Function
  67. Private Function RemoveTopFolderFromPath(ByVal ShortName As String) As String
  68.     RemoveTopFolderFromPath = Mid$(ShortName, InStr(ShortName, "\") + 1)
  69. End Function
  70.  
  71. Private Function RemoveTopFoldersByQty(ByVal FullPath As String, ByVal FolderQty As Long) As String
  72.     Dim counter As Long
  73.     Dim evalPath As String
  74.     evalPath = Replace(FullPath, "/", "\")
  75.     For counter = 1 To FolderQty
  76.         evalPath = RemoveTopFolderFromPath(evalPath)
  77.     Next counter
  78.     RemoveTopFoldersByQty = evalPath
  79. End Function
  80.  
  81. Private Function RemoveFileNameFromPath(ByVal ShortName As String) As String
  82.     RemoveFileNameFromPath = Mid$(ShortName, 1, Len(ShortName) - InStr(StrReverse(ShortName), "\"))
  83. End Function
  84.  
  85. Private Function GetFilePathByRootFolder(ByVal RootFolder As String, ByVal SearchPath As String) As String
  86.     Dim Result As String
  87.     Dim evalPath As String
  88.     Dim testFilePath As String
  89.     Dim slashCounter As Integer                                                                         'added by AC
  90.    Dim i As Integer                                                                                    'added by AC
  91.    
  92.     Dim oneDrivePathFound As Boolean
  93.        
  94.     evalPath = IIf(InStr(SearchPath, "\") = 0, "\", vbNullString) & SearchPath
  95.    
  96.     slashCounter = 0                                                                                    'added by AC
  97.    Do While evalPath Like "*\*"                                                                        'added by AC
  98.        slashCounter = slashCounter + 1                                                                 'added by AC
  99.        evalPath = RemoveTopFolderFromPath(evalPath)                                                    'added by AC
  100.    Loop                                                                                                'added by AC
  101.    slashCounter = slashCounter + 1
  102.     evalPath = IIf(InStr(SearchPath, "\") = 0, "\", vbNullString) & SearchPath
  103.  
  104.     For i = 1 To slashCounter                                                                           'added by AC
  105.        testFilePath = RootFolder & IIf(Left$(evalPath, 1) <> "\", "\", vbNullString) & evalPath        'added by AC
  106.        'Debug.Print testFilePath                                                                       'added by AC
  107.        If Not (Dir(testFilePath)) = vbNullString Then                                                  'added by AC
  108.            oneDrivePathFound = True                                                                    'added by AC
  109.            Exit For                                                                                    'added by AC
  110.        End If                                                                                          'added by AC
  111.        'remove top folder in path                                                                      'added by AC
  112.        evalPath = RemoveTopFolderFromPath(evalPath)                                                    'added by AC
  113.    Next i                                                                                              'added by AC
  114.    
  115.     If oneDrivePathFound = True Then
  116.         Result = testFilePath
  117.     Else
  118.         Result = vbNullString
  119.        
  120.     End If
  121.    
  122.     GetFilePathByRootFolder = Result
  123.    
  124. End Function
  125.  
  126. Private Function GetRegistrySubKeys(ByVal pathToFolder As String) As Variant
  127. ' Credits: https://stackoverflow.com/a/8667984/1521579
  128.    Dim registryObject As Object
  129.     Dim computerID As String
  130.     Dim subkeys() As Variant
  131.     'Dim key As Variant
  132.  
  133.     computerID = "."
  134.     Set registryObject = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
  135.     computerID & "\root\default:StdRegProv")
  136.  
  137.     registryObject.EnumKey HKEY_CURRENT_USER, pathToFolder, subkeys
  138.     GetRegistrySubKeys = subkeys
  139.     'For Each key In subKeys
  140.    '    Debug.Print key
  141.    'Next
  142. End Function
  143.  
  144. Private Function GetRegistryValues(ByVal pathToFolder As String) As Variant
  145. ' Credits: https://stackoverflow.com/a/8667984/1521579
  146.    Dim registryObject As Object
  147.     Dim computerID As String
  148.     Dim values() As Variant
  149.     Dim valuesTypes() As Variant
  150.     'Dim value As Variant
  151.    
  152.  
  153.     computerID = "."
  154.     Set registryObject = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
  155.     computerID & "\root\default:StdRegProv")
  156.  
  157.     registryObject.EnumValues HKEY_CURRENT_USER, pathToFolder, values, valuesTypes
  158.     GetRegistryValues = values
  159.     'For Each value In values
  160.    '    Debug.Print value
  161.    'Next
  162. End Function
  163.  
  164.  
  165.  
  166. Private Sub AddArrayItemsToCollection(ByVal evalCollection As Collection, ByVal evalArray As Variant)
  167.    
  168.     Dim Item As Variant
  169.    
  170.     For Each Item In evalArray
  171.         evalCollection.Add Item
  172.     Next Item
  173.    
  174. End Sub
  175.  
  176.  
  177.  
Advertisement
Add Comment
Please, Sign In to add comment