Excel's fullname property with OneDrive

后端 未结 12 1966
别那么骄傲
别那么骄傲 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条回答
  •  梦毁少年i
    2020-12-24 10:28

    Here's a small improvement on Philip Swannell's improvement of Virtuoso's original answer for when the number of "\" to remove from the path is more than 4 / varies (depending on the file, i found i needed to remove 5 or sometimes 6 of these). The shortcomings mentioned by Philip are still there though.

    Private Function Local_Workbook_Name(ByRef wb As Workbook) As String
    'returns local wb path or nothing if local path not found
        Dim i As Long
        Dim OneDrivePath As String
        Dim ShortName As String
        Dim testWbkPath As String
        Dim OneDrivePathFound As Boolean
    
        'Check if it looks like a OneDrive location
        If InStr(1, wb.FullName, "https://", vbTextCompare) > 0 Then
            'Replace forward slashes with back slashes
            ShortName = Replace(wb.FullName, "/", "\")
    
            'Remove the first four backslashes
            For i = 1 To 4
                ShortName = RemoveTopFolderFromPath(ShortName)
            Next
    
            'loop through three OneDrive options
            For i = 1 To 3
                OneDrivePath = Environ(Choose(i, "OneDrive", "OneDriveCommercial", "OneDriveConsumer"))
                If Len(OneDrivePath) > 0 Then
                    'Loop to see if the tentative LocalWorkbookName is the name of a file that actually exists, if so return the name
                    Do While ShortName Like "*\*"
                        testWbkPath = OneDrivePath & "\" & ShortName
                        If Not (Dir(testWbkPath)) = vbNullString Then
                            OneDrivePathFound = True
                            Exit Do
                        End If
                        'remove top folder in path
                        ShortName = RemoveTopFolderFromPath(ShortName)
                    Loop
                End If
                If OneDrivePathFound Then Exit For
            Next i
        Else
            Local_Workbook_Name = wb.FullName
        End If
    
        If OneDrivePathFound Then Local_Workbook_Name = testWbkPath
    
    End Function
    Function RemoveTopFolderFromPath(ByVal ShortName As String) As String
        RemoveTopFolderFromPath = Mid(ShortName, InStr(ShortName, "\") + 1)
    End Function
    

提交回复
热议问题