January 21, 2021
Hot Topics:

Anything to declare?

  • By Sam Huggill
  • Send Email »
  • More Articles »

The first, and most used function is the Open File dialog. Just add the following code to a form's General Declarations procedure (making sure that you have a command button on the form) and you should be able to call the open file dialog:

  lStructSize As Long 
  hwndOwner As Long 
  hInstance As Long 
  lpstrFilter As String 
  lpstrCustomFilter As String 
  nMaxCustFilter As Long 
  nFilterIndex As Long 
  lpstrFile As String 
  nMaxFile As Long 
  lpstrFileTitle As String 
  nMaxFileTitle As Long 
  lpstrInitialDir As String 
  lpstrTitle As String 
  flags As Long 
  nFileOffset As Integer 
  nFileExtension As Integer 
  lpstrDefExt As String 
  lCustData As Long 
  lpfnHook As Long 
  lpTemplateName As String 
End Type 
Private Declare Function_
  GetOpenFileName Lib_
  Alias "GetOpenFileNameA"_
  (pOpenfilename As OPENFILENAME)_
  As Long 
Private Sub Command1_Click() 
ofn.lStructSize = Len(ofn) 
ofn.hwndOwner = Form1.hWnd 
ofn.hInstance = App.hInstance 
ofn.lpstrFilter = "Text Files_
  (*.txt)" + Chr$(0) + "_
  *.txt" + Chr$(0) + "_
  Rich Text Files (*.rtf)"_
  + Chr$(0) + "*.rtf" + Chr$(0) 
ofn.lpstrFile = Space$(254) 
ofn.nMaxFile = 255 
ofn.lpstrFileTitle = Space$(254) 
ofn.nMaxFileTitle = 255 
ofn.lpstrInitialDir = CurDir 
ofn.lpstrTitle = "Dialog Title" 
ofn.flags = 0 
Dim a 
a = GetOpenFileName(ofn) 
If (a) Then 
  MsgBox "File to Open: "_
   + Trim$(ofn.lpstrFile) 
  'Do the file open stuff here 
  MsgBox "Cancel was pressed" 
End If 
End Sub

Page 2 of 4

This article was originally published on November 20, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date