1

是否可以使用 VBA 重命名活动文档(我从中运行宏的 word 文档)?

现在我正在以新名称保存我的活动文档,然后尝试删除原始文档。后一部分不会通过,所以原版永远不会被删除。

任何人都知道这是否可能?

4

1 回答 1

1

我最近花了很多时间这样做,因为我不喜欢在执行“另存为”时必须删除以前的文件——我想要一个“另存为并删除旧文件”的答案。我的答案是从这里复制的。

我将它添加到运行良好的快速启动栏。

  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
于 2013-09-07T23:36:37.430 回答