Results 1 to 3 of 3

Thread: Help using OPENFILENAME

  1. #1
    Guest

    Question

    I have a Button called btnBrowse1 & a text box called txtTEST1. I also have the code below in a seperate module, to be used by multiple forms. How do I call the code below when I click btnBrowse1 & have the selected file & pathname appear in txtTEST1?

    Part 2 of the question where is there an exampe of the different types of file extension so that I can search for more than just .txt & .rtf files?

    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

    Public Function ShowFileDialog(hWnd As Long)
    Dim ofn As OPENFILENAME
    ofn.lStructSize = Len(ofn)
    ofn.hwndOwner = 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 = "Our File Open Title"
    ofn.flags = 0
    Dim lngRetVal As Long
    lngRetVal = GetOpenFileName(ofn)

    End Function

    TIA

  2. #2
    Guest
    Q1 Answer:

    Code:
    Public Function ShowFileDialog(hWnd As Long) 
        Dim ofn As OPENFILENAME 
        ofn.lStructSize = Len(ofn) 
        ofn.hwndOwner = 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 = "Our File Open Title" 
        ofn.flags = 0 
        'Remove --> Dim lngRetVal As Long 
        'change --> lngRetVal = GetOpenFileName(ofn) -->
        'To the following ling of code:
        ShowFileDialog = GetOpenFileName(ofn)
    End function
    And the usage:

    Code:
        varname = ShowFileDialog(ofn)
    [/quote]

    Q2:

    With the new technology developing, we never know what extensions are used now and then, so uh...

  3. #3
    New Member
    Join Date
    Sep 2000
    Posts
    4

    Sorry about miscoding.

    Well, forget the [ofn], and replace that with hWnd.
    Brief, for all our sakes, turn off that darn VB!

    Escaflowne's Omega Helper.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width