Excel's fullname property with OneDrive

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

    I have the same problem as you. But I have solved that problem. The first I turn off OneDrive before I running the script.

    you can add this script on the first script into your vba/module:

    Call Shell("cmd.exe /S /C" & "%LOCALAPPDATA%\Microsoft\OneDrive\OneDrive.exe /shutdown")
    

    and then, on your last script on your vba/module you can insert this for activate your OneDrive:

    Call Shell("cmd.exe /S /C" & "start %LOCALAPPDATA%\Microsoft\OneDrive\OneDrive.exe /background")
    

    I am using Windows10 on that script.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-24 10:31

    Very helpful, thanks. I had a similar issue, but with a folder name rather than a filename. Consequently I modified it slightly. I made it work for folder names AND filenames (doesn't have to be a workbook). In case it's helpful, code is below:

    Public Function Local_Name(theName As String) As String
        Dim i               As Integer
        Dim objShell        As Object
        Dim UserProfilePath As String
    
        ' Check if it looks like a OneDrive location.
        If InStr(1, theName, "https://", vbTextCompare) > 0 Then
    
            ' Replace forward slashes with back slashes.
            Local_Name = Replace(theName, "/", "\")
    
            'Get environment path using vbscript.
            Set objShell = CreateObject("WScript.Shell")
            UserProfilePath = objShell.ExpandEnvironmentStrings("%UserProfile%")
    
            ' Trim OneDrive designators.
            For i = 1 To 4
                Local_Name = Mid(Local_Name, InStr(Local_Name, "\") + 1)
            Next i
    
            ' Construct the name.
            Local_Name = UserProfilePath & "\OneDrive\" & Local_Name
        Else
            ' (must already be local).
            Local_Name = theName
        End If
    End Function
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-24 10:34

    This is really great stuff. I have run into this problem on some windows 10 machines but not others and it seems to come and go. I tried everything resetting OneDrive, changing the configuration etc. The only thing I tried that at least works on my machine is to use Fullname=CurDir & FileName, instead of FullName= activeworkbook.Path & FileName.

    This returned the full local name without the https stuff and I was able to open my file ok.

    0 讨论(0)
  • 2020-12-24 10:35

    Easy Fix (early 2019) - For anyone else having this issue:

    OneDrive > Settings > Office: - Uncheck 'Use Office applications to sync Office files that I open'

    This lets excel save the file in the typical "C:\Users[UserName]\OneDrive..." file format instead of the UNC "https:\" format.

    0 讨论(0)
提交回复
热议问题