Solved

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

Posted on 2008-06-11
9
622 Views
Last Modified: 2012-08-13
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

0
Comment
Question by:guxiyou
  • 3
  • 3
  • 2
9 Comments
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 400 total points
ID: 21764429
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.
0
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 100 total points
ID: 21764616
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")


0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 21764670
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.
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 21764788
" ... 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 :))
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:guxiyou
ID: 21764813
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.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 21764834
guxiyou,
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.
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 21765012
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 :)
0
 

Author Closing Comment

by:guxiyou
ID: 31466327
Thanks, your comments were very helpful and exactly what I was looking for.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now