PDA

View Full Version : Autosave with filename and datetime stamp



tonyl
04-22-2013, 09:15 PM
Hello all, I am extremely new to VBA and hope you will bear with me.

What I am trying to do is Autosave a word form with a filename generated from a formfield and append the date time to the filename.

This is the code I have at the moment which works fine but I cannot for the life of me work out how to append the date

Private Sub Document_Close()
Dim FileName As String

ActiveDocument.SaveAs FileName:="C:\forms\orders\" & _
ActiveDocument.FormFields("text1").Result & ".doc"
End Sub

It is probably really simple, but I can't work it out. Any help will be greatly appreciated

Thank you in advance

Tony

fumei
04-23-2013, 03:15 PM
It is fairly simple. What we need is an exact example of how you want it. You state "date time". Does that mean something like (let's assume the result of Text1 is "yadda"):

C:\forms\orders\yaddaApril24201313:14:37

yadda & April 24 2013 13h14m37s

tonyl
04-23-2013, 06:00 PM
It is fairly simple. What we need is an exact example of how you want it. You state "date time". Does that mean something like (let's assume the result of Text1 is "yadda"):

C:\forms\orders\yaddaApril24201313:14:37

yadda & April 24 2013 13h14m37s

Thank you for the timely reply, apologies for not being more specific, would like to have it as C:\forms\orders\yadda_1437240413 which is a military time/date stamp but would settle for C:\forms\orders\yadda-14:37-24-04-13, the main reason being if I have more than one to the same company on the same day. The date is in "text15" formfield and the extension is .doc using office XP

Thank you

Tony

fumei
04-23-2013, 08:05 PM
Oh, the date is from the textbox. Not system- time? Significant piece of information to know. That's OK. Just as a heads up though, the more and explicit information you give, the better and explicit infomation you get back.

Try
Dim SaveName As String

SaveName = "C:\forms\orders\" & _
ActiveDocument.FormFields("text1").Result & "_" & _
Format(ActiveDocument.FormFields("text15").Result, "hmddmmyy") & _
".doc"
ActiveDocument.SaveAs FileName:=SaveNameThe key thing you are looking for is Format. Put your cursor on the word (in the code) and press F1 to get VBA Help. Click on Examples to see how Format works.

Important note. As you can see yourself - put some text into "text15" - you can have something that can NOT be formatted into a viable date-time. If "text15" has "skhfksfhs vksbvfkbsv", then that is what you get.

Oh, and notice that I changed your FileName to SaveName. That is a word used by VBA itself. It is not a reserved word, but it is an internal word (see FileName:=). It is best to avoid such terms.

fumei
04-23-2013, 08:26 PM
BTW, you can get yadda-14:37-24-04-13 if you wanted. You can get almost anything you want. I hope you study Format because it can be powerful. As you can see, 14:37 April 24 2013 can be:

143724042013 (using "hmddmmyy" - no spaces)

14:37 24042013 (using "h:m ddmmyy" - notice the space)

14:37_24042013 (using "h:m_ddmmyy" - notice the underscore)

Time 14:37_Date 24042013 (using "Ti""m""e h:m_""D""ate ddmmyy")


As "m" and "d" ARE reserved (month and day) in this context (Format), to get a literal m (for Time) and literal d (for Date), you must use the indication for a string literal - TWO quotation marks. This tells VBA that the character (s) between them should be considered nothing BUT a string.

The point being is that Format is not just for dates or times, although that is probably its most common use.

tonyl
04-24-2013, 04:14 AM
Thank you so much Fumei, the code works almost perfectly. The only problem is the time is coming out as 00 as in the filename ends up as C:
\forms\orders\yadda_00240413, does the time stamp get picked up from the system time or is it looking in the formfield text15 as this field only contains the date and I can't see in the properties wher I can make it show the time and date. Once again I am extremely appreciative of your time and effort to help me in this endeavour

Cheers

Tony

fumei
04-24-2013, 03:49 PM
Hi Tony. There is that explicit thing again. You stated the date was coming from Text15. The Format function works with whatever is in Text15.

You did NOT state you want the time to come from system time...so it does not.

So...

You want the TIME to come from system time (i.e. the current time), but the DATE to come from something input into Text15.

I have to say it does not really make sense.

So say it is (current time and date) 14:37 April 24 2013. If Text15 is December 20 2012, you want to get 14:37 December 20 2012?

Seems odd to disconnect the time from the date. But hey....whatever.

System time is the reserved keyword Now. Format works exactly the same. To get just the time:

Fomat(Now, "hm")

Dim SaveName As String

SaveName = "C:\forms\orders\" & _
ActiveDocument.FormFields("text1").Result & "_" & _
Format(Now, "hm") & _
Format(ActiveDocument.FormFields("text15").Result, "dmmyy") & _
".doc"

fumei
04-24-2013, 03:52 PM
I have to ask. Why are you getting an input date (from text15)?

Gerry

tonyl
04-25-2013, 04:07 PM
I have to ask. Why are you getting an input date (from text15)?

Gerry
This form is used as a purchase order for our business and it just puts the current date onto the form for me (no biggy) The main reason I am going this route as most people here aren't able to grasp the file/save as and name the file with todays date and save it in the appropriate folder

Many thanks again for your help

Cheers

Tony..

fumei
04-25-2013, 07:18 PM
Well if your are getting current date for the filesave, why not just do that? It seems counter productive to use code to put the current date into Text15, and then use code again to get it out of Text15.

tonyl
04-25-2013, 11:36 PM
Well if your are getting current date for the filesave, why not just do that? It seems counter productive to use code to put the current date into Text15, and then use code again to get it out of Text15.

Thanks again for you help, works great now, I will do some research myself and work out how to use the system date as you suggested

Once again many thanks

Cheers

Tony..