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


19

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

  • 首先,文件->另存为...,然后选择新名称。一个拷贝文件的制成。
  • 然后,进入Windows资源管理器并删除具有旧名称的旧文件。

我想通过一步从Office本身“重命名”文件来简化这些步骤。我该怎么办?

有关更具娱乐性和神秘性的版本,请参阅修订版1


@Ramhound我想我看不到解决方案的问题所在(Travis有一个更好的解决方案,我正在做什么,而roviuser使得最好的答案似乎是VBA宏-我可能会这样做,我出于某种原因(或实际的VSTO编译加载项)没有考虑这样做
-enderland

同时?好吧,当您得到答案时,然后告诉我如何同时出现在两个地方:-)
MDMoore313

您不能因为欧盟限制了允许MS在其产品之间进行的集成。允许办公室重命名文件会破坏这些规则,并使其成为唯一文件
乍得,

@乍得你在这里开玩笑吗?我的意思是OpenOffice也可以添加重命名功能。
heinrich5991

Answers:


12

回答此问题的“最简单”方法似乎在此回答上有很大的基础。

  1. 将以下代码插入normal.dotm模板(C:\Documents and Settings\user name\Application Data\Microsoft\Templates适用于Windows 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

辛苦了 这样的问题和答案就是SuperUser的全部内容。
xdumaine 2013年

我已经广泛使用了。谢谢超级用户!
enderland 2013年

11

内置功能无法做到这一点。如办公室在文件中所述

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

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


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

1
归功于到期。我不愿意尝试这个。VBA太丑了。
xdumaine

4

不,它不是内置函数。

一种解决方法是使用新名称保存文件。然后返回“文件”,“另存为”并删除旧文件,这将比关闭文档,资源管理器,重命名和重新打开更加有效。


2
到目前为止,与进入该浏览器进行驾驶相比,这是一个更好的选择。
enderland

3

这与@Travis答案略有不同。

同样,它不是内置函数。

  1. 在Word中,关闭文件,确认是否需要保存更改。
  2. 仍然在Word中,单击以打开文件。
  3. 如有必要,导航到该文件,右键单击该文件并重命名。
  4. 仍在“文件打开”对话框中时,打开重命名的文件。

此解决方案:

  1. 消除了Windows资源管理器中长期孤独的驱动器,以删除旧文件。
  2. 到“文件打开/另存为”对话框仅一趟。
  3. 仅单击几次鼠标即可完成操作,而不仅仅是“另存为”操作。
  4. 与VBA或类似解决方案相比,只需单击几下鼠标即可完成操作。
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.