INTELLIGENT WORK FORUMS FOR COMPUTER PROFESSIONALS
Log In
Come Join Us!
Are you a Computer / IT professional? Join Tek-Tips Forums!
- Talk With Other Members
- Be Notified Of Responses
To Your Posts
- Keyword Search
- One-Click Access To Your
Favorite Forums
- Automated Signatures
On Your Posts
- Best Of All, It's Free!
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.
Posting Guidelines
Promoting, selling, recruiting, coursework and thesis posting is forbidden. Students Click Here
|
VBA Visual Basic for Applications (Microsoft) FAQ
VBA How To
Open File(s) using the GetOpenFilename dialog by Bowers74
Posted: 4 Sep 03 (Edited 8 May 04)
|
The following two procedures allow you to select a file (or multiple files) to open by using the GetOpenFilename dialog box:
To open a single file
CODESub OpenSingleFile() Dim Filter As String, Title As String Dim FilterIndex As Integer Dim Filename As Variant ' File filters Filter = "Excel Files (*.xls),*.xls," & _ "Text Files (*.txt),*.txt," & _ "All Files (*.*),*.*" ' Default Filter to *.* FilterIndex = 3 ' Set Dialog Caption Title = "Select a File to Open" ' Select Start Drive & Path ChDrive ("E") ChDir ("E:\Chapters\chap14") With Application ' Set File Name to selected File Filename = .GetOpenFilename(Filter, FilterIndex, Title) ' Reset Start Drive/Path ChDrive (Left(.DefaultFilePath, 1)) ChDir (.DefaultFilePath) End With ' Exit on Cancel If Filename = False Then MsgBox "No file was selected." Exit Sub End If ' Open File Workbooks.Open Filename MsgBox Filename, vbInformation, "File Opened" ' This can be removed End Sub To open multiple files (or a single file)
CODESub OpenMultipleFiles() Dim Filter As String, Title As String, msg As String Dim i As Integer, FilterIndex As Integer Dim Filename As Variant ' File filters Filter = "Excel Files (*.xls),*.xls," & _ "Text Files (*.txt),*.txt," & _ "All Files (*.*),*.*" ' Default filter to *.* FilterIndex = 3 ' Set Dialog Caption Title = "Select File(s) to Open" ' Select Start Drive & Path ChDrive ("E") ChDir ("E:\Chapters\chap14") With Application ' Set File Name Array to selected Files (allow multiple) Filename = .GetOpenFilename(Filter, FilterIndex, Title, , True) ' Reset Start Drive/Path ChDrive (Left(.DefaultFilePath, 1)) ChDir (.DefaultFilePath) End With ' Exit on Cancel If Not IsArray(Filename) Then MsgBox "No file was selected." Exit Sub End If ' Open Files For i = LBound(Filename) To UBound(Filename) msg = msg & Filename(i) & vbCrLf ' This can be removed Workbooks.Open Filename(i) Next i MsgBox msg, vbInformation, "Files Opened"' This can be removed End Sub I hope that you find this helpful! 
|
Back to VBA Visual Basic for Applications (Microsoft) FAQ Index
Back to VBA Visual Basic for Applications (Microsoft) Forum |
|
|
|
Join Tek-Tips® Today!
Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.
Here's Why Members Love Tek-Tips Forums:
Talk To Other Members
- Notification Of Responses To Questions
- Favorite Forums One Click Access
- Keyword Search Of All Posts, And More...
Register now while it's still free!
Already a member? Close this window and log in.
Join Us Close