使用 Access VBA 将文件保存到 OneDrive [英] Save files to OneDrive using Access VBA
问题描述
我们的系统正在转换为云存储,我们的个人驱动器很快就会消失,因此我需要将保存应用程序保存到用户 OneDrives.
Our systems are changing over to cloud storage to our personal drives will be vanishing soon, therefore I need to save save applications to users OneDrives.
这对我来说是一个新领域,我在网上阅读的内容没有多大意义.
This is new territory for me and what I am reading online is not making much sense.
这是我目前所拥有的:
Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
Dim FileExistsbol As Boolean
Dim stFileName As String
Dim CopyFrom As String
Dim CopyTo As String
stFileName = "C:\Users\" & Environ("UserName") & "\OneDrive - Company"
stFileName = Trim(stFileName)
FileExistsbol = dir(stFileName) <> vbNullString
If FileExistsbol Then
Kill stFileName
CopyFrom = "C:\Test File.txt"
CopyTo = "C:\Users\" & Environ("UserName") & "\OneDrive - Company"
FileSystemObject.CopyFile CopyFrom, CopyTo
Else
CopyFrom = "C:\Test File.txt"
CopyTo = "C:\Users\" & Environ("UserName") & "\OneDrive - Company"
FileSystemObject.CopyFile CopyFrom, CopyTo
End If
此代码以前用于复制到本地驱动器,但相信它在这种情况下不起作用,因为它尝试复制到的新驱动器是基于网络的.
This code has previously worked for copying to local drives, but believe its not working on this occasion because the new drives its trying to copy to is web based.
错误:实际上没有填充错误
感谢任何帮助
推荐答案
注意你的变量 stFileName
包含目录路径而不是文件名,所以 kill
表达式是以目录为目标.
Note that your variable stFileName
contains the directory path not a filename, and so the kill
expression is targeting the directory.
然而,不是测试和删除文件,而是为 CopyFile
方法使用 overwrite
参数怎么样,例如:
However, instead of testing for and deleting the file, how about using the overwrite
argument for the CopyFile
method, e.g.:
Dim objFSO As New FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\Test File.txt", "C:\Users\" & Environ("UserName") & "\OneDrive - Company", True
Set objFSO = Nothing
您需要在项目中添加对 Microsoft Scripting Runtime 的引用,以启用我示例中的 objFSO
变量,以便使用早期绑定进行定义.
You'll need to add a reference to Microsoft Scripting Runtime in your project to enable the objFSO
variable in my example to be defined using early binding.
这篇关于使用 Access VBA 将文件保存到 OneDrive的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!