VBA - 如何从 Excel 2007 的最近文档列表中删除文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/879334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 10:26:00  来源:igfitidea点击:

VBA - How do I remove a file from the recent documents list in excel 2007?

excel-vbafilems-officevbaexcel

提问by notnot

The recent documents feature in Office is really useful, but I moved a file to a new directory and now I can't get Excel to stop hitting me with a "can't find this file" notification whenever I open a workbook. The Excel options seem only to control how many of these "recent documents" are displayed and not how many are actually saved. So I;'m wondering if there's a way in VBA to get at the list and remove the offending file.

Office 中的最近文档功能非常有用,但我将一个文件移动到一个新目录,现在我无法让 Excel 停止在我打开工作簿时发出“找不到此文件”的通知。Excel 选项似乎只控制显示这些“最近的文档”的数量,而不控制实际保存的数量。所以我想知道 VBA 中是否有办法获取列表并删除有问题的文件。

回答by Jato

Try this...

尝试这个...

Public Function TestIt()
    For i = 1 To Application.RecentFiles.Count - 1
        Dim answer As String
        answer = MsgBox("Delete " & Application.RecentFiles(i).Name, vbYesNo)

        If answer = vbYes Then
            answer = MsgBox("Are you sure?", vbYesNo)
            If answer = vbYes Then
                Application.RecentFiles(i).Delete
            End If
        End If
    Next i
End Function

回答by Paul Roub

Not a VBA solution, but open up Regedit and you can remove files from the list at will.

不是 VBA 解决方案,但打开 Regedit,您可以随意从列表中删除文件。

The "File MRU" list is what you're after; for Excel 2007 it's under

“文件 MRU”列表是您所追求的;对于 Excel 2007,它位于

HKEY_CURRENT_USER\Software\Microsoft\Office.0\Excel\File MRU

Adjust the version number accordingly.

相应地调整版本号。

Close Excel, delete the offending file's entry from the list found there, and restart.

关闭 Excel,从那里找到的列表中删除有问题的文件条目,然后重新启动。

回答by Gunnar Bernstein

Facing the same issue I wrote this litte macro, removing all files from the recent file list, which are not accessible:

面对同样的问题,我写了这个小宏,从最近的文件列表中删除了所有无法访问的文件:

Public Function CheckRecentFiles() As Integer
    Dim i As Integer, c As Integer
    For i = Application.RecentFiles.count To 1 Step -1
        'Debug.Print Application.RecentFiles(i).name
        If Dir(Application.RecentFiles(i).name) = "" Then
            Debug.Print "Delete from recent file list: " & Application.RecentFiles(i).name
            Application.RecentFiles(i).Delete
            c = c + 1
        End If
    Next i
    Debug.Print c & " files removed."
    CheckRecentFiles = c
End Function

回答by Gunnar Bernstein

Based on @GunnarBernsteinI 's answer,I just added this to my Personal Macro Book. This is going to be super handy to clean up the temp files that I create to answer questions on SO.

根据@GunnarBernsteinI 的回答,我刚刚将其添加到我的个人宏手册中。这对于清理我为回答有关 SO 的问题而创建的临时文件将非常方便。

Public Sub CleanRecentFiles()
    Const ReviewEntry As Boolean = False
    Dim f As RecentFile
    For Each f In Application.RecentFiles
        If Len(Dir(f.Name)) = 0 Then
            f.Delete
        ElseIf ReviewEntry Then
            Debug.Print f.Name
            Stop
        End If
    Next
End Sub

Demo

演示

enter image description here

在此处输入图片说明

回答by Seema

Open the Recent Workbooks List. Right click quickly and firmly between the icon and text for the document you wish to remove from the list. A dropdown list appears. It is the list which allows you to pin an item to the list. Choose Remove from List. It does work but it can be a bit tricky to time it correctly. If you are too slow it will just try to open the file.

打开最近的工作簿列表。在要从列表中删除的文档的图标和文本之间快速而牢固地右键单击。出现一个下拉列表。它是允许您将项目固定到列表的列表。选择从列表中删除。它确实有效,但正确计时可能有点棘手。如果你太慢,它只会尝试打开文件。

回答by Hilli

Try the routine above not as function but as SUB. And in the second line remove "-1" at its end, because the last entry will not be handled else.

不是作为函数而是作为 SUB 尝试上面的例程。并在第二行删除其末尾的“-1”,因为不会处理最后一个条目。

Then the routine will work properly.

然后例程将正常工作。