Consulting

Results 1 to 8 of 8

Thread: Solved: "Save As" Date as part of file name

  1. #1
    VBAX Regular
    Joined
    Mar 2008
    Location
    Texas
    Posts
    19
    Location

    Solved: "Save As" Date as part of file name

    I need help again. I have a button macro open a form with a predefined path and need it to use as part of the filename, cell H4 which is a date. No matter how I format the cell it still wants to save the date with / instead of -. H4 is formatted as MM-DD-YYYY and it still uses MM/DD/YYYY.

    Also, how do I get the path to save to the "My Documents" of the user who is saving the document.

    Here is the code:

    Private Sub SaveWorkbookAs_Click()
    '
    ' Macro recorded 03/10/2008


    Dim Msg, Style, Title, Help, Ctxt, Response, MyString
    DestFile = InputBox _
    ("Enter the full path and file name to save." _
    & Chr(10) & "Example- C:\Documents and Settings\User\My Documents\Technician Activity Log Week of 03-04-2008.xls (valid path)" _
    & Chr(10) & "Make sure to tag the filename with the 'XLS' extention, so you continue working on it at a later date." _
    & Chr(10) & "The Click on OK to save the workbook.", "Technician Activity Log - SAVE WORKBOOK AS", "C:\Documents and Settings\" & (User) & "\My Documents\Technician Activity Log Week of " & (Sheets("Monday").Range("H4")) & ".xls")


    On Error Resume Next

    ActiveWorkbook.SaveAs Filename:=DestFile, FileFormat:= _
    xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
    , CreateBackup:=False
    If Err <> 0 Then
    MsgBox " DID NOT SAVE WORKBOOK. " _
    & Chr(10) & "" _
    & Chr(10) & " Possible causes," _
    & Chr(10) & "" _
    & Chr(10) & "- Action cancelled by you." _
    & Chr(10) & "- No filename was specified." _
    & Chr(10) & "- The destination directory does not exist. Create it and try again." _
    Exit Sub
    End If
    On Error GoTo lineabort

    MsgBox "Workbook succesfully saved as " & DestFile
    Exit Sub
    lineabort:
    Exit Sub


    End Sub

  2. #2
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    [VBA]Private Sub SaveWorkbookAs_Click()
    '
    ' Macro recorded 03/10/2008


    Dim Msg, Style, Title, Help, Ctxt, Response, MyString
    DestFile = InputBox _
    ("Enter the full path and file name to save." _
    & Chr(10) & "Example- C:\Documents and Settings\User\My Documents\Technician Activity Log Week of 03-04-2008.xls (valid path)" _
    & Chr(10) & "Make sure to tag the filename with the 'XLS' extention, so you continue working on it at a later date." _
    & Chr(10) & "The Click on OK to save the workbook.", "Technician Activity Log - SAVE WORKBOOK AS", "C:\Documents and Settings\" & (User) & "\My Documents\Technician Activity Log Week of " & (format(Sheets("Monday").Range("H4")).value,"mm-dd-yyyy) & ".xls"
    On Error Resume Next

    ActiveWorkbook.SaveAs Filename:=DestFile, FileFormat:= _
    xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
    , CreateBackup:=False
    If Err <> 0 Then
    MsgBox " DID NOT SAVE WORKBOOK. " _
    & Chr(10) & "" _
    & Chr(10) & " Possible causes," _
    & Chr(10) & "" _
    & Chr(10) & "- Action cancelled by you." _
    & Chr(10) & "- No filename was specified." _
    & Chr(10) & "- The destination directory does not exist. Create it and try again." _
    Exit Sub
    End If
    On Error GoTo lineabort

    MsgBox "Workbook succesfully saved as " & DestFile
    Exit Sub
    lineabort:
    Exit Sub

    End Sub[/VBA]
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  3. #3
    VBAX Mentor tstav's Avatar
    Joined
    Feb 2008
    Location
    Athens
    Posts
    350
    Location
    Hi canselmi,
    I've done various changes to your code.
    [vba]Private Sub SaveWorkbookAs_Click()
    Dim Msg, Style, Title, Help, Ctxt, Response, MyString

    DestFile = InputBox _
    ("Enter the full path and file name to save." _
    & Chr(10) & "Example- C:\Documents and Settings\User\My Documents\Technician Activity Log Week of 03-04-2008.xls (valid path)" _
    & Chr(10) & "Make sure to tag the filename with the 'XLS' extention," & vbCrLf & _
    "so you continue working on it at a later date." _
    & Chr(10) & "Then Click on OK to save the workbook.", _
    "Technician Activity Log - SAVE WORKBOOK AS", _
    "C:\Documents and Settings\(User)\My Documents\Technician Activity Log Week of " & _
    Format(Sheets("Monday").Range("H4").Value, "mm-dd-yyyy") & ".xls")

    On Error Resume Next
    ActiveWorkbook.SaveAs Filename:=DestFile, FileFormat:= _
    xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _
    CreateBackup:=False

    If Err <> 0 Then
    MsgBox " DID NOT SAVE WORKBOOK. " _
    & Chr(10) & "" _
    & Chr(10) & " Possible causes," _
    & Chr(10) & "" _
    & Chr(10) & "- Action cancelled by you." _
    & Chr(10) & "- No filename was specified." _
    & Chr(10) & "- The destination directory does not exist. Create it and try again."
    Exit Sub
    Else
    MsgBox "Workbook succesfully saved as " & DestFile
    End If
    End Sub[/vba]

    Oh, Simon had already answered before my post. Maybe I'm giving the same answer...
    He didn't know it was impossible, so he did it. (Jean Cocteau)

  4. #4
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    tstav you did but you tidied the code up and i just noticed a typo in the addition to the code i made:
    [VBA]
    ......My Documents\Technician Activity Log Week of " & (format(Sheets("Monday").Range("H4")).value,"mm-dd-yyyy) & ".xls"
    [/VBA]should read:
    [VBA]
    ......My Documents\Technician Activity Log Week of " & format(Sheets("Monday").Range("H4")).value,"mm-dd-yyyy") & ".xls"
    [/VBA]i had a "(" where i didn't need one and was missing a ", its 'cos i haven't woke up yet!
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  5. #5
    VBAX Regular
    Joined
    Mar 2008
    Location
    Texas
    Posts
    19
    Location
    Can you tell me how to set the path to include the username because (User) will not work and I've tried %User% as well. Is it %Username%?

    See below

    [VBA]"C:\Documents and Settings\(User)\My Documents\Technician Activity Log Week of " & _
    Format(Sheets("Monday").Range("H4").Value, "mm-dd-yyyy") & ".xls") [/VBA]

  6. #6
    VBAX Mentor tstav's Avatar
    Joined
    Feb 2008
    Location
    Athens
    Posts
    350
    Location
    Quote Originally Posted by canselmi
    Can you tell me how to set the path to include the username
    Here you are:

    [vba]"C:\Documents and Settings\" & Environ.("Username") & "\My Documents\Technician Activity Log Week of " & _
    Format(Sheets("Monday").Range("H4").Value, "mm-dd-yyyy") & ".xls") [/vba]
    He didn't know it was impossible, so he did it. (Jean Cocteau)

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    CreateObject("WScript.Shell").Specialfolders(16) & "\Technician Activity Log Week of " & _
    Format(Sheets("Monday").Range("H4").Value, "mm-dd-yyyy") & ".xls")
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8
    VBAX Regular
    Joined
    Mar 2008
    Location
    Texas
    Posts
    19
    Location
    XLD, thank you again and to everyone else that helped. The last code worked perfect. You are still "the bomb" in my books.

Posting Permissions

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