Consulting

Results 1 to 6 of 6

Thread: VBA to save as word file with current date/time

  1. #1
    VBAX Regular
    Joined
    Oct 2009
    Posts
    69
    Location

    VBA to save as word file with current date/time

    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!


    [VBA]
    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
    [/VBA]

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    You keep posting basically the same question over and over again. Hopefully this is going to help you move on.
    [vba]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
    [/vba]

    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.
    Attached Files Attached Files

  3. #3
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Note you need to do the second save. Tell me why...

  4. #4
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.

  5. #5
    [VBA]
    ...

    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
    [/VBA]

    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?

  6. #6
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.
    Last edited by fumei; 02-14-2012 at 01:29 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •