dcsimg
December 8, 2016
Hot Topics:

Anything to declare?

  • November 19, 2002
  • 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:

Private Type OPENFILENAME 
  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_
  "comdlg32.dll"_
  Alias "GetOpenFileNameA"_
  (pOpenfilename As OPENFILENAME)_
  As Long 
 
Private Sub Command1_Click() 
Dim ofn As OPENFILENAME 
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 
Else 
  MsgBox "Cancel was pressed" 
End If 
End Sub




Page 2 of 4



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

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

Sitemap | Contact Us

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