Results 1 to 6 of 6
  1. #1
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Application.GetOpenFilename (XP)

    When using Application.GetOpenFilename there is an "open and cancel" button. What would be the code that controls the "cancel" button. I have tried vbCancel but that does not work.

    Thanks,
    John

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Application.GetOpenFilename (XP)

    If you look up GetOpenFilename in the Excel VBA help, you'll see an example. GetOpenFilename returns the Boolean value False if the user clicks Cancel, and the path+filename if the user clicks Open.

    Dim fileToOpen As Variant
    fileToOpen = Application.GetOpenFilename(...)
    If VarType(fileToOpen) = vbBoolean Then
    ' User clicked Cancel
    MsgBox "You clicked Cancel"
    Else
    ' User clicked OK
    MsgBox "You selected " & fileToOpen
    End If

  3. #3
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Application.GetOpenFilename (XP)

    Thank you Hans

  4. #4
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Application.GetOpenFilename (XP)

    Hans,

    On a similar note using: Set fd = Application.FileDialog(msoFileDialogFolderPicker)

    What would the code be for the "cancel" button on the FileDialog be?

    Regards,
    John

    PS-My Excel help does not portray the example as you previously mentioned.

  5. #5
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Application.GetOpenFilename (XP)

    I found the solution. It is not quite what I thought it would be.

    Set fd = Application.FileDialog(msoFileDialogFolderPicker)

    With fd
    If .Show = -1 Then
    'User clicked Ok
    Else
    'User clicked Cancel
    End If
    End With


    Regards,
    John

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Application.GetOpenFilename (XP)

    There seemed to be little point in copying the example from the online help literally, so I posted a slight modification.

    The Show method of the FileDialog object returns True if the user clicked OK (an you can then inspect the SelectedItems array to see which file or files were selected), and False if the user clicked Cancel. Again, see the online help.

Posting Permissions

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