Solved insert date and hour in macro code for word 2010

April 8, 2013 at 08:40:44
Specs: Windows 7
Hello, i am writing for the first time on this forum and i appologise for my english.
. I'm glad that i have found a part of the solution for saving one word file to difrent locations with these code

Sub SaveX2()
Dim strFN As String
Dim strFX As String
Dim strL1 As String
Dim strL2 As String

'setup two locations - ending with /
strL1 = "H:/"
strL2 = "C:/Temp/"

'get filename from user
strFN = InputBox("Enter file name without extension", "Save to Stick and HDD")
'check for no file extension
If InStr(1, strFX, ".") > 0 Then
strFX = Left(strFX, InStr(1, strFX, "."))
End If
'get existing filename and extract file extension
strFX = ActiveDocument.Name
'test for file extension on current document
If InStr(1, strFX, ".") = 0 Then
'none so use a default
strFX = ".doc"
'use existing
strFX = Right(strFX, Len(strFX) - InStr(1, strFX, ".") + 1)
End If

'create the filename
strFN = strFN & strFX

'save to stick first
ActiveDocument.SaveAs FileName:=strL1 & strFN
'save to HDD second
ActiveDocument.SaveAs FileName:=strL2 & strFN
End Sub

Now my question is can i insert the date and hour automaticly as file name in stade of typing the name of file in the input box ?

please can you help me ? Thank you.

See More: insert date and hour in macro code for word 2010

Report •

April 8, 2013 at 10:04:16
First, a posting tip:

Please click on the blue line at the end of this post and read the instructions on how post VBA code in this forum. Using the method described in that How To will make viewing the code easier for all.

As for your question, you cannot use a "/" or a ":" in a filename so please tell us what you want the filename to look like.


4/8/2013 12:45:23 PM is not valid.

4-8-2013 12_45_23 PM is.

How would you like the filename formatted?

Click Here Before Posting Data or VBA Code ---> How To Post Data or Code.

Report •

April 8, 2013 at 11:41:37
Thank you for your quick response. I would like to have the name of the file like this: filename plus hour and date. it doesn't metter how it looks like , your second exemple is perfect as long as i can quickly see the time in the list. and that i do not have to write it down when i save the file.
Thanks allot.

Report •

April 8, 2013 at 13:32:12
✔ Best Answer
You can build the Filename with the following syntax:

'create the filename
'Extract Date e.g. 4-8-2013
      strDate = Month(Date) & "-" & Day(Date) & "-" & Year(Date) & " "
'Extract Time e.g. 16_32_23
      strTime = Hour(Time) & "_" & Minute(Time) & "_" & Second(Time)
'Save document
         ActiveDocument.SaveAs FileName:=strDate & strTime

Click Here Before Posting Data or VBA Code ---> How To Post Data or Code.

Report •

Related Solutions

April 10, 2013 at 04:49:06
You're top . I do not know how to thank you. You saved me. Thanks again.

Report •

Ask Question