ThisWorkbook.FullName returns a URL after syncing with OneDrive. I want the file path on disk

前端 未结 3 1324
清酒与你
清酒与你 2021-01-13 16:58

I have a workbook on OneDrive. Usually, ThisWorkbook.FullName returns a path on disk:

c:\\Users\\MyName\\OneDrive - MyCompany\\BlaBla\\MyWorkbook 09-21-17.xl         


        
相关标签:
3条回答
  • 2021-01-13 17:08

    The only thing I can think of, is to cache the localPath when you know you have it (before you start saving & synchronizing), and then "rebuild" the path using the cached localPath and the workbook's Name:

    Public Sub Test()
        Dim localPath As String
        With New FileSystemObject
    
            With .GetFolder(ActiveWorkbook.Path)
                localPath = .Path
            End With
    
            'SaveAs/synchronize...
    
            Debug.Print .BuildPath(localPath, ActiveWorkbook.Name)
    
        End With
    End Sub
    
    0 讨论(0)
  • 2021-01-13 17:09

    I used Windows a environment variable to solve this problem.

    In my example I was using a private OneDrive, but it is fairly simple to change the code to handle OneDrive for Business. The environment variable would then be "OneDriveCommercial" instead of "OneDriveConsumer".

    This is my code for converting the OneDrive URL into a local path:

    Rem consumer URL to OneDrive root: "https://d.docs.live.net/<64-bit hex value>/"
    OneDriveServerURL = "https://d.docs.live.net/"
    
    path = ActiveWorkbook.path
    Worksheets("Menu").Range("G6").Value = path
    
    If Left(path, Len(OneDriveServerURL)) = OneDriveServerURL Then
      Rem remove from start to first "/" after server URL
      path = Mid(path, InStr(Len(OneDriveServerURL) + 1, path, "/"))
    
      Rem replce "/" by "\"
      path = Replace(path, "/", Application.PathSeparator)
    
      Rem add OneDrive root folder from environment variable
      path = Environ("OneDriveConsumer") + path
    End If
    
    0 讨论(0)
  • 2021-01-13 17:15
    Sub get_folder_path()
    
    'early binding
    Dim fso As FileSystemObject
    Set fso = New FileSystemObject
    
    'late binding
    'Dim fso As Object
    'Set fso = CreateObject("Scripting.FileSystemObject")
    
    Dim folder As String
    folder = fso.GetAbsolutePathName(ThisWorkbook.Name)
    Debug.Print (folder)
    
    0 讨论(0)
提交回复
热议问题