Remove a file from recent files list

K

Kathy Webster

How can I remove "c:\blue\test.doc" from the Word recent files list using
VB?
 
H

Helmut Weber

Hi Kathy,

something along these lines.

Sub Remove()
Dim x As Long
With Application
For x = 1 To .RecentFiles.Count
If .RecentFiles(x).Path = "C:\blue" Then
If .RecentFiles(x).Name = "test.doc" Then
Stop ' for testing
.RecentFiles(x).Delete
End If
End If
Next
End With
End Sub


--

Gruß

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
S

Steve Yandl

Kathy,

The routine below may do what you want. I don't know of a way to refresh
the list without shutting down Word and starting up again but this will
insure it is off the list when Word starts again.

Sub KillARecentFileRef()

Dim oRecFile As RecentFile

If RecentFiles.Count >= 1 Then
For Each oRecFile In RecentFiles
If oRecFile.Path = "c:\blue" And oRecFile.Name = "test.doc" Then
RecentFiles(oRecFile.Index).Delete
End If
Next oRecFile
Set oRecFile = Nothing
End If

End Sub



Steve Yandl
 
K

Kathy Webster

Thanks, but I should have mentioned I'm actually initiating this from a
MSAccess module. When I put your code in the module and compile it, I get
"User-defined type not defined", and
Dim oRecFile As RecentFile
is highlighted.
 
H

Helmut Weber

Hi Kathy,

Sub test445()
Dim oWrd As Object
' Dim oWrd as Word.Application
' doesn't work here and now for unknown reasons
' though all references are set
Dim x As Long
Set oWrd = GetObject(, "Word.Application")
' Word already running

With oWrd
For x = .RecentFiles.Count To 1 Step -1 '!
' I missed that in my first posting
If .RecentFiles(x).Path = "C:\blue" Then
If .RecentFiles(x).Name = "test1.doc" Then
.RecentFiles(x).Delete
End If
End If
Next
End With

End Sub

If you prefer Steve's code,
you would have to tell Access, that recentfile
is a Word-object, like
Dim oRecFile as oWrd.recentfile
whereby oWrd would be Word.Application.

However, nothing I've tried worked with recentfile.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top