Give me the current date and time in a format that can be saved

Give me the current date and time in a format that can be saved

jake_egley
Advocate Advocate
510 Views
5 Replies
Message 1 of 6

Give me the current date and time in a format that can be saved

jake_egley
Advocate
Advocate

I want a variable named Time that contains the current date and time in a format that can be saved.

 

Such that I can use a function like this:

 

System.IO.File.Copy("C:\filename.xltx", "C:\filename_" & Time & ".xltx")

 

Thank you

Jake Egley
Inventor 2022
Accepted solutions (1)
511 Views
5 Replies
Replies (5)
Message 2 of 6

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi @jake_egley ,

Welcome to the forum.

I think something like this should work.

 

Hope that helps,

Curtis

 

Dim Time As String = DateTime.Now.ToString("HHmmss")
Logger.Info("Current Time: " & Time)

Dim oDate As String = DateTime.Now.ToString("MMddyyyy")
Logger.Info("Formatted Date: " & oDate)

System.IO.File.Copy("C:\filename.xltx", "C:\filename_" & oDate & "_" & Time & ".xltx")

 

EESignature

Message 3 of 6

jake_egley
Advocate
Advocate

@Curtis_Waguespack That works, thanks. What is the purpose of the Logger lines?

Jake Egley
Inventor 2022
Message 5 of 6

jake_egley
Advocate
Advocate

@Curtis_Waguespack I am now trying to write this into a macro in VBA and it doesn't work, how do you write it in VBA?

Jake Egley
Inventor 2022
Message 6 of 6

Curtis_Waguespack
Consultant
Consultant

@jake_egley 

 

I didn't test/run this... but it would be something like this for VBA

 

 

Dim Time As String
Set Time = DateTime.Now.ToString("HHmmss")

Dim oDate As String
Set oDate = DateTime.Now.ToString("MMddyyyy")

Call System.IO.File.Copy("C:\filename.xltx", "C:\filename_" & oDate & "_" & Time & ".xltx")

 

 

EESignature