Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations GregLocock on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selecting a file in VBA

Status
Not open for further replies.

Guest
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
 
Replies continue below

Recommended for you

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.
Code:
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) <> &quot;&quot; Then
    s1 = sFileName & vbCrLf & &quot;Already Exists.&quot; & vbCrLf & _
    & &quot;Should I overwrite the file?&quot; 

    UsrRes = MsgBox(s1, vbYesNo + _ 
    vbDefaultButton2, &quot;Overwrite Existing File?&quot;)
    If UsrRes = vbNo Then
        MsgBox &quot;Orignal File Preserved!&quot;, vbOKOnly, _
               &quot;New File Not Saved!&quot;
        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.
 
hi,

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

thanks
 
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
 
Miles Mack,
Try function: application.GetOpenFilename. It displays open file dialog box and returns the selected file.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor