# Function Reference

# _WinAPI_GetOpenFileName

Creates an Open dialog box that lets the user specify the drive, directory, and the name of a file or set of files to open

#include <WinAPIDlg.au3>
_WinAPI_GetOpenFileName ( [$sTitle = "" [, $sFilter = "All files (*.*)" [, $sInitalDir = "." [, $sDefaultFile = "" [, $sDefaultExt = "" [, $iFilterIndex = 1 [, $iFlags = 0 [, $iFlagsEx = 0 [, $hWndOwner = 0]]]]]]]]] )

## Parameters

$sTitle | [optional] string to be placed in the title bar of the dialog box
$sFilter | [optional] Pairs of filter strings (for example "Text Files (*.txt)|All Files (*.*)") The first string in each pair is a display string that describes the filter (for example, "Text Files") The second string specifies the filter pattern (for example, "*.TXT") To specify multiple filter patterns for a single display string, use a semicolon to separate the patterns (for example, "*.TXT;*.DOC;*.BAK") A pattern string can be a combination of valid file name characters and the asterisk (*) wildcard character Do not include spaces in the pattern string.
$sInitalDir | [optional] String that can specify the initial directory
$sDefaultFile | [optional] A file name used to initialize the File Name edit control
$sDefaultExt | [optional] String that contains the default extension
$iFilterIndex | [optional] Specifies the index of the currently selected filter in the File Types control
$iFlags | [optional] See Flags in $tagOPENFILENAME information
$iFlagsEx | [optional] See FlagEx in $tagOPENFILENAME information
$hWndOwner | [optional] Handle to the window that owns the dialog box. This member can be any valid window handle, or it can be 0 if the dialog box has no owner

## Return Value

Success: | an array in the following format: [0] - Contains the number of strings [1] - Contains the path selected [2] - Contains file selected [n] - Contains file selected
Failure: | sets the @error flag to non zero, call _WinAPI_CommDlgExtendedError() to get extended error information.

## Related

$tagOPENFILENAME, _WinAPI_CommDlgExtendedError, _WinAPI_GetSaveFileName

## See Also

Search GetOpenFileName in MSDN Library.

## Example

    #include "Extras\HelpFileInternals.au3"

    #include <GUIConstantsEx.au3>
    #include <WinAPIConstants.au3>
    #include <WinAPIDlg.au3>
    #include <WindowsStylesConstants.au3>

    _Example_Defaults()
    _Example_ExplorerStyleMultiSelect()
    _Example_OldStyle()
    _Example_ExplorerStyleSingleSelect()
    _Example_ExplorerStyle_NoPlaceBar()

    Func _Example_Defaults()
        Local $hGui, $idBtn_Dialog, $aFile, $sError

        ; Create GUI
        $hGui = GUICreate("GetOpenFileName use defaults", 400, 296)
        _MemoCreate(2, 32, 396, 226, $WS_HSCROLL)
        $idBtn_Dialog = GUICtrlCreateButton("Open Dialog", 155, 270, 90, 20)
        GUISetState(@SW_SHOW)

        While 1
            Switch GUIGetMsg()
                Case $idBtn_Dialog
                    $aFile = _WinAPI_GetOpenFileName() ; use defaults
                    If @error Then
                        $sError = _WinAPI_CommDlgExtendedError()
                        _MemoWrite("CommDlgExtendedError (" & @error & "): " & $sError)
                    Else
                        For $x = 1 To $aFile[0]
                            _MemoWrite($aFile[$x])
                        Next
                    EndIf
                Case $GUI_EVENT_CLOSE
                    ExitLoop
            EndSwitch
        WEnd
        GUIDelete($hGui)
    EndFunc   ;==>_Example_Defaults

    Func _Example_ExplorerStyleMultiSelect()
        Local $hGui, $idBtn_Dialog, $aFile, $sError

        ; Create GUI
        $hGui = GUICreate("GetOpenFileName use Explorer Style (Multi Select)", 400, 296)
        _MemoCreate(2, 32, 396, 226, $WS_HSCROLL)
        $idBtn_Dialog = GUICtrlCreateButton("Open Dialog", 155, 270, 90, 20)
        GUISetState(@SW_SHOW)

        While 1
            Switch GUIGetMsg()
                Case $idBtn_Dialog
                    $aFile = _WinAPI_GetOpenFileName("My Open File Dialog", _
                            "Text File (*.txt;*.au3)", ".", @ScriptName, "", 1, _
                            BitOR($OFN_ALLOWMULTISELECT, $OFN_EXPLORER), 0, $hGui)
                    If @error Then
                        $sError = _WinAPI_CommDlgExtendedError()
                        _MemoWrite("CommDlgExtendedError (" & @error & "): " & $sError)
                    Else
                        For $x = 1 To $aFile[0]
                            _MemoWrite($aFile[$x])
                        Next
                    EndIf
                Case $GUI_EVENT_CLOSE
                    ExitLoop
            EndSwitch
        WEnd
        GUIDelete($hGui)
    EndFunc   ;==>_Example_ExplorerStyleMultiSelect

    Func _Example_OldStyle()
        Local $hGui, $idBtn_Dialog, $aFile, $sError

        ; Create GUI
        $hGui = GUICreate("GetOpenFileName use Old Style (Multi Select)", 400, 296)
        _MemoCreate(2, 32, 396, 226, $WS_HSCROLL)
        $idBtn_Dialog = GUICtrlCreateButton("Open Dialog", 155, 270, 90, 20)
        GUISetState(@SW_SHOW)

        While 1
            Switch GUIGetMsg()
                Case $idBtn_Dialog
                    $aFile = _WinAPI_GetOpenFileName("My Open File Dialog", _
                            "Text File (*.txt)|AutoIt File (*.au3)", ".", @ScriptName, _
                            "", 2, $OFN_ALLOWMULTISELECT, 0, $hGui)
                    If @error Then
                        $sError = _WinAPI_CommDlgExtendedError()
                        _MemoWrite("CommDlgExtendedError (" & @error & "): " & $sError)
                    Else
                        For $x = 1 To $aFile[0]
                            _MemoWrite($aFile[$x])
                        Next
                    EndIf
                Case $GUI_EVENT_CLOSE
                    ExitLoop
            EndSwitch
        WEnd
        GUIDelete($hGui)
    EndFunc   ;==>_Example_OldStyle

    Func _Example_ExplorerStyleSingleSelect()
        Local $hGui, $idBtn_Dialog, $aFile, $sError

        ; Create GUI
        $hGui = GUICreate("GetOpenFileName use Explorer Style (Single Select)", 400, 296)
        _MemoCreate(2, 32, 396, 226, $WS_HSCROLL)
        $idBtn_Dialog = GUICtrlCreateButton("Open Dialog", 155, 270, 90, 20)
        GUISetState(@SW_SHOW)

        While 1
            Switch GUIGetMsg()
                Case $idBtn_Dialog
                    $aFile = _WinAPI_GetOpenFileName("My Open File Dialog", _
                            "Text File (*.txt)|AutoIt File (*.au3)", ".", @ScriptName, _
                            "", 2, 0, 0, $hGui)
                    If @error Then
                        $sError = _WinAPI_CommDlgExtendedError()
                        _MemoWrite("CommDlgExtendedError (" & @error & "): " & $sError)
                    Else
                        For $x = 1 To $aFile[0]
                            _MemoWrite($aFile[$x])
                        Next
                    EndIf
                Case $GUI_EVENT_CLOSE
                    ExitLoop
            EndSwitch
        WEnd
        GUIDelete($hGui)
    EndFunc   ;==>_Example_ExplorerStyleSingleSelect

    Func _Example_ExplorerStyle_NoPlaceBar()
        Local $hGui, $idBtn_Dialog, $aFile, $sError

        ; Create GUI
        $hGui = GUICreate("GetOpenFileName use Explorer Style (Single Select)", 400, 296)
        _MemoCreate(2, 32, 396, 226, $WS_HSCROLL)
        $idBtn_Dialog = GUICtrlCreateButton("Open Dialog", 155, 270, 90, 20)
        GUISetState(@SW_SHOW)

        While 1
            Switch GUIGetMsg()
                Case $idBtn_Dialog
                    $aFile = _WinAPI_GetOpenFileName("My Open File Dialog", _
                            "Text File (*.txt)|AutoIt File (*.au3)", ".", @ScriptName, _
                            "", 2, $OFN_EXPLORER, $OFN_EX_NOPLACESBAR, $hGui)
                    If @error Then
                        $sError = _WinAPI_CommDlgExtendedError()
                        _MemoWrite("CommDlgExtendedError (" & @error & "): " & $sError)
                    Else
                        For $x = 1 To $aFile[0]
                            _MemoWrite($aFile[$x])
                        Next
                    EndIf
                Case $GUI_EVENT_CLOSE
                    ExitLoop
            EndSwitch
        WEnd
        GUIDelete($hGui)
    EndFunc   ;==>_Example_ExplorerStyle_NoPlaceBar