Excel's fullname property with OneDrive

后端 未结 12 2004
别那么骄傲
别那么骄傲 2020-12-24 10:12

If I want to use the open Workbook object to get the fullname of an Excel file after saving it, but that file has been synchronized to OneDrive, I get a \"https\" address in

12条回答
  •  我在风中等你
    2020-12-24 10:45

    Instead of using the variable ThisWorkbook.Path use Environ("OneDrive").

    Option Explicit
    '
    Function TransferURL(wbkURL As String) As String
    ' Converts the URL of a OneDrive into a path.
    ' Returns the path's name.
        
        Dim oFs As Object
        Dim oFl As Object
        Dim oSubFl As Object
     
        Dim pos As Integer
        Dim pathPart As String
        Dim oneDrive As String
        Dim subFl As String
            
        Set oFs = CreateObject("Scripting.FileSystemObject")
            
        ' Check the version of OneDrive.
        If VBA.InStr(1, _
                     VBA.UCase(wbkURL), "MY.SHAREPOINT.COM") = 0 Then
            
            oneDrive = "OneDriveConsumer"
            
        Else
            
            oneDrive = "OneDriveCommercial"
            
        End If
        
        Set oFl = oFs.GetFolder(Environ(oneDrive))
        
        ' Iteration over OneDrive's subfolders.
        For Each oSubFl In oFl.SUBFOLDERS
            
            subFl = "/" & VBA.Mid(oSubFl.Path, _
                                  VBA.Len(Environ(oneDrive)) + 2) & "/"
        
            ' Check if part of the URL.
            If VBA.InStr(1, _
                         wbkURL, subFl) > 0 Then
                    
                ' Determine the path after OneDrive's folder.
                pos = VBA.InStr(1, _
                                wbkURL, subFl)
            
                pathPart = VBA.Mid(VBA.Replace(wbkURL, "/", _
                                               Application.PathSeparator), pos)
            
            End If
        
        Next
        
        TransferURL = Environ(oneDrive) & pathPart
    
    End Function
    

    Call the function by:

    ' Check if path specification as URL.
    If VBA.Left(VBA.UCase(oWbk.Path), _
                5) = "HTTPS" Then
    
        ' Call ...
        pathName = TransferURL(oWbk.Path)
    
    End If
    

    The differentiation between OneDriveConsumer and OneDriveCommercial is derived from:

    https://social.msdn.microsoft.com/Forums/en-US/1331519b-1dd1-4aa0-8f4f-0453e1647f57/how-to-get-physical-path-instead-of-url-onedrive?forum=officegeneral

    Edited by MatChrupczalski Thursday, May 9, 2019 5:45 PM

提交回复
热议问题