Why is the error in this code?

A

avkokin

Hello. I use very simple macro (below), but I get error "Run-Time
error '5152'. Method 'Name' of object 'RecentFile" failed." Why? If I
use "On error resume next" the all correct, but it represented only 7
or 8 recent files but not 9. Strangely...
Sub rflist2()
Dim j As Long
Dim MyList As String
'On Error Resume Next
For j = 1 To Application.RecentFiles.Count
MyList = MyList & Application.RecentFiles(j) & vbCrLf
Next
MsgBox MyList
End Sub

Thank you
 
M

macropod

Hi avkokin,

I wonder if the problem is due to one or more of the files having been opened from a zip archive? On my system (Vista with Word
2000), such files don't appear in the macro's output even though they appear in Word's recent file list.

You could try something along the lines of:
Sub rflist()
Dim j As Long
Dim rFile As RecentFile
Dim rList As String
rList = "Recent Files" & vbCrLf
For Each rFile In Application.RecentFiles
j = j + 1
rList = rList & j & ". " & rFile.Path & "\" & rFile.Name & vbCrLf
Next
MsgBox rList
End Sub
 
A

avkokin

Thank you, but if I not use 'On error resume next' I get this error
'5152' all the same. Though it views all 9 recent files now.
 
M

macropod

Hi avkokin,

I have no idea why you're getting an error - the code I posted works just fine on my system.
 
A

avkokin

Hi.
I understood why I got error. Into list of recent files was one
document which was deleted earlier.
And I have one question: how to define existence of the files from the
list of recent files?
Thank you.
 
M

macropod

Hi avkokin,

You could use something like:
Sub rflist()
Dim j As Long
Dim rFile As RecentFile
Dim rList As String
Dim rFound As String
rList = "Recent Files" & vbCrLf
rFound = "Found Files" & vbCrLf
For Each rFile In Application.RecentFiles
j = j + 1
rList = rList & j & ". " & rFile.Path & "\" & rFile.Name & vbCrLf
If InStr(Dir(rFile.Path & "\" & rFile.Name), ".doc") > 0 Then _
rFound = rFound & rFile.Path & "\" & rFile.Name & vbCrLf
Next
MsgBox rList
MsgBox rFound
End Sub
 

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