Consulting

Results 1 to 6 of 6

Thread: User not defined? OpenFileDialog1

  1. #1

    User not defined? OpenFileDialog1

    I all, newbee here. I keep getting user not defined for the following code. Whicj library does it belong to?

    [VBA]OpenFileDialog1.ShowDialog()[/VBA]

    Thanks

  2. #2
    VBAX Mentor XL-Dennis's Avatar
    Joined
    May 2004
    Location
    ?stersund, Sweden
    Posts
    499
    Location
    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:

    [vba]
    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()
    [/vba]

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

    Kind regards,
    Dennis
    Kind regards,
    Dennis

    ExcelKB | .NET & Excel | 2nd edition PED


  3. #3
    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?

  4. #4
    VBAX Mentor XL-Dennis's Avatar
    Joined
    May 2004
    Location
    ?stersund, Sweden
    Posts
    499
    Location
    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:
    [vba]
    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
    [/vba]

    Below is an example that works with version 97 and onforward.
    [vba]
    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
    [/vba]

    Kind regards,
    Dennis
    Kind regards,
    Dennis

    ExcelKB | .NET & Excel | 2nd edition PED


  5. #5
    Thanks Dennis. You've been a great help.

  6. #6
    VBAX Mentor XL-Dennis's Avatar
    Joined
    May 2004
    Location
    ?stersund, Sweden
    Posts
    499
    Location
    You're welcome and please let us know how it progress.

    Kind regards,
    Dennis
    Kind regards,
    Dennis

    ExcelKB | .NET & Excel | 2nd edition PED


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •