Application Object Example

This example demonstrates how to use Application.GetOpenFilename to get the name of a file to open. The key to using this function is to assign its return value to a Variant data type:

Sub UsingGetOpenFilename() Dim sFilter As String Dim vaFile As Variant 'Build a filter list.

sFilter = "Excel 2007 Files,*.xlsx," & _ "Excel 2000-2003 Files,*.xls" 'Display the File Open dialog, putting the result in a Variant vaFile = Application.GetOpenFilename(FileFilter:=sFilter, FilterIndex:=1, _

Title:="Open a New or Old File", MultiSelect:=False) 'If user cancelled, then exit, else open the selected file. If vaFile <> False Then

Workbooks.Open Filename:=vaFile Else

MsgBox "Action Cancelled" End If End Sub

0 0

Post a comment