+ Reply to Thread
Results 1 to 8 of 8

Prevent Save, enable SaveAs with default file name

Hybrid View

  1. #1
    Registered User
    Join Date
    01-15-2009
    Location
    Vancouver, Canada
    MS-Off Ver
    Excel 2003
    Posts
    5

    Prevent Save, enable SaveAs with default file name

    hello all VBA/Excel experts, I am new to this forum and asking for your help. Here is what I am trying to do:
    1) if a user clicks Save or Ctrl-S, a message should come up saying that this is not allowed, as it would overwrite the existing file (this works)
    2) if a user clicks Save As, it should suggest a default file name (Consisting of a value in a cell + date) and it should save the file with either the suggested name or a new name (this does not work, a message from Step 1 comes up). Below is the code. Thank you so much for your help and have a great day.
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    If SaveAsUI = False Then
       Cancel = True
        MsgBox "This would overwrite the template. Use 'Save As' instead."
        Exit Sub
        End If
    SaveAsUI = True
    Dim oFileDialog As FileDialog
    Set oFileDialog = Application.FileDialog(msoFileDialogSaveAs)
      Dim strName As String
      Dim strDate As String
      Dim strCustName As String
      Dim strShortCustName As String
      Dim myPath As String
      Cancel = False
    myPath = ActiveWorkbook.Path
    strCustName = Sheet4.Range("C4")
    strShortCustName = Left(strCustName, 6)
    strDate = Format(Date, "Medium Date")
    strName = myPath & "/" & strShortCustName & " " & strDate & ".xls"
        With oFileDialog
        SaveAsUI = True
            .InitialFileName = strName
            .Show
            .Execute
            Cancel = False
            End With
    End Sub

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    Welcome to the forum.

    Ignoring your question for the moment,

    1. What keeps the user from using the SaveAs dialog to save over the original file? Why not just make the file read-only?

    2. Once the user does a SaveAs to another filename, they are still precluded from doing a Save. Is that your intent?
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    01-15-2009
    Location
    Vancouver, Canada
    MS-Off Ver
    Excel 2003
    Posts
    5
    hello shg,

    1. What keeps the user from using the SaveAs dialog to save over the original file? Why not just make the file read-only?

    Yes good idea, I will make it read-only. I would still like to have the message box appear if a user clicks save.

    2. Once the user does a SaveAs to another filename, they are still precluded from doing a Save. Is that your intent?

    This is exactly my problem. I would like the user to be able to save the file if they go through SaveAs, but not if they go through Save. Thank you.

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    Quote Originally Posted by shg
    2. Once the user does a SaveAs to another filename, they are still precluded from doing a Save. Is that your intent?
    This is exactly my problem. I would like the user to be able to save the file if they go through SaveAs, but not if they go through Save. Thank you.
    So -- the file is named "Bob". I dutifully save the file as "Joe". After that, I'm still not allowed to just press Save to resave "Joe"?

  5. #5
    Registered User
    Join Date
    01-15-2009
    Location
    Vancouver, Canada
    MS-Off Ver
    Excel 2003
    Posts
    5
    Hello shg:

    So -- the file is named "Bob". I dutifully save the file as "Joe". After that, I'm still not allowed to just press Save to resave "Joe"?

    If this was possible, that would be great. The process would then be:
    1. Open Bob
    2. If save Bob, not allowed
    3. If save as Joe, allowed
    4. If save Joe, allowed
    Thank you!

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
        If ThisWorkbook.ReadOnly And Not SaveAsUI Then
            MsgBox "This file is read-only. Please use File > SaveAs."
            Cancel = True
        End If
    End Sub

  7. #7
    Registered User
    Join Date
    11-04-2013
    Location
    Spartanburg, SC
    MS-Off Ver
    Excel 2013
    Posts
    1

    Re: Prevent Save, enable SaveAs with default file name

    Dora,
    I ran across this thread and it looks like what I am trying to accomplish with my spreadsheet. Did you ever get this resolved and if so what was the code you used?

    Thanks

  8. #8
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Prevent Save, enable SaveAs with default file name

    This is a 4 yr old thread and not likely that the original poster will come back to reply to it. Its better you create a new thread for your question. Check the 1st point in this link - http://www.excelforum.com/faq.php to know how.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1