View Single Post
  #8   Report Post  
Posted to microsoft.public.excel.programming
Rich Locus Rich Locus is offline
external usenet poster
 
Posts: 74
Default Can I display an open file dialog from a macro

Gary:

I added one minor addition to your Open Dialogue box to actually open the
file. Your code works great.

Public Sub FileDialogExamples()
'
Dim strPick As String
Dim strWorkbookOnlyName As String
Dim strWorkbookFullPathAndName As String

On Error GoTo err_Sub

'- - - - - - - - - - - - - - - - - - - -
'Allow user to Open file(s)
With Application.FileDialog(msoFileDialogOpen)
.AllowMultiSelect = False 'allow only one file to be chosen
.InitialFileName = Left(ActiveWorkbook.FullName, _
Len(ActiveWorkbook.FullName) - Len(ActiveWorkbook.Name))
.Title = "This is a test of File Selecting..."
.Filters.Clear
.Filters.Add "Excel Files", "*.xls", 1
.Filters.Add "Text Files", "*.csv, *.txt, *.prn", 2
.Filters.Add "ALL Files", "*.*", .Filters.Count + 1
.FilterIndex = 1
.Show
strPick = .SelectedItems(1)
End With
' MsgBox strPick

Workbooks.Open strPick
strWorkbookFullPathAndName = ActiveWorkbook.FullName
strWorkbookOnlyName = ActiveWorkbook.Name

--
Rich Locus
Logicwurks, LLC


"Gary Brown" wrote:

'/==========================================/
' Sub Purpose: Examples of using the FileDialog
' msoFileDialogOpen = 1
' msoFileDialogSaveAs = 2
' msoFileDialogFilePicker = 3
' msoFileDialogFolderPicker = 4
' Note: ONLY difference between
' 'msoFileDialogFilePicker' and 'msoFileDialogOpen'
' is that FilePicker button says 'OK' while
' FileDialogOpen button says 'Open'
'
Public Sub FileDialogExamples()
'
Dim strPick As String

On Error GoTo err_Sub

'- - - - - - - - - - - - - - - - - - - -
'Allow user to select file(s)
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False 'allow only one file to be chosen
.InitialFileName = Left(ActiveWorkbook.FullName, _
Len(ActiveWorkbook.FullName) - Len(ActiveWorkbook.name))
.Title = "This is a test of File Picking..."
.Filters.Clear
.Filters.Add "Excel Files", "*.xls", 1
.Filters.Add "Text Files", "*.csv, *.txt, *.prn", 2
.Filters.Add "ALL Files", "*.*", .Filters.Count + 1
.FilterIndex = 1
If .Show = False Then
GoTo exit_Sub
End If
strPick = .SelectedItems(1)
End With
MsgBox strPick

'- - - - - - - - - - - - - - - - - - - -
'Allow user to Open file(s)
With Application.FileDialog(msoFileDialogOpen)
.AllowMultiSelect = False 'allow only one file to be chosen
.InitialFileName = Left(ActiveWorkbook.FullName, _
Len(ActiveWorkbook.FullName) - Len(ActiveWorkbook.name))
.Title = "This is a test of File Selecting..."
.Filters.Clear
.Filters.Add "Excel Files", "*.xls", 1
.Filters.Add "Text Files", "*.csv, *.txt, *.prn", 2
.Filters.Add "ALL Files", "*.*", .Filters.Count + 1
.FilterIndex = 1
.Show
strPick = .SelectedItems(1)
End With
MsgBox strPick

'- - - - - - - - - - - - - - - - - - - -
'Allow user to select folder(s)
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False 'not applicable w/folders
.InitialFileName = Left(ActiveWorkbook.FullName, _
Len(ActiveWorkbook.FullName) - Len(ActiveWorkbook.name))
.Title = "This is a test of Folder Picking..."
.Show
strPick = .SelectedItems(1)
End With
MsgBox strPick

'- - - - - - - - - - - - - - - - - - - -
'Allow user to save current file
With Application.FileDialog(msoFileDialogSaveAs)
.Title = "This is a test of File Save As..."
.InitialFileName = Application.ActiveWorkbook.name
.Show
strPick = .SelectedItems(1)
End With
MsgBox strPick

'- - - - - - - - - - - - - - - - - - - -

exit_Sub:
On Error Resume Next
Exit Sub

err_Sub:
Debug.Print "Error: " & Err.Number & " - (" & _
Err.Description & _
") - Sub: FileDialogExamples - " & Now()
GoTo exit_Sub
End Sub
'/==========================================/

--
Hope this helps.
If it does, please click the Yes button.
Thanks in advance for your feedback.
Gary Brown



" wrote:

I would like to have a macro display one of the standard open file dialogs.
When the user selects a file and clicks "Open" (or whatever) I would like
to have the file name returned to the macro so it can use it.

Thanks so much for any help.
.