So before saving to the current XML file that my VBA code is working on, I want to create a timestamp based backup of the XML file in VBA. This is how I plan to do it:
Sub CreateBackup() Dim objFSO As FileSystemObject Set objFSO = CreateObject("Scripting.FileSystemObject") oldFilePath = ThisWorkbook.Path & "\DEMO.xml" newFilePath = ThisWorkbook.Path & "\DEMO" & Format(DateTime.Now, "yyyy-MM-dd hh:mm:ss") & ".xml.bak" objFSO.CopyFile oldFilePath, newFilePath End Sub
However, everytime I get a
Bad Filename or Number error. I have double checked (using the debugger) that my variables are reading the correct filepath with filename. What gives?
: is not allowed in file names.
Format(DateTime.Now, "yyyy-MM-dd hh:mm:ss")
Format$(DateTime.Now, "yyyy-MM-dd hh_mm_ss")
Note that using
Format$() returns a string instead of a variant (using