Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

In Microsoft Office, when wanting to save a file to a different file name without keeping a copy of the previous file name, doing so requires two steps:

  • First, File -> Save As... and select the new name. A copy of the file is made.
  • Then, go into Windows Explorer and delete the old file with the old name.

I would like to simplify these steps by "renaming" the file, from Office itself, in one step. How could I do this?

For a more entertaining and cryptic version see Revision 1.

share|improve this question
    
@Ramhound I guess I fail to see how this isn't a problem with a solution (Travis has a better solution that what I am doing and roviuser makes it seem like the best answer is either a VBA macro - which I might do, I didn't think to do this for some reason - or an actual VSTO compiled add-on) –  enderland May 3 '13 at 17:33
    
Simultaneous? Well, when you get the answer then tell my how to be in two places at once:-) –  BigHomie May 3 '13 at 18:01
    
You can not because the EU has limited the integration that MS is allowed to make between its products. Allowing office to rename a file would break those rules and make it a monoply –  Chad May 3 '13 at 18:21
    
@Chad are you kidding here? I mean OpenOffice could just do add a rename function too. –  heinrich5991 May 3 '13 at 20:26

5 Answers 5

up vote 11 down vote accepted

The "easiest" way to answer this appears to be significantly building on this answer.

  1. Insert following code into normal.dotm template (found in C:\Documents and Settings\user name\Application Data\Microsoft\Templates for Windows 7 for Word)
  2. Save normal.dotm
  3. Add this to the quicklaunch toolbar in Word.
  4. Optional - remap a keyboard shortcut to this
  5. Optional - digitally sign your template (recommended)

Note this actually moves the old file to the Recycle Bin rather than trashing completely and also sets the new file name in a very convenient fashion.


Option Explicit

 'To send a file to the recycle bin, we'll need to use the Win32 API
 'We'll be using the SHFileOperation function which uses a 'struct'
 'as an argument. That struct is defined here:
Private Type SHFILEOPSTRUCT
    hwnd As Long
    wFunc As Long
    pFrom As String
    pTo As String
    fFlags As Integer
    fAnyOperationsAborted As Long
    hNameMappings As Long
    lpszProgressTitle As Long
End Type

 ' function declaration:
Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long

 'there are some constants to declare too
Private Const FO_DELETE = &H3
Private Const FOF_ALLOWUNDO = &H40
Private Const FOF_NOCONFIRMATION = &H10
Private Const FOF_SILENT = &H4

Function RecycleFile(FileName As String, Optional UserConfirm As Boolean = True, Optional HideErrors As Boolean = False) As Long
     'This function takes one mandatory argument (the file to be recycled) and two
     'optional arguments: UserConfirm is used to determine if the "Are you sure..." dialog
     'should be displayed before deleting the file and HideErrors is used to determine
     'if any errors should be shown to the user

    Dim ptFileOp As SHFILEOPSTRUCT
     'We have declared FileOp as a SHFILEOPSTRUCT above, now to fill it:
    With ptFileOp
        .wFunc = FO_DELETE
        .pFrom = FileName
        .fFlags = FOF_ALLOWUNDO
        If Not UserConfirm Then .fFlags = .fFlags + FOF_NOCONFIRMATION
        If HideErrors Then .fFlags = .fFlags + FOF_SILENT
    End With
     'Note that the entire struct wasn't populated, so it would be legitimate to change it's
     'declaration above and remove the unused elements. The reason we don't do that is that the
     'struct is used in many operations, some of which may utilise those elements

     'Now invoke the function and return the long from the call as the result of this function
    RecycleFile = SHFileOperation(ptFileOp)

End Function


Sub renameAndDelete()

    ' Store original name
    Dim sOriginalName As String
    sOriginalName = ActiveDocument.FullName

    ' Save As
    Dim sFilename As String, fDialog As FileDialog, ret As Long
    Set fDialog = Application.FileDialog(msoFileDialogSaveAs)

    'set initial name so you don't have to navigate to
    fDialog.InitialFileName = sOriginalName

    ret = fDialog.Show

    If ret <> 0 Then
        sFilename = fDialog.SelectedItems(1)
    Else
        Exit Sub
    End If

    Set fDialog = Nothing

    'only do this if the file names are different...
    If (sFilename <> sOriginalName) Then
        'I love vba's pretty code
         ActiveDocument.SaveAs2 FileName:=sFilename, FileFormat:= _
            wdFormatXMLDocument, LockComments:=False, Password:="", AddToRecentFiles _
            :=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts _
            :=False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
            SaveAsAOCELetter:=False, CompatibilityMode:=14

        ' Delete original (don't care about errors, I guess)
        Dim hatersGonnaHate As Integer
        hatersGonnaHate = RecycleFile(sOriginalName, False, True)

    End If

End Sub
share|improve this answer
    
Good work. Questions and answers like this are what SuperUser is all about. –  xdumaine May 3 '13 at 18:45
    
I have been using this extensively. Thanks superuser! –  enderland Jun 14 '13 at 17:16

You can't do this with built in functionality. As office states in it's documentation

Renaming a file changes the file name of an existing file. You cannot rename a file while someone has it open in any program. The file must be closed, and if it is a shared file, it must be checked in. You can save an open file with a new name, but a copy of the file with the original name will still exist.

It seems like something like this could be built in by creating a custom "Rename As..." function with VSTO or VBA (as in Oliver's answer). You'd just have to program it to save a new copy then delete the old one.

share|improve this answer

Here's a little VBA macro I threw together which does pretty much exactly what you want:

Sub Macro1()
    ' Store original name
    Dim sOriginalName As String
    sOriginalName = ActiveDocument.FullName

    ' Save As
    Dim sFilename As String, fDialog As FileDialog, ret As Long
    Set fDialog = Application.FileDialog(msoFileDialogSaveAs)
    ret = fDialog.Show
    If ret <> 0 Then
        sFilename = fDialog.SelectedItems(1)
    Else
        Exit Sub
    End If
    Set fDialog = Nothing

    ' Don't replace the original file
    If sFilename = sOriginalName Then Exit Sub

     ActiveDocument.SaveAs2 FileName:=sFilename, FileFormat:= _
        wdFormatXMLDocument, LockComments:=False, Password:="", AddToRecentFiles _
        :=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts _
        :=False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
        SaveAsAOCELetter:=False, CompatibilityMode:=14

    ' Delete original
    Kill sOriginalName
End Sub
share|improve this answer
1  
Credit where it's due. I wasn't willing to try this. VBA is so ugly. –  xdumaine May 3 '13 at 17:58

No, it is not a built in function.

One work around would be to save the file with the new name. Then go back to File, Save As and delete the old file which would make it more efficient than closing your document, explorer, renaming, reopening.

share|improve this answer
2  
This is by far a better option than getting into that explorer to go driving. –  enderland May 3 '13 at 17:08

Here is a slight variation on @Travis answer.

Again, it's not a built in function.

  1. In Word, close the file, confirming to save changes if necessary.
  2. Still in Word, click to Open a file.
  3. Navigate to the file if necessary, right-click the file and rename it.
  4. While still in the File Open dialog, Open the renamed file.

This solution:

  1. Eliminates the long lonely drive in Windows Explorer to delete the old file.
  2. Is only one trip to the File Open/Save-As dialog.
  3. Completes the operation with only a few more mouse-clicks than just the Save-As operation.
  4. Also completes the operation with only a few more mouse-clicks than a VBA or similar solution.
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.