View Single Post
  #7   Report Post  
Posted to microsoft.public.excel.programming
Geoff Budd Geoff Budd is offline
external usenet poster
 
Posts: 10
Default Macro to delete "unpinned" recent files

Thanks everybody. I've ended up with the following code, which effectively
just counts backwards, deleting as it goes, and that seems to work.
By the way, the reason I test for [F00000002] is that this indicates a
read-only file that is not pinned - so to catch them all, I have to delete
all those that are prefixed by [F00000000] or [F00000002].
This also works for Word 2007 if you replace "\12.0\Excel\File MRU\" by
"\12.0\Word\File MRU\" in the "RegKey=" statement.
Thanks again - you're all stars!
Geoff

Sub ClearMRU_NotPinned()
Dim X, OriginalMax, NumberOfRecentFiles
Dim RegKey, rKeyWord, WSHShell
OriginalMax = Application.RecentFiles.Maximum
Application.RecentFiles.Maximum = 50
Set WSHShell = CreateObject("WScript.Shell")
RegKey = "HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\ Excel\File MRU\"
NumberOfRecentFiles = Application.RecentFiles.Count ' (original total file
count)
For X = NumberOfRecentFiles To 1 Step -1
rKeyWord = WSHShell.RegRead(RegKey & "Item " &
Application.RecentFiles(X).Index)
If InStr(1, rKeyWord, "[F00000000]") Then
Application.RecentFiles(X).Delete
End If
If InStr(1, rKeyWord, "[F00000002]") Then
Application.RecentFiles(X).Delete
End If
Next X
Application.RecentFiles.Maximum = OriginalMax
End Sub

"Mike H" wrote:

Hi,

Further testing and I agree your right, well spotted. 50 does seem to be the
max in E2007 so the modified version of my code is below. Thanks for pointing
that out, as I said in my first post with regards to the registry I'm an
absolute novice.

Sub ClearMRU_NotPinned()
On Error Resume Next
OriginalMax = Application.RecentFiles.Maximum
Application.RecentFiles.Maximum = 50
Dim delfiles()
Max = Application.RecentFiles.Count
ReDim delfiles(1 To Max)
Dim WSHShell, RegKey, rKeyWord
Set WSHShell = CreateObject("WScript.Shell")
RegKey = "HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\ Excel\File MRU\"
For X = 1 To Application.RecentFiles.Count
rKeyWord = WSHShell.RegRead(RegKey & "Item " &
Application.RecentFiles(X).Index)
If InStr(1, rKeyWord, "[F00000000]") Then
delfiles(X) = Application.RecentFiles(X).Index
End If
Next
For X = UBound(delfiles()) To 1 Step -1
IsPinned = Application.WorksheetFunction.Match(X, delfiles(), 0)
If IsPinned = "" Then
IsPinned = ""
Else
Application.RecentFiles(X).Delete
IsPinned = ""
End If
Next
Application.RecentFiles.Maximum = OriginalMax
End Sub


Mike

"p45cal" wrote:


If I'm right about Application.RecentFiles.Count (this value is the
smaller of the number of entries in the registry and the
Application.RecentFiles.Maximum (this last is the -Number of Recent
files to show-)) being possibly smaller than the number of entries in
the registry, then code so far put forward will only delete those
visible in the Office Button list; others in the registry will move up
and become visible in the Office Button list. So we could temporarily
set the -Number of Recent files to show- to its maximum of 50 and put it
back to its previous setting afterwards:

OriginalMax = Application.RecentFiles.Maximum
Application.RecentFiles.Maximum = 50
'do the main processing here
Application.RecentFiles.Maximum = OriginalMax

Now as long as there are never more than 50 entries in the registry
(can there be?) it should never have to be run more than once.


--
p45cal

*p45cal*
------------------------------------------------------------------------
p45cal's Profile: http://www.thecodecage.com/forumz/member.php?userid=558
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=147510

.