×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Selecting a file in VBA

Selecting a file in VBA

Selecting a file in VBA

(OP)
Hi there,

Does anyone know how to display a file open dialog and retrieve the name of the file that the user opens.  At the moment I can display the dialog and ascertain if they click OK or Cancel but I cannot find out which file they selected.

Any help much appreciated :)

Miles Macklin

RE: Selecting a file in VBA

I assume that you are using the Microsoft Common Dialog Control on your form. This sample shows the SaveAs dialog box. You may have to clean it up a bit for your needs.

dlg.Filter = "SolidWorks Part File (*.SLDPRT)|*.SLDPRT"
dlg.InitDir = "C:\Test\"
dlg.ShowSave
sFileName = dlg.FileName
sPartName = dlg.FileTitle
GetFileName = True
If Len(sFileName) = 0 Then
    GetFileName = False
    MsgBox "Cancelled!"
ElseIf Dir(sFileName) <> "" Then
    s1 = sFileName & vbCrLf & "Already Exists." & vbCrLf & _
    & "Should I overwrite the file?"

    UsrRes = MsgBox(s1, vbYesNo + _
    vbDefaultButton2, "Overwrite Existing File?")
    If UsrRes = vbNo Then
        MsgBox "Orignal File Preserved!", vbOKOnly, _
               "New File Not Saved!"
        GetFileName = False
    Else
       Kill sFileName
    End If
End If

DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.

RE: Selecting a file in VBA

hi,

appreciated if someone give me an idea how to get Microsoft Common Dialog Control at all?

thanks

RE: Selecting a file in VBA

Right mouse on open space on the toolbox and select Additional Controls.  Then select Microsoft Common Dialog Control.  Doing so, will add that control to your toolbox options.  Then you can create the control as you would any other

RE: Selecting a file in VBA

Miles Mack,
Try function: application.GetOpenFilename. It displays open file dialog box and returns the selected file.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources