none
Change the default save file name RRS feed

  • Question

  • Since I did not get any response in the SharePoint forum, I'll try here:

    I have created a small document management system for a client. In their old system, new documents were automatically given a file name based on some of the metadata fields. They want the same to happen in the new system (Word 2010 and SharePoint 2010), they do not want to create their own filename.

    So, is it possible - either in Word 2010 or in SharePoint 2010 - to generate the default filename automatically? So that when they have filled out the fields in their template and hit save, the suggested filename is guaranteed to be unique? No need for this to be very advanced, but maybe based on timestamp etc.

    I realize that this is something I'd have to code myself, but some hints for where to start would be nice.

    (Basically the system is just a SP2010 site with a document library, and word 2010 templates are available on the new document ribbon button.)

    Tuesday, March 29, 2011 6:35 AM

Answers

  • Hi,

    I finally found time to work on this, and ended up with the following Word macros that seem to work fine:

     

    Sub FileSave()
     If (IsSaved = True) Then
      ActiveDocument.Save
     Else
      Call MySaveAs
     End If
     
    End Sub
    
    Sub FileSaveAs()
     If (IsSaved = True) Then
      Dialogs(wdDialogFileSaveAs).Show
     Else
      Call MySaveAs
     End If
    End Sub
    
    ' adds the default save filename to the standard dialog
    Sub MySaveAs()
     With Dialogs(wdDialogFileSaveAs)
      .Name = Format(DateTime.Now, "yyyy-mm-dd--Hh-Nn-Ss")
      .Show
     End With
    End Sub
    
    ' checks if the document has been saved (once or more) before,
    ' i.e. if it has been given a path (e.g to a SharePoint library)
    Function IsSaved()
     If Len(ActiveDocument.Path) = 0 Then
      IsSaved = False
     Else
      IsSaved = True
     End If
    End Function
    

     

    In this macro, a filename is suggested if the file has not been saved before. Otherwise the File-save and File-save-as are unchanged

     


    Øystein Garnes http://devramblings.wordpress.com/
    Friday, April 15, 2011 10:52 AM
  • Hello,

    Yes, you can do this in a COM add-in: cancel the DocumentBeforeSave event and show the FileSave dialog.

    2 notes.

    DocumentBeforeSave may be called by AutoSave. Ro bypass this, you'll need to intercept all Ribbon controls that generate that event; in the event handler you'll set a flag that you check in DocumentBeforeSave: if the flag is set, then the event is caused by a user action, otherwise - this is AutoSave.

    There are two ways to show the FileSave dialog in Word:

    HTH


    Regards from Belarus (GMT + 2),

    Andrei Smolin
    Add-in Express Team Leader
    • Marked as answer by Bessie Zhao Tuesday, April 5, 2011 8:19 AM
    Tuesday, March 29, 2011 7:50 AM
  • Hi SkyWise,

     

    Andrei has pointed you to the articles that discuss the save dialogs and where you might consider calling them in an add-in. Covering the other part of your question, here’s some further information to consider.

     

    Sometimes I get rather draconian about forcing saves:

     

    MustSave:

      With wdoc.Dialogs(wdDialogFileSaveAs)

         .Name = strPathName + strProgName + "Something Unique"

          If Not .Show = -1 Then

             MsgBox("You're required to save the document now.", , ThisAddIn.GCL)

             GoTo MustSave

          End If

      End With

     

    The variable strPathName is the directory path, strProgName is a unique name I have constructed from a user entry field in a previous form, and then I append another unique label to the name.  However, to state the obvious, anytime you use a SaveAs dialog you give the user the opportunity to mess with your unique name. J

     

    As for making something in the name guaranteed to be unique I know of two methods. Store a counter in a PrivateProfileString file located in a common folder.

     

    Dim sCounter as Integer = wdoc.System.PrivateProfileString _

      (FileName:=ThisAddIn.PathToSharedDirectory + ThisAddIn.NameOfFile, Section:=ThisAddIn.YourSectionName, Key:="Counter")

     

    When you pick-up the counter’s value, increment it by 1 and then save it back into the profile string. Then use the value in your filename.

     

    Another method, which does not involve reading a stored file, is to generate a number based on time. The likelihood of it being unique is pretty high.

     

    Dim yBegin As Date = #11/15/2010#

    Dim dNow As Date = Date.UtcNow

    Dim eTicks As Int64 = dNow.Ticks - yBegin.Ticks

    Dim eS As New TimeSpan(eTicks)

     

    The value of eTicks will be a very large number and converting it to a timespan makes the resultant values of (es.Days, es.Hours, es.Minutes, es.Seconds) more practical to use.  Combine them into your unique number to be part of the filename.

     

    Hope this helps


    Regards
    • Marked as answer by Bessie Zhao Tuesday, April 5, 2011 8:19 AM
    Tuesday, March 29, 2011 1:34 PM

All replies

  • Hello,

    Yes, you can do this in a COM add-in: cancel the DocumentBeforeSave event and show the FileSave dialog.

    2 notes.

    DocumentBeforeSave may be called by AutoSave. Ro bypass this, you'll need to intercept all Ribbon controls that generate that event; in the event handler you'll set a flag that you check in DocumentBeforeSave: if the flag is set, then the event is caused by a user action, otherwise - this is AutoSave.

    There are two ways to show the FileSave dialog in Word:

    HTH


    Regards from Belarus (GMT + 2),

    Andrei Smolin
    Add-in Express Team Leader
    • Marked as answer by Bessie Zhao Tuesday, April 5, 2011 8:19 AM
    Tuesday, March 29, 2011 7:50 AM
  • Hi SkyWise,

     

    Andrei has pointed you to the articles that discuss the save dialogs and where you might consider calling them in an add-in. Covering the other part of your question, here’s some further information to consider.

     

    Sometimes I get rather draconian about forcing saves:

     

    MustSave:

      With wdoc.Dialogs(wdDialogFileSaveAs)

         .Name = strPathName + strProgName + "Something Unique"

          If Not .Show = -1 Then

             MsgBox("You're required to save the document now.", , ThisAddIn.GCL)

             GoTo MustSave

          End If

      End With

     

    The variable strPathName is the directory path, strProgName is a unique name I have constructed from a user entry field in a previous form, and then I append another unique label to the name.  However, to state the obvious, anytime you use a SaveAs dialog you give the user the opportunity to mess with your unique name. J

     

    As for making something in the name guaranteed to be unique I know of two methods. Store a counter in a PrivateProfileString file located in a common folder.

     

    Dim sCounter as Integer = wdoc.System.PrivateProfileString _

      (FileName:=ThisAddIn.PathToSharedDirectory + ThisAddIn.NameOfFile, Section:=ThisAddIn.YourSectionName, Key:="Counter")

     

    When you pick-up the counter’s value, increment it by 1 and then save it back into the profile string. Then use the value in your filename.

     

    Another method, which does not involve reading a stored file, is to generate a number based on time. The likelihood of it being unique is pretty high.

     

    Dim yBegin As Date = #11/15/2010#

    Dim dNow As Date = Date.UtcNow

    Dim eTicks As Int64 = dNow.Ticks - yBegin.Ticks

    Dim eS As New TimeSpan(eTicks)

     

    The value of eTicks will be a very large number and converting it to a timespan makes the resultant values of (es.Days, es.Hours, es.Minutes, es.Seconds) more practical to use.  Combine them into your unique number to be part of the filename.

     

    Hope this helps


    Regards
    • Marked as answer by Bessie Zhao Tuesday, April 5, 2011 8:19 AM
    Tuesday, March 29, 2011 1:34 PM
  • Thanks Andrei and RichMichaels!

    Some other more important events have come up, but I will look into these tips Soon(TM).

    Tuesday, April 5, 2011 10:03 AM
  • Hi,

    I finally found time to work on this, and ended up with the following Word macros that seem to work fine:

     

    Sub FileSave()
     If (IsSaved = True) Then
      ActiveDocument.Save
     Else
      Call MySaveAs
     End If
     
    End Sub
    
    Sub FileSaveAs()
     If (IsSaved = True) Then
      Dialogs(wdDialogFileSaveAs).Show
     Else
      Call MySaveAs
     End If
    End Sub
    
    ' adds the default save filename to the standard dialog
    Sub MySaveAs()
     With Dialogs(wdDialogFileSaveAs)
      .Name = Format(DateTime.Now, "yyyy-mm-dd--Hh-Nn-Ss")
      .Show
     End With
    End Sub
    
    ' checks if the document has been saved (once or more) before,
    ' i.e. if it has been given a path (e.g to a SharePoint library)
    Function IsSaved()
     If Len(ActiveDocument.Path) = 0 Then
      IsSaved = False
     Else
      IsSaved = True
     End If
    End Function
    

     

    In this macro, a filename is suggested if the file has not been saved before. Otherwise the File-save and File-save-as are unchanged

     


    Øystein Garnes http://devramblings.wordpress.com/
    Friday, April 15, 2011 10:52 AM
  • Hi,

    I finally found time to work on this, and ended up with the following Word macros that seem to work fine:

     

    Sub FileSave()
     If (IsSaved = True) Then
      ActiveDocument.Save
     Else
      Call MySaveAs
     End If
     
    End Sub
    
    Sub FileSaveAs()
     If (IsSaved = True) Then
      Dialogs(wdDialogFileSaveAs).Show
     Else
      Call MySaveAs
     End If
    End Sub
    
    ' adds the default save filename to the standard dialog
    Sub MySaveAs()
     With Dialogs(wdDialogFileSaveAs)
      .Name = Format(DateTime.Now, "yyyy-mm-dd--Hh-Nn-Ss")
      .Show
     End With
    End Sub
    
    ' checks if the document has been saved (once or more) before,
    ' i.e. if it has been given a path (e.g to a SharePoint library)
    Function IsSaved()
     If Len(ActiveDocument.Path) = 0 Then
      IsSaved = False
     Else
      IsSaved = True
     End If
    End Function
    

     

    In this macro, a filename is suggested if the file has not been saved before. Otherwise the File-save and File-save-as are unchanged

     


    Øystein Garnes http://devramblings.wordpress.com/
    Very useful. Thank you
    Wednesday, May 15, 2013 12:46 PM
  • Thanks, this works! If I wanted to make the default based upon a Document Property, how would I do that?
    Friday, May 4, 2018 10:48 PM