如何使用VBA删除Excel功能区中的最近文档历史记录

时间:2022-09-02 08:24:39

How to remove the recent document history in Excel Ribbon using VBA.

如何使用VBA删除Excel功能区中的最近文档历史记录。

I am using the code below, but it doesn't seems to work.

我正在使用下面的代码,但它似乎不起作用。

Sub Button1_Click()
    For i = 1 To Application.RecentFiles.Count - 1
        Application.RecentFiles(i).Delete
    Next i
End Sub

Thanks ...

谢谢 ...

3 个解决方案

#1


3  

To clear the list of recently used files, and not mess with the user's settings, the following code will work:

要清除最近使用的文件列表,而不是弄乱用户的设置,以下代码将起作用:

originalSetting = Application.RecentFiles.Maximum

Application.RecentFiles.Maximum = 0

Application.RecentFiles.Maximum = originalSetting

This will remove the recent files and then reset the maximum number of recent files back to whatever the user had initially.

这将删除最近的文件,然后将最近的最大文件数重置回用户最初的任何内容。

If you just want to remove them individually, you can step through them in reverse order to get the job done.

如果您只是想单独删除它们,可以按相反的顺序逐步执行它们以完成工作。

 Dim i As Integer

For i = Application.RecentFiles.Count To 1 Step -1
    Application.RecentFiles.Item(i).Delete
Next

You need to run from the bottom of the collection up, because as soon as you delete one of the entries from the RecentFiles collection, all of the indexes of the remaining files change. This way, each time through the loop, you are deleting the last item in the collection.

您需要从集合的底部开始运行,因为只要从RecentFiles集合中删除其中一个条目,其余文件的所有索引都会更改。这样,每次循环时,您都将删除集合中的最后一项。

And also, since this collection is Base 1 instead of Base 0, the last item in the collection is Application.RecentFiles.Count rather than .RecentFiles.Count-1.

而且,由于此集合是Base 1而不是Base 0,因此集合中的最后一项是Application.RecentFiles.Count而不是.RecentFiles.Count-1。

I just love all those little inconsistencies in Excel.. :)

我只是喜欢Excel中所有那些不一致的东西.. :)

#2


2  

There is no direct mechanism for hiding the most recently used file listing. It can be done, however, by setting the Application.RecentFiles.Maximum to zero (0).

没有用于隐藏最近使用的文件列表的直接机制。但是,可以通过将Application.RecentFiles.Maximum设置为零(0)来完成。

For a detailed discussion, see Change the Ribbon in Excel 2007 by Ron de Bruin, and scroll down to the section titled "Dictator examples and Hide the MRU ('Most Recently Used') file list", with code provided by Jim Rech.

有关详细讨论,请参阅Ron de Bruin在Excel 2007中更改功能区,并向下滚动到标题为“Dictator示例和隐藏MRU('最近使用的')文件列表”的部分,其中包含由Jim Rech提供的代码。

This can also be done manually. See: How to Clear and Delete Recent Documents List in Office 2007 (Word, Excel, PowerPoint).

这也可以手动完成。请参阅:如何在Office 2007中清除和删除最近的文档列表(Word,Excel,PowerPoint)。

-- Mike

- 迈克

#3


0  

To clear the activeworkbook from the list of recentfiles use this:

要从recentfiles列表中清除activeworkbook,请使用以下命令:

Sub DeleteFileFromRecentFiles()
    Dim i As Integer
    For Each RecentFile In Application.RecentFiles
        If ActiveWorkbook.Name = RecentFile.Name Then
            i = i + 1
            Application.RecentFiles.Item(i).Delete
        End If
    Next
End Sub

Sincerely, Richard

理查德

#1


3  

To clear the list of recently used files, and not mess with the user's settings, the following code will work:

要清除最近使用的文件列表,而不是弄乱用户的设置,以下代码将起作用:

originalSetting = Application.RecentFiles.Maximum

Application.RecentFiles.Maximum = 0

Application.RecentFiles.Maximum = originalSetting

This will remove the recent files and then reset the maximum number of recent files back to whatever the user had initially.

这将删除最近的文件,然后将最近的最大文件数重置回用户最初的任何内容。

If you just want to remove them individually, you can step through them in reverse order to get the job done.

如果您只是想单独删除它们,可以按相反的顺序逐步执行它们以完成工作。

 Dim i As Integer

For i = Application.RecentFiles.Count To 1 Step -1
    Application.RecentFiles.Item(i).Delete
Next

You need to run from the bottom of the collection up, because as soon as you delete one of the entries from the RecentFiles collection, all of the indexes of the remaining files change. This way, each time through the loop, you are deleting the last item in the collection.

您需要从集合的底部开始运行,因为只要从RecentFiles集合中删除其中一个条目,其余文件的所有索引都会更改。这样,每次循环时,您都将删除集合中的最后一项。

And also, since this collection is Base 1 instead of Base 0, the last item in the collection is Application.RecentFiles.Count rather than .RecentFiles.Count-1.

而且,由于此集合是Base 1而不是Base 0,因此集合中的最后一项是Application.RecentFiles.Count而不是.RecentFiles.Count-1。

I just love all those little inconsistencies in Excel.. :)

我只是喜欢Excel中所有那些不一致的东西.. :)

#2


2  

There is no direct mechanism for hiding the most recently used file listing. It can be done, however, by setting the Application.RecentFiles.Maximum to zero (0).

没有用于隐藏最近使用的文件列表的直接机制。但是,可以通过将Application.RecentFiles.Maximum设置为零(0)来完成。

For a detailed discussion, see Change the Ribbon in Excel 2007 by Ron de Bruin, and scroll down to the section titled "Dictator examples and Hide the MRU ('Most Recently Used') file list", with code provided by Jim Rech.

有关详细讨论,请参阅Ron de Bruin在Excel 2007中更改功能区,并向下滚动到标题为“Dictator示例和隐藏MRU('最近使用的')文件列表”的部分,其中包含由Jim Rech提供的代码。

This can also be done manually. See: How to Clear and Delete Recent Documents List in Office 2007 (Word, Excel, PowerPoint).

这也可以手动完成。请参阅:如何在Office 2007中清除和删除最近的文档列表(Word,Excel,PowerPoint)。

-- Mike

- 迈克

#3


0  

To clear the activeworkbook from the list of recentfiles use this:

要从recentfiles列表中清除activeworkbook,请使用以下命令:

Sub DeleteFileFromRecentFiles()
    Dim i As Integer
    For Each RecentFile In Application.RecentFiles
        If ActiveWorkbook.Name = RecentFile.Name Then
            i = i + 1
            Application.RecentFiles.Item(i).Delete
        End If
    Next
End Sub

Sincerely, Richard

理查德