我有一个Excel工作簿,该工作簿本地存储在我的PC上,但位于我的 OneDrive 同步文件夹中。每当我尝试时(从立即窗口以及通过编程方式):
? Excel.Application.ThisWorkbook.FullName
我得到类似的东西
https://d.docs.live.net/c31ceb5b47a36fa2/VBA/learnVBAmacros.xlsb
我文件的实际本地路径为:
C:\ Users \ viset \ OneDrive \ VBA \ learnVBAmacros.xlsb
如何获取工作簿的后一个 LOCAL 路径,而不是OneDrive上的URL?
答案 0 :(得分: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环境变量。我自己是一样的,但每个人都必须有理由。
答案 1 :(得分: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
答案 2 :(得分:0)
我遇到了同样的问题,并且很容易解决。
如果文件要在OneDrive上等待同步,则将使用ThisWorkBook.Path
作为URL地址。
除非您的文件同步ThisWorkBook.Path
的值,否则将包含文件的本地地址。
答案 3 :(得分:0)
结合一些较早的答案,我使用了此功能。它对OneDrive URL的格式的假设更少,并且使用环境而不是注册表。
注意:它确实仍然对OneDrive 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
答案 4 :(得分: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)