How can I programmatically add a file to word's Recent Files?

I want to be able to remove items from the ApplicationClass.RecentFiles and then restore them.  I am able to remove them by playing with the RecentFiles.Maximum size, but thus far I have been unable to restore them.  I've included a code sample, and will award points for the first person to show me what I'm doing wrong in the simple example.

Thanks in advance!
private static Word.ApplicationClass wd = null; // Initialized elsewhere
private object[] recentFiles;
private int recentMax;
public void RemoveRecent()
    recentFiles = new object[wd.RecentFiles.Count];
    for (int i = 1; i <= wd.RecentFiles.Count; i++)
        recentFiles[i - 1] = (object)wd.RecentFiles[i];
    recentMax = wd.RecentFiles.Maximum;
    wd.RecentFiles.Maximum = 0;
public void RestoreRecent()
    object readOnly = true;
    wd.RecentFiles.Maximum = recentMax;
    for (int i = recentFiles.Length - 1; i > 0; i--)
        wd.RecentFiles.Add(ref recentFiles[i], ref readOnly);

Open in new window

Who is Participating?

Improve company productivity with a Business Account.Sign Up

anarki_jimbelConnect With a Mentor Commented:
I believe it's quite obvious what's wrong.

You copy a collection of recent files into some array. What's the type of a recent file object?
Ansewr: Word.RecentFile, isn't it?

Now, you are trying to add these objects back to recent files using "Add" function. What's the type of the first argument in Add? Yes, object, but I believe not any object, but, e.g.,  Word.Document. And not Word.RecentFile object. Word.RecentFile is return type for the method...

So, I believe, the method does not accept the object type  but does not throw an exception.

Hope this helps.
GrahamSkanConnect With a Mentor RetiredCommented:
This is VB syntax.

To simply add to the top of the list :

wd.RecentFiles.Add "C:\MyFile.doc"

Note that you can capture the new object into a variable if necessary:

    Dim rf As RecentFile
    Set rf = wd.RecentFiles.Add("C:\MyFile.doc")

Sorry. Hit the submit button before finishing.

The first parameter can be a Document object, but it can also be a string. The  code that I have shown does work in VBA.

The RecentFile object has Name property which  is, of course, a string, so you could use that.
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

" ... will award points for the first person to show me what I'm doing wrong "

I showed - and what!? :)

Be careful when you do promises! (You didn't promise any point for the solution :))
guxiyouAuthor Commented:
I looked back over my question, and you are correct. Is there a way I can re-award points? Or award to you as well? I guess I thought the need for the solution was implied.
You have picked my two comments as the answer and the assist. If that was an error and you meant to pick anarki_jimbel's in one of those capacities, you can ask in the support area for the question to be reopened and the points re-assigned. I will raise no objection.
No worries, guys - I'm just joking :)
I'm here for fun, and not crazy about point at all. Just a break at working place :)
guxiyouAuthor Commented:
Thanks, your comments were very helpful and exactly what I was looking for.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.