Public Sub BackUpOneDrive()
'Create back up file on onedrive
Dim DestFile As String, FSO As Object, CurrentDate As String
Dim SrcFile As String, FolderName As String
'****change file name and foldrpath to suit
SrcFile = "YourFileName" '***File name with NO file extension ie. NO ".xlsm"
FolderName = "\Documents\YourFoldername\" 'one drive folder location
On Error GoTo erfix
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'save copy of file as temp file
ActiveWorkbook.SaveCopyAs Environ$("temp") & "" & SrcFile
'make back up path
CurrentDate = Format(Now, "dd.mm.yy hh.mm AM/PM")
DestFile = (Environ("onedrive") & FolderName & SrcFile & "_" & CurrentDate & ".xlsm")
Set FSO = CreateObject("Scripting.FileSystemObject")
'copy temp file to back up path
FSO.CopyFile Environ$("temp") & "" & SrcFile, DestFile, True 'source,destination,save
'remove temp file
Kill Environ$("temp") & "" & SrcFile
erfix:
If Err.Number <> 0 Then
On Error GoTo 0
MsgBox "Backup Error"
End If
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Set FSO = Nothing
End Sub
Private Sub Workbook_Open()
Call BackUpOneDrive
End Sub
Public Sub BackUpOneDrive()
'Create back up file on onedrive
Dim DestFile As String, FSO As Object, CurrentDate As String, Splitter As Variant
Dim SrcFile As String, FolderName As String, FileNm As String
SrcFile = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 5)
If InStr(SrcFile, "_") Then
Splitter = Split(ActiveWorkbook.Name, "_")
FileNm = Splitter(0)
Else
FileNm = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 5)
End If
'****change folderpath to suit
FolderName = "\Documents\YourFoldername\" 'one drive folder location
On Error GoTo erfix
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'save copy of file as temp file
ActiveWorkbook.SaveCopyAs Environ$("temp") & "" & SrcFile
CurrentDate = Format(Now, "dd.mm.yy hh.mm AM/PM")
'make back up path
DestFile = (Environ("onedrive") & FolderName & FileNm & "_" & CurrentDate & ".xlsm")
Set FSO = CreateObject("Scripting.FileSystemObject")
'copy temp file to back up path
FSO.CopyFile Environ$("temp") & "" & SrcFile, DestFile, True 'source,destination,save
'remove temp file
Kill Environ$("temp") & "" & SrcFile
erfix:
If Err.Number <> 0 Then
On Error GoTo 0
MsgBox "Error"
End If
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Set FSO = Nothing
End Sub