如何在 MS Office 中同时重命名文件并删除旧版本?

end*_*and 19 microsoft-office rename microsoft-word microsoft-powerpoint microsoft-excel

在 Microsoft Office 中,当想要将文件保存为不同的文件名而不保留先前文件名的副本时,这样做需要两个步骤:

  • 首先,文件 -> 另存为...并选择新名称。制作了文件的副本
  • 然后,进入 Windows 资源管理器并删除旧名称的旧文件。

我想通过一步“重命名”来自 Office 本身的文件来简化这些步骤。我怎么能这样做?

有关更有趣和更神秘的版本,请参阅修订版 1

end*_*and 12

回答这个问题的“最简单”方法似乎是建立在这个答案之上的。

  1. 将以下代码插入 normal.dotm 模板(在C:\Documents and Settings\user name\Application Data\Microsoft\TemplatesWindows 7 for Word 中找到)
  2. 保存 normal.dotm
  3. 将此添加到 Word 中的快速启动工具栏。
  4. 可选 - 将键盘快捷键重新映射到此
  5. 可选 - 对您的模板进行数字签名(推荐)

请注意,这实际上将旧文件移动到回收站而不是完全删除,并且还以非常方便的方式设置新文件名。


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
Run Code Online (Sandbox Code Playgroud)


xdu*_*ine 11

您无法使用内置功能执行此操作。正如办公室在其文档中所述

重命名文件会更改现有文件的文件名。当有人在任何程序中打开文件时,您无法重命名文件。文件必须关闭,如果是共享文件,则必须签入。您可以使用新名称保存打开的文件,但原名称的文件副本仍将存在。

似乎可以通过使用VSTO或 VBA创建自定义的“重命名为...”函数来构建类似的内容(如 Oliver 的回答)。您只需对其进行编程以保存新副本,然后删除旧副本。


Der*_*ler 6

这是我拼凑的一个小 VBA 宏,它几乎完全符合您的要求:

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
Run Code Online (Sandbox Code Playgroud)