【发布时间】:2021-09-13 22:13:25
【问题描述】:
我在 MS ACCESS 中遇到以下问题:
使用 VBA,PDF 文件存储在 OneDrive 文件夹中并与 Sharepoint 同步。众所周知,OneDrive 在所有计算机上都以相同的方式声明为另一种驱动器。
保存后,文件位于computer01文件夹中:
C:\Users\User01\OneDrive - AllFolders\SharedFolder\AllDocs\Doc01.pdf =>( \USER01 )
现在,computer02 上的 user02 也连接到该文件夹,他想通过 VBA 打开文件。相应的共享表字段中文件的路径为:
C:\Users\User01\OneDrive - AllFolders\SharedFolder\AllDocs\Doc01.pdf
当然User02不能通过VBA打开文件,因为路径应该是:
C:\Users\User02\OneDrive - AllFolders\SharedFolder\AllDocs\Doc01.pdf =>( \USER02 )
例如我尝试使用这样的路径来保存文件(这不是正确的语法!):
(Environ("USERPROFILE")) & "\OneDrive - AllFolders\SharedFolder\AllDocs\Doc01.pdf
但是 (Environ("USERPROFILE")) as table.field.value 当然理解为字符串而不是变量!
如何解决这个问题?谢谢!
我的代码:
Option Compare Database
Option Explicit
Public OneDriveDirectory As String
Public Function SetPathToOneDrive() As String
SetPathToOneDrive = (Environ("USERPROFILE")) & "\OneDrive - AllFolders\SharedFolder\AllDocs\"
End Function
Sub SaveDoc()
Dim xSource As String, xDestination As String, xFilename As String
Dim FSO As Object
Dim db As DAO.Database
Dim rs As DAO.Recordset
OneDriveDirectory = SetPathToOneDrive
xFilename = "Doc01.pdf"
xSource = (Environ("USERPROFILE")) & "\" & xFilename
xDestination = OneDriveDirectory & xFilename
Set db = CurrentDb
Set rs = db.OpenRecordset("tblDocLinks", dbOpenDynaset)
Set FSO = VBA.CreateObject("Scripting.FileSystemObject")
Call FSO.CopyFile(xSource, xDestination)
With rs
.AddNew
.Fields("fldDocPath") = xDestination
.Update
End With
rs.close
db.close
End Sub
【问题讨论】:
-
也许可以试试
%UserProfile%\OneDrive - All Folders\SharedFolder\AllDocs\Doc01.pdf见superuser.com/questions/217504/… -
我收到错误 SetPathToOneDrive = %UserProfile% & "OneDrive - All Folders\SharedFolder\AllDocs" 和 SetPathToOneDrive = %UserProfile%\OneDrive - All Folders \SharedFolder\AllDocs" (RED VBA LINE)
标签: vba ms-access hyperlink path onedrive