PDA

View Full Version : VBA to save as word file with current date/time



megha
02-03-2012, 12:20 PM
I have a code that SaveAs the word file with current date and time stamp. It works perfect. However I want to change my code which let the user to modify that same file later on and re-save with current date and time. I would appreciate any help as I am not at all good with the VBA. Thank you!



Private Sub CommandButton1_Click()
Dim sFileName As String
Dim sPath As String
CommandButton1.Enabled = False
sFileName = Format(DateValue(Now()), "mmm_dd_yyyy") & "_" & Team.Value & "_" & Shift.Value & _
Format(TimeSerial(Hour(Now()), Minute(Now()), Second(Now())), "hh_mm_ss_AM/PM")
If Len(Dir("P:\M's DOC\Rounds\Completed Rounds\" & Format(DateValue(Now()), "mmm_yyyy"), vbDirectory)) = 0 Then
MkDir "P:\M's DOC\Rounds\Completed Rounds\" & Format(DateValue(Now()), "mmm_yyyy")
End If
sPath = "P:\M's DOC\Rounds\Completed Rounds\" & Format(DateValue(Now()), "mmm_yyyy")
sFileName = sFileName & ".doc"
ActiveDocument.SaveAs FileName:=sPath & "\" & sFileName, FileFormat:=xlNormal, ReadOnlyRecommended:=False
MsgBox "Checklist has been saved"
ThisDocument.Close SaveChanges:=False
End Sub

fumei
02-03-2012, 08:20 PM
You keep posting basically the same question over and over again. Hopefully this is going to help you move on.
Option Explicit
Const VersionFolder As String = "H:\Temp\Temp Files\"
Sub cmdCreate_Click()
Dim sFileName As String
If txtName.Value = " " Then
MsgBox "Please Enter your name the Name field", vbCritical
Else
ActiveDocument.Variables("VersionNumber").Value = 1

sFileName = Format(DateValue(Now()), "mmm_dd_yyyy") & "_" & Team.Value & "_" & Shift.Value & _
Format(TimeSerial(Hour(Now()), Minute(Now()), Second(Now())), "hh_mm_ss_AM/PM") _
& " Rev " & ActiveDocument.Variables("VersionNumber").Value & ".doc"

ThisDocument.SaveAs FileName:=VersionFolder & sFileName, ReadOnlyRecommended:=False
MsgBox "Checklist has been saved to Temp Folder"

ActiveDocument.Variables("CurVersionName").Value = ThisDocument.Name
cmdCreate.Enabled = False
With ActiveDocument
.Save
.Close
End With
End If
End Sub

Private Sub cmdModify_Click()
Dim sFileName As String
ActiveDocument.Variables("VersionNumber").Value = _
ActiveDocument.Variables("VersionNumber").Value + 1
sFileName = Format(DateValue(Now()), "mmm_dd_yyyy") & "_" & Team.Value & "_" & Shift.Value & _
Format(TimeSerial(Hour(Now()), Minute(Now()), Second(Now())), "hh_mm_ss_AM/PM") _
& " Rev " & ActiveDocument.Variables("VersionNumber").Value & ".doc"

ThisDocument.SaveAs FileName:=VersionFolder & sFileName, ReadOnlyRecommended:=False
Kill (VersionFolder & ActiveDocument.Variables("CurVersionName").Value)
' update current version name, and save again

With ActiveDocument
.Variables("CurVersionName").Value = ActiveDocument.Name
.Save
End With

End Sub
Sub Document_Open()
Me.Shift.List = Split("Day Night")
Me.Team.List = Split("A.Team B.Team C.Team D.Team")
End Sub


1. use Option Explicit

2. use Constants where you can. The folder for files is used repeatedly, so it is a good place for a constant. NOTE: change my "H:\Temp\Temp Files\" to your folder.

3. the best way to retain values is to use document Variables, in this case "CurVersionName" to hold the previous name. So that after the SaveAs the previous name can be returned for the Modify procedure. Also the version number ("VersionNumber") is added to to use for the SaveAs.

I fail to see the need for having the version number in the document. In fact, it is a BAD idea. Someone could manually change it and then badda boom your versions are messed up. If you do not need to have it capable of manual change (and you do not)...then DON'T.

fumei
02-03-2012, 08:21 PM
Note you need to do the second save. Tell me why...

fumei
02-03-2012, 08:56 PM
Oh, and to reiterate a previous question...why are you doing this??? It seems an extraordinary amount of work (that you do NOT know how to do) for a very minimal (if any) return.

BoatwrenchV8
02-14-2012, 08:02 AM
...

ThisDocument.SaveAs FileName:=VersionFolder & sFileName, ReadOnlyRecommended:=False
Kill (VersionFolder & ActiveDocument.Variables("CurVersionName").Value)
' update current version name, and save again

With ActiveDocument
.Variables("CurVersionName").Value = ActiveDocument.Name
.Save
End With


Hmmm. My stab at it: The reason for the second save is so the document variable can be re created with the new, current name and be saved in the word file created. Correct?

fumei
02-14-2012, 01:14 PM
yes, Variables are NOT saved with a SaveAs new document. They need an explicit Save. IMO, this is not quite a bug, but comes very close.

The thing with this thread though is you need to create the Varaiblevalue BEFORE the SaveAs (in order to capture the current name). So you need the explicit second save in order to retain the value in the new document.