首页 > 解决方案 > 如何正确简单地确定 OneDrive 物理路径?

问题描述

这个问题在 Stack Overflow 上出现过很多次,但我找不到既简单又有效的解决方案。这是我的代码:

Public Function GetPhysicalPath() As String
  
  Dim objFso As FileSystemObject
  Set objFso = CreateObject("Scripting.FileSystemObject")
  
  Dim strOneDrivePath As String
  strOneDrivePath = ActivePresentation.Path
  
  Dim strAbsolutePath As String
  strAbsolutePath = objFso.GetAbsolutePathName(strOneDrivePath)
  
  Dim strPhysicalPath As String
  
  If InStr(1, strOneDrivePath, "https://") > 0 Then
    strOneDrivePath = Replace(strOneDrivePath, "https://", "https:\")
    strOneDrivePath = Replace(strOneDrivePath, "/", "\")
  
    strPhysicalPath = Replace(strAbsolutePath, strOneDrivePath, vbNullString)
  Else
    strPhysicalPath = strOneDrivePath & "\"
  End If
  
  GetPhysicalPath = strPhysicalPath
  
  Set objFso = Nothing

End Function

问题是当我打开一个文件时,这个函数有时返回的不是实际的物理路径,而是C:\Users\User1\Documents. 在我执行SaveAs并覆盖实际上在同一个地方的同一个文件之后 - 一切正常。为什么会发生这种情况?

标签: vba

解决方案


推荐阅读