Open A File Requested By The User

Instead of specifying the file to open in your code, you can retrieve the name of the file by prompting the user with an Open dialog box. To display an Open dialog box from an Excel macro, you use the GetOpenFilename method.

When you use this method, the file selected in the Open dialog box does not open when the user clicks OK. The dialog box passes the name of the file selected back to the variable that receives the statement assignment. If you want to open the selected file, you need to use the Open property.

The GetOpenFilename includes several optional parameters that you can specify to customize the Open dialog box. You can use the FileFilter parameter to allow the user to specify the type of files to open from the dialog box. You specify the file type by listing a value in the Files of Type drop-down box of the Open dialog box.

For example, "XML Files (*.xml) " specifies that Excel should only open XML files. You can specify multiple file types as long as you separate each one by a comma.

You can use the FilterIndex parameter to indicate the default file filtering option. Your choice of parameter depends on what you selected for the FileFilter parameter. You specify a filter value between 1 and the number of filters you selected. If you omit this parameter, VBA uses the first filter specified as the default value.

Use the Title parameter to customize the name of the dialog box. For example, if you want the dialog box to open a text file, you can change the title of the dialog box to "Open Text File".

If you want to select and open multiple files at once, specify a value of True for the MultiSelect parameter.

OPEN A FILE REQUESTED BY THE USER

OPEN A FILE REQUESTED BY THE USER

Open File Requested User With Python

'—n Create a new subroutine

0 Type Dim UserFile As Variant, replacing UserFile with the variable to receive name of file to open.

< Type UserFile = Application. GetOpenFilename().

S Type Title:="Dialog box text" within the parentheses, replacing Dialog box text with the text for header of the dialog box.

'—n Create a new subroutine

0 Type Dim UserFile As Variant, replacing UserFile with the variable to receive name of file to open.

< Type UserFile = Application. GetOpenFilename().

'—Q Type FileFilter:="Text Files (*.txt), *.txt" within the parentheses to specify type of file to open.

S Type Title:="Dialog box text" within the parentheses, replacing Dialog box text with the text for header of the dialog box.

+1 0

Post a comment