如何获取存储在本地OneDrive文件夹中的Excel工作簿的路径+文件名?(而不是其URL!)

4

我有一个Excel工作簿,保存在我的个人电脑的本地文件夹中,但在我的同步OneDrive文件夹内。每当我尝试使用立即窗口或编程方式来执行以下操作时:

? Excel.Application.ThisWorkbook.FullName

我得到的信息类似于:
https://d.docs.live.net/c31ceb5b47a36fa2/VBA/learnVBAmacros.xlsb
而我的文件的真正本地路径为:
C:\Users\viset\OneDrive\VBA\learnVBAmacros.xlsb
我怎样才能获取后者的本地路径而不是它在OneDrive上的URL?

你可以在即时窗口中检查 ?environ(17)。 - skkakkar
1
这个问题的解决方案可以在这里找到。 - GWD
7个回答

1
只需前往您的OneDrive设置,取消选中“使用Office应用程序同步……”即可。

在此输入图片描述


虽然该链接可能回答了问题,但最好在此处包含答案的关键部分并提供参考链接。仅有链接的答案如果链接页面更改则可能变得无效。- 【来自审查】 - Martin
谢谢,如果不需要协作,这是一个很好的解决方案。 - WeAreOne

0

我使用CurDir解决了这个问题。

Function GetFilePath() As String
    GetFilePath = CurDir
End Function

CurDir 不一定返回当前工作簿的路径。有关更多信息,请参见此处。有关此问题的解决方案,请参见此处 - GWD

0

结合之前的几个答案,我选择了这个函数。它对 OneDrive URL 的格式做出了较少的假设,并且使用环境而不是注册表。

注意:它仍然对 OneDrive URL 做出了一些假设。具体来说:

  • 假定以 "https:" 开头
  • 假定主机名后只有一个 URL 路径组件
  • 假定接下来的内容将与本地文件系统匹配
Function GetWorkbookDirectory() As String

    Dim sPath As String
    Dim sOneDrive As String
    Dim iPos As Integer
    
    sPath = Application.ActiveWorkbook.Path
    
    ' Is this a OneDrive path?
    If Left(sPath, 6) = "https:" Then
        ' Find the start of the "local part" of the name
        iPos = InStr(sPath, "//")           ' Find start of URL hostname
        iPos = InStr(iPos + 2, sPath, "/")  ' Find end of URL hostname
        iPos = InStr(iPos + 1, sPath, "/")  ' Find start of local part
        
        ' Join that with the local location for OneDrive files
        sPath = Environ("OneDrive") & Mid(sPath, iPos)
        sPath = Replace(sPath, "/", Application.PathSeparator)
    End If
    
    GetWorkbookDirectory = sPath

End Function

0

我曾经遇到过同样的问题,解决方案非常简单。

如果你的文件在OneDrive上挂起同步,那么ThisWorkBook.Path的值将是一个URL地址。

除非你的文件同步完成,否则ThisWorkBook.Path的值将包含文件的本地地址。

here you have the results


0

这个可以用,但是抱歉我不记得在哪里找到的了

Sub GetDocLocalPath_Test()

  MsgBox GetDocLocalPath(ThisWorkbook.FullName)

End Sub

Function GetDocLocalPath(docPath As String) As String
'Gel Local Path NOT URL to Onedrive
Const strcOneDrivePart As String = "https://d.docs.live.net/"
Dim strRetVal As String, bytSlashPos As Byte

  strRetVal = docPath & "\"
  If Left(LCase(docPath), Len(strcOneDrivePart)) = strcOneDrivePart Then 'yep, it's the OneDrive path
    'locate and remove the "remote part"
    bytSlashPos = InStr(Len(strcOneDrivePart) + 1, strRetVal, "/")
    strRetVal = Mid(docPath, bytSlashPos)
    'read the "local part" from the registry and concatenate
    strRetVal = RegKeyRead("HKEY_CURRENT_USER\Environment\OneDrive") & strRetVal
    strRetVal = Replace(strRetVal, "/", "\") 'slashes in the right direction
    strRetVal = Replace(strRetVal, "%20", " ") 'a space is a space once more
End If
GetDocLocalPath = strRetVal

End Function

Function RegKeyRead(i_RegKey As String) As String
Dim myWS As Object

  On Error Resume Next
  'access Windows scripting
  Set myWS = CreateObject("WScript.Shell")
  'read key from registry
  RegKeyRead = myWS.RegRead(i_RegKey)
End Function

你可能是从这里得到了这个解决方案。该解决方案最初由Cooz2编写,并由LucasHol适用于VBA。 - GWD

0
将文件名拆分并使用环境变量来设置本地的OneDrive文件夹。
dim fn as string

fn = ThisWorkbook.FullName
'split off the workbook name if fullname is a url
fn = split(fn, "/")(ubound(split(fn, "/")))
'split off the workbook name if fullname is a local path
fn = Split(fn, "\")(UBound(Split(fn, "\")))
'add the environment var
fn = environ("onedrive") & "\" & fn

'check to see if it exists
if len(dir(fn)) > 0 then
    debug.print fn
end if

请注意,存在OneDrive和OneDriveConsumer环境变量。我的环境变量相同,但每个变量都有其原因。

请检查您的代码,它给我输出了以下内容 C:\Users\kakka\OneDrive\C:\Users\kakka\Documents\environ.xlsm - skkakkar
这取决于ThisWorkbook.FullName是OneDrive URL还是本地路径。OP已经说明ThisWorkbook.FullName返回的是OneDrive URL,我提供了针对这种情况的转换方法。当然,可以轻松生成一个简单的检查。 - user11060139

0

我使用了FileSystemObject解决了这个问题。

Dim fso as FileSystemObject, localPath as String, localFullFileName as String
localPath = fso.GetParentFolderName(fso.GetAbsolutePathName(Application.ActiveWorkbook.Name))
localFullFileName = fso.GetAbsolutePathName(Application.ActiveWorkbook.Name)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接