Save files to OneDrive using Access VBA

余生长醉 提交于 2020-07-24 05:44:47

问题


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.

This is what I have so far:

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.

Error: No error is actually being populated

Any help is appreciated


回答1:


Note that your variable stFileName contains the directory path not a filename, and so the kill expression is targeting the directory.


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

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.



来源:https://stackoverflow.com/questions/48183546/save-files-to-onedrive-using-access-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!