Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 128
Default GetOpenFilename - Trapping Errors

Hi

I used the VB Example code from Microsoft Help but wanted to select MULTIPLE
files and have Option Explicit declared. The error trapping no longer worked
properly for no file being selected n the dialog box- can someone give me a
hand in getting it to work.

Thanks

Tim

Option Explicit
Sub ImportBudgetFileData()

Dim FileToOpen

FileToOpen = Application _
.GetOpenFilename("Text Files (*.xls), *.xls", , , , True)
'If FileToOpen(1) < False Then
' MsgBox "Open " & FileToOpen
'End If


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default GetOpenFilename - Trapping Errors

You can use

If IsArray(FileToOpen ) = False Then
'do nothing
Else


--
Regards Ron de Bruin
http://www.rondebruin.nl


"Tim Childs" wrote in message ...
Hi

I used the VB Example code from Microsoft Help but wanted to select MULTIPLE
files and have Option Explicit declared. The error trapping no longer worked
properly for no file being selected n the dialog box- can someone give me a
hand in getting it to work.

Thanks

Tim

Option Explicit
Sub ImportBudgetFileData()

Dim FileToOpen

FileToOpen = Application _
.GetOpenFilename("Text Files (*.xls), *.xls", , , , True)
'If FileToOpen(1) < False Then
' MsgBox "Open " & FileToOpen
'End If




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 128
Default GetOpenFilename - Trapping Errors

Ron

MANY thanks

Tim


"Ron de Bruin" wrote in message
...
You can use

If IsArray(FileToOpen ) = False Then
'do nothing
Else


--
Regards Ron de Bruin
http://www.rondebruin.nl


"Tim Childs" wrote in message

...
Hi

I used the VB Example code from Microsoft Help but wanted to select

MULTIPLE
files and have Option Explicit declared. The error trapping no longer

worked
properly for no file being selected n the dialog box- can someone give

me a
hand in getting it to work.

Thanks

Tim

Option Explicit
Sub ImportBudgetFileData()

Dim FileToOpen

FileToOpen = Application _
.GetOpenFilename("Text Files (*.xls), *.xls", , , , True)
'If FileToOpen(1) < False Then
' MsgBox "Open " & FileToOpen
'End If






  #4   Report Post  
Posted to microsoft.public.excel.programming
GS GS is offline
external usenet poster
 
Posts: 364
Default GetOpenFilename - Trapping Errors

Hi Tim,

The example code in VBA Help doesn't work if MultiSelect is set to true.
Setting MultiSelect to TRUE results in VBA expecting an array, even if it's
only a single selection. You may want to consider the following code (by John
Walkenbach) if you intent to work with an array of filenames:

Sub GetImportFileName2()
Dim Filt As String
Dim FilterIndex As Integer
Dim FileName As String
Dim Title As String
Dim i As Integer
Dim Msg As String

' Set up list of file filters
Filt = "Text Files (*.txt),*.txt," & _
"Lotus Files (*.prn),*.prn," & _
"Comma Separated Files (*.csv),*.csv," & _
"ASCII Files ((*.asc),*.asc," & _
"All Files (*.*),*.*"

' Display *.* by default
FilterIndex = 5

' Set the dialog box caption
Title = "Select a file to import"

' Get the filename
FileName = Application.GetOpenFilename _
(FileFilter:=Filt, _
FilterIndex:=FilterIndex, _
Title:=Title, _
MultiSelect:=True)

' Exit if dialog box canceled
If Not IsArray(FileName) Then
MsgBox "No file was selected."
Exit Sub
End If

' Display full path and name of the files
For i = LBound(FileName) To UBound(FileName)
Msg = Msg & FileName(i) & vbCrLf
Next i
MsgBox "You selected:" & vbCrLf & Msg
End Sub

HTH
Regards,
GS
  #5   Report Post  
Posted to microsoft.public.excel.programming
GS GS is offline
external usenet poster
 
Posts: 364
Default GetOpenFilename - Trapping Errors

A mistake exists in the declarations. The correction:

Dim FileName As String

should be:

Dim FileName As Variant

Regards,
GS


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 128
Default GetOpenFilename - Trapping Errors

Hi

Many thanks for this response - I found the help on filters useful, too

bw

Tim


"GS" wrote in message
...
Hi Tim,

The example code in VBA Help doesn't work if MultiSelect is set to true.
Setting MultiSelect to TRUE results in VBA expecting an array, even if

it's
only a single selection. You may want to consider the following code (by

John
Walkenbach) if you intent to work with an array of filenames:

Sub GetImportFileName2()
Dim Filt As String
Dim FilterIndex As Integer
Dim FileName As String
Dim Title As String
Dim i As Integer
Dim Msg As String

' Set up list of file filters
Filt = "Text Files (*.txt),*.txt," & _
"Lotus Files (*.prn),*.prn," & _
"Comma Separated Files (*.csv),*.csv," & _
"ASCII Files ((*.asc),*.asc," & _
"All Files (*.*),*.*"

' Display *.* by default
FilterIndex = 5

' Set the dialog box caption
Title = "Select a file to import"

' Get the filename
FileName = Application.GetOpenFilename _
(FileFilter:=Filt, _
FilterIndex:=FilterIndex, _
Title:=Title, _
MultiSelect:=True)

' Exit if dialog box canceled
If Not IsArray(FileName) Then
MsgBox "No file was selected."
Exit Sub
End If

' Display full path and name of the files
For i = LBound(FileName) To UBound(FileName)
Msg = Msg & FileName(i) & vbCrLf
Next i
MsgBox "You selected:" & vbCrLf & Msg
End Sub

HTH
Regards,
GS



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Trapping errors in a VLOOKUP function Blue Max Excel Worksheet Functions 1 April 26th 09 03:03 AM
Trapping errors with Conditional Formatting Sandusky Excel Discussion (Misc queries) 3 January 6th 09 08:52 PM
Trapping Workbook OPEN errors sclark Excel Programming 1 January 18th 04 11:12 PM
annoying bell when trapping errors Mika Excel Programming 0 November 7th 03 10:41 AM
Remind me to set Error Trapping to Break on Unhandled Errors don Excel Programming 0 November 5th 03 06:06 PM


All times are GMT +1. The time now is 11:40 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"