在 Microsoft Office 中,当想要将文件保存为不同的文件名而不保留以前文件名的副本时,这样做需要两个步骤:
- 首先,文件 -> 另存为...,然后选择新名称。复制该文件已制作完毕。
- 然后,进入 Windows 资源管理器并删除具有旧名称的旧文件。
我想通过从 Office 本身一步“重命名”文件来简化这些步骤。我该怎么做?
欲了解更有趣、更神秘的版本,请参阅修订版 1。
答案1
回答这个问题的“最简单”方法似乎在很大程度上建立在这个答案。
- 将以下代码插入 normal.dotm 模板(适用
C:\Documents and Settings\user name\Application Data\Microsoft\Templates
于 Windows 7 的 Word) - 保存 normal.dotm
- 将其添加到 Word 中的快速启动工具栏。
- 可选 - 将键盘快捷键重新映射到此
- 可选 - 对模板进行数字签名(推荐)
请注意,这实际上是将旧文件移动到回收站而不是完全丢弃,并且还以非常方便的方式设置新文件名。
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
答案2
答案3
下面是我编写的一个小 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
答案4
这是@Travis 回答的一个细微变化。
再说一遍,它不是一个内置功能。
- 在 Word 中,关闭文件,并确认保存更改(如有必要)。
- 仍在 Word 中,单击“打开文件”。
- 如果需要,导航到该文件,右键单击该文件并重命名。
- 仍在文件打开对话框中,打开重命名的文件。
此解决方案:
- 消除了在 Windows 资源管理器中长时间孤独地删除旧文件的情况。
- 只需前往文件打开/另存为对话框一次。
- 与“另存为”操作相比,仅需单击几次鼠标即可完成操作。
- 与 VBA 或类似的解决方案相比,只需多点击几次鼠标即可完成操作。