PDA

View Full Version : User not defined? OpenFileDialog1



boneKrusher
01-27-2006, 05:34 PM
I all, newbee here. I keep getting user not defined for the following code. Whicj library does it belong to?

OpenFileDialog1.ShowDialog()

Thanks

XL-Dennis
01-27-2006, 06:09 PM
Hi,

In general You should select the keyword (here OpenFileDialog) and hit the F1-button as the help is good.

#1 Drag the OpenFindDialog from the toolbox (You find it under the group Dialogs) to the Form.

#2 Add the following structure to Your procedure:


Dim OpenFileDialog1 As New OpenFileDialog
With OpenFileDialog1

.Filter = "Excel Files (*.xls|*.xls|" & "All files|*.*"

If (.ShowDialog() = Windows.Forms.DialogResult.OK) Then

stFile = .FileName

End If

End With

'The following expression should also be used as part of the procedure:

OpenFileDialog1.Dispose()


You should also add the SEH do the procedure, that is Try - Catch - Finally.

Kind regards,
Dennis

boneKrusher
01-28-2006, 06:14 AM
Thanks for the help. I am using a "working Model" of VB6 which does not have help or OpenFindDialog in the toolbox. Is there a way to do this VBA?

XL-Dennis
01-28-2006, 06:33 AM
Hi,



Thanks for the help. I am using a "working Model" of VB6 which does not have help or OpenFindDialog in the toolbox


That's true as it was implemented with VB.NET 2005 which I initially thought that You used to develop with ;)

Below is an example that is valid for version 2002 and 2003 of Excel:

Option Explicit
Sub Open_Files_2002_2003()
Dim fdOpen As Office.FileDialog
Dim lnNumber As Long
'Instantiate the object
Set fdOpen = Application.FileDialog(msoFileDialogOpen)
With fdOpen
'Control the detailed view to use.
.InitialView = msoFileDialogViewDetails
'Decide if the users would be allowed to open multiply files or only one.
.AllowMultiSelect = True
'Name for the Dialogform to be showed.
.Title = "Open XL-Dennis files"
'Name of the Open button
.ButtonName = "Open XL-Dennis Files"
'Control which folder to be open initially.
.InitialFileName = "c:\Test\"
'Which extension that will used to filter the file list.
.Filters.Add "XL-Dennis files", "*.xls", 1
'The default file filter.
.FilterIndex = 1
'Open the dialogform.
.Show
'Open the selected files.
For lnNumber = 1 To .SelectedItems.Count
Application.Workbooks.Open (.SelectedItems(lnNumber))
Next lnNumber
End With
'Release object from memory.
Set fdOpen = Nothing
End Sub


Below is an example that works with version 97 and onforward.

Option Explicit
Sub Open_One_Or_More_Files()
Dim vaFiles As Variant
Dim i As Long
vaFiles = Application.GetOpenFilename _
(FileFilter:="Excel Files (*.xls),*.xls", _
Title:="Open files", MultiSelect:=True)
'If the user cancel the operation
If Not IsArray(vaFiles) Then Exit Sub
'Open the selected file(s)
With Application
.ScreenUpdating = False
For i = 1 To UBound(vaFiles)
Workbooks.Open vaFiles(i)
Next i
.ScreenUpdating = True
End With
End Sub


Kind regards,
Dennis

boneKrusher
01-28-2006, 07:02 AM
Thanks Dennis. You've been a great help.

XL-Dennis
01-28-2006, 07:07 AM
You're welcome and please let us know how it progress.

Kind regards,
Dennis