30.04.2017 Views

4523756273

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

246<br />

Part IV: Communicating with Your Users<br />

Sub GetImportFileName ()<br />

Dim Finfo As String<br />

Dim FilterIndex As Integer<br />

Dim Title As String<br />

Dim FileName As Variant<br />

‘ Set up list of file filters<br />

FInfo = “Text Files (*.txt),*.txt,” & _<br />

“Lotus Files (*.prn),*.prn,” & _<br />

“Comma Separated Files (*.csv),*.csv,” & _<br />

“ASCII Files (*.asc),*.asc,” & _<br />

“All Files (*.*),*.*”<br />

‘ Display *.* by default<br />

FilterIndex = 5<br />

‘ Set the dialog box caption<br />

Title = “Select a File to Import”<br />

‘ Get the filename<br />

FileName = Application.GetOpenFilename (FInfo, _<br />

FilterIndex, Title)<br />

‘ Handle return info from dialog box<br />

If FileName = False Then<br />

MsgBox “No file was selected.”<br />

Else<br />

MsgBox “You selected “ & FileName<br />

End If<br />

End Sub<br />

Figure 15-7 shows the dialog box Excel displays when you execute this procedure.<br />

In a real application, you would do something more meaningful with the<br />

filename. For example, you might want to open it by using a statement such<br />

as this:<br />

Workbooks.Open FileName<br />

Notice that the FileName variable is declared as a Variant data type. If the<br />

user clicks Cancel, that variable contains a Boolean value (False). Otherwise,<br />

FileName is a string. Therefore, using a Variant data type handles both possibilities.<br />

By the way, the dialog box may look different, depending on which version of<br />

Windows you use.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!