Visual Basic Win32 Shell Routines
SHAddToRecentDocs: Add Entires to Recent Documents List
     
Posted:   Sunday July 4, 1997
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB4-32, Windows 95
OS restrictions:   None
Author:   VBnet - Randy Birch
     
 Prerequisites
None.

Certain features have come to be expected in a Windows application. One of these is an application automatically adding an entry into the the Recent Files ("Documents") list whenever a file is opened or saved. Using the simple code below and the SHAddFilesToRecentDocs API, your app can perform this feat as well. The actual call is a one-liner.

The SHAddFilesToRecentDocs API takes as a parameter either the pidl of the file opened, or more simply, the path and filename as a string. The API creates a shortcut from the information, depositing it automatically into the 'Documents' or 'Recent Documents' menu. As with most APIs, you tell it what to expect to find in shData by specifying one of the two available flags as the shFlags member. In addition, by passing VB's null string (or 0&, if declared appropriately) as shData, the call to SHAddFilesToRecentDocs will clear out all entries in the list.

One caveat ... SHAddFilesToRecentDocs doesn't care if the file or path actually exist (at least if passing a string .. I didn't try with a pidl), so it is up to your app to pass qualified filenames.
 BAS Module Code
None.

 Form Code
To a project form add a control array for two option buttons (Option1(0) and Option1(1)), and a textbox (Text1),as indicated in the illustration. Add the following to the form:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Const SHARD_PATH = &H2&

Private Declare Function SHAddToRecentDocs Lib "shell32" _
  (ByVal dwFlags As Long, _
   ByVal dwData As String) As Long


Private Sub Form_Load()

   Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2

End Sub


Private Sub Command1_Click()

   Dim shFlag As Long
   Dim shData As String
  
   shFlag = SHARD_PATH

   Select Case Option1(0).Value = True
      Case True:
       
        'We're about to add an item, so
        'set the 'file name' from Text1    
         shData = Text1.Text

      Case False:   
          
        'User selected delete.
        'Pass a null string as shData   
         shData = vbNullString
     
   End Select
 
  'update or clear the list    
   Call SHAddToRecentDocs(shFlag, shData)
  
End Sub
 Comments
Run the project, enter a file path and name into the textbox, select an option and hit Do It. Then, check the Documents menu item to see the file in the list. It may take a second or so for it to show up.

Windows takes care of "bumping" out older items as entries are added to the list. In addition, as the shortcut created uses the filename specified in the textbox (without the path), you can't have more than one entry with the same name, regardless if the path is different.

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter