Coyote Level: Guest

|
Re: Recent Docs in File Menu Archived to Disk
OK-- Yow weee.. let me try this. This is one way to get recent files in your application. Suspect you can understand just by looking at the code, but if you want to build an example project to test this... Then
Open a new project: add a module named API and copy/paste the following code:
' MODULE CODE STARTS
Option Compare Text
Option Explicit
' We Declare GetOpenFileName
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
"GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
' Constants used by GetOpenFileName
Private Const OFN_LONGNAMES = &H200000
Private Const OFN_CREATEPROMPT = &H2000 ' prompt to overwrite
Private Const OFN_NODEREFERENCELINKS = &H100000
' OpenFileName Type is used by GetOpenFileName API
Private Type OPENFILENAME
lStructSize As Long ' size of type/structure
hwndOwner As Long ' Handle of owner window
hInstance As Long
lpstrFilter As String ' Filters used to select files
lpstrCustomFilter As String
nMaxCustomFilter As Long
nFilterIndex As Long ' index of Filter to start with
lpstrFile As String ' Holds filepath and name
nMaxFile As Long ' Maximum Filepath and name length
lpstrFileTitle As String ' Filename
nMaxFileTitle As Long ' Max Length of filename
lpstrInitialDir As String ' Starting Directory
lpstrTitle As String ' Title of window
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
'@=========================================================================
' GetOpenName:
' Prompts user for a file to open.
' Parameters:
'
' Returns: Filename and path user selects
'==========================================================================
Function GetOpenName(Optional ByVal WindowTitle As String = "Load File", _
Optional ByVal Filters As String = "All Files" + vbNullChar + "*.*", _
Optional ByVal DefaultFileName As String = "")
Dim RET As Long ' Return values
Dim DlgInfo As OPENFILENAME ' Dim our type for Dialog setup
' Setup the way the dialog looks and acts
With DlgInfo
' set size of structure/type
.lStructSize = Len(DlgInfo)
' Set Parent window
.hwndOwner = Screen.ActiveForm.hWnd
' What files to show
.lpstrFilter = Filters
' Set Filter index
.nFilterIndex = 1
' Set the initial filename/path leaving space left over incase user selects larger
.lpstrFile = DefaultFileName & Space$(1024) & vbNullChar & vbNullChar
' set max len of filename/path
.nMaxFile = Len(.lpstrFile)
' whether to apply an extension if non is supplie
.lpstrDefExt = vbNullChar & vbNullChar
' Spacre for filename
.lpstrFileTitle = vbNullChar & Space$(512) & vbNullChar & vbNullChar
.nMaxFileTitle = Len(.lpstrFileTitle)
'starting folder, double-null terminated
.lpstrInitialDir = CurDir + vbNullChar
' Set the window title
.lpstrTitle = WindowTitle
' Set open Flags
.flags = OFN_LONGNAMES Or OFN_CREATEPROMPT Or OFN_NODEREFERENCELINKS
End With
' Get Filename from dialog
GetOpenName = GetOpenFileName(DlgInfo)
' remove unneeded chars
GetOpenName = Left(DlgInfo.lpstrFile, InStr(DlgInfo.lpstrFile, vbNullChar) - 1)
End Function
' MODULE CODE ENDS
NEXT ~~~~
Put a "File" menu on your form with "Open" (MnuOpen)and "Exit" (mnuExit)under File.
Copy and paste the following code to your form...
' Form1 CODE STARTS
Option Compare Text
Option Explicit
Const MaxRecentFiles = 6 'Max of files to show in list
'@================================================
' MnuOpen_Click:
' User Want to open a file, show dialog and add
' to recent files list.
'=================================================
Private Sub MnuOpen_Click()
Dim FileName As String ' hold filename
Dim A As Long ' general use
' get filename
FileName = API.GetOpenName("Recent File List Demo, Add to file list")
If FileName = "" Then Exit Sub
' if list is too long trim
If MnuRecentFiles.Count > MaxRecentFiles Then
' move items up one
For A = 2 To MnuRecentFiles.UBound
MnuRecentFiles(A - 1).Caption = MnuRecentFiles(A).Caption
Next A
' unload last item
Unload MnuRecentFiles(MnuRecentFiles.UBound)
End If ' end if list is too long trim
' load new menu item
A = MnuRecentFiles.UBound + 1
Load MnuRecentFiles(A)
' set caption
MnuRecentFiles(A).Caption = FileName
End Sub
'@=====================================================
' MnuRecentFiles_Click()
' Show filename that user clicked
'======================================================
Private Sub MnuRecentFiles_Click(Index As Integer)
MsgBox ("You Selected file:" + MnuRecentFiles(Index).Caption)
End Sub
'@=====================================================
' MnuExit:
' Use clicked exit in menu so end program
'======================================================
Private Sub mnuExit_Click()
End
End Sub
' ~~~~~ FORM1 CODE ENDS ~~~~
Hope the code formating does not get screwed when I post this ? Anyway - You can then go to Open and select a file - which will not actually open since this is a demo and the code for that is not here -- BUT - what ever you select will be added to the "File" menu as a recent file.
OK when I Preview this post the code has little winkie icons stuck in it, replace them with a ")" (no quotes). Poooo eee and it does it here.. Ok replace with just a: )
Shift&0
Hope this answers your question.
>>>Coyote<<<
|