Excel's fullname property with OneDrive

后端 未结 12 1999
别那么骄傲
别那么骄傲 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:34

    I found a thread online which contained enough information to put something simple together to solve this. I actually implemented the solution in Ruby, but this is the VBA version:

    Option Explicit
    
    Private Function Local_Workbook_Name(ByRef wb As Workbook) As String
    
      Dim Ctr As Long
      Dim objShell As Object
      Dim UserProfilePath As String
    
      'Check if it looks like a OneDrive location
      If InStr(1, wb.FullName, "https://", vbTextCompare) > 0 Then
    
        'Replace forward slashes with back slashes
        Local_Workbook_Name = Replace(wb.FullName, "/", "\")
    
        'Get environment path using vbscript
        Set objShell = CreateObject("WScript.Shell")
        UserProfilePath = objShell.ExpandEnvironmentStrings("%UserProfile%")
    
          'Trim OneDrive designators
        For Ctr = 1 To 4
           Local_Workbook_Name = Mid(Local_Workbook_Name, InStr(Local_Workbook_Name, "\") + 1)
        Next
    
          'Construct the name
        Local_Workbook_Name = UserProfilePath & "\OneDrive\" & Local_Workbook_Name
    
      Else
    
        Local_Workbook_Name = wb.FullName
    
      End If
    
    End Function
    
    Private Sub testy()
    
      MsgBox ActiveWorkbook.FullName & vbCrLf & Local_Workbook_Name(ActiveWorkbook)
    
    End Sub
    

提交回复
热议问题