VBA Open File with Dialog Box

VBA Open File with Dialog Box

Anonymous
Not applicable
30,882 Views
51 Replies
Message 1 of 52

VBA Open File with Dialog Box

Anonymous
Not applicable
Just thought I would post this because I have been looking for a working VBA file open dialog box solution for awhile. I'm an old autolisped making the jump to VBA and I have seen and read various solutons for the equivalent getfiled autolisp function but I never had much luck with them. This one worked for me it uses the Win API to do the job.

Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
"GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Public Function ShowOpen(Filter As String, _
InitialDir As String, _
DialogTitle As String) As String
Dim OFName As OPENFILENAME
'Set the structure size
OFName.lStructSize = Len(OFName)
'Set the owner window
OFName.hwndOwner = 0
'Set the filter
OFName.lpstrFilter = Filter
'Set the maximum number of chars
OFName.nMaxFile = 255
'Create a buffer
OFName.lpstrFile = Space(254)

'Create a buffer
OFName.lpstrFileTitle = Space$(254)
'Set the maximum number of chars
OFName.nMaxFileTitle = 255
'Set the initial directory
OFName.lpstrInitialDir = InitialDir
'Set the dialog title
OFName.lpstrTitle = DialogTitle
'no extra flags
OFName.flags = 0
'Show the 'Open File' dialog
If GetOpenFileName(OFName) Then
ShowOpen = Trim(OFName.lpstrFile)
Else
ShowOpen = ""
End If
End Function

Make a form and place the following code listed below on a button to call the showopen routine.

Private Sub CommandButton1_Click()
Dim Filter As String
Dim InitialDir As String
Dim DialogTitle As String
Dim OutputStr As String

Filter = "Drawing Files (*.dwg)" + Chr$(0) + "*.dwg" + Chr$(0) + _
"All Files (*.*)" + Chr$(0) + "*.*" + Chr$(0)
InitialDir = "C:\Program Files\AutoCAD 2006\Sample"
DialogTitle = "Open a DWG file"
OutputStr = ShowOpen(Filter, InitialDir, DialogTitle)
MsgBox OutputStr
End Sub
30,883 Views
51 Replies
Replies (51)
Message 41 of 52

Anonymous
Not applicable
Is there no way to access the ACAD file dialog (like in Inventor) rather than the common dialog control ?
I'm on ACAD 2008.
0 Likes
Message 42 of 52

Anonymous
Not applicable
Hi,
Can you please tell me how to go ahead with this:
When i select a file from a Dialog box i need to read the contents of a query file and put into a access file,

Can any one help me on this.

Thanks
Bairam
0 Likes
Message 43 of 52

Anonymous
Not applicable

Sccadmember,

 

Thanks for the code in your first post... Works like a charm! Smiley Happy

 

Cheers

0 Likes
Message 44 of 52

Anonymous
Not applicable

Just an update for those trying to use Ed's wonderful FileDialog Class in AutoCAD 2014 and beyond.

 

Besides all the PtrSafe and LongPtr changes to the Declare GetOpenFileName & OPENFILENAME type, you need to change the Len(udtStruct) to LenB(udtStruct).

 

AutoCAD 2014 is the first version to use VBA7, which is 64-bit, like AutoCAD.  Prior to this, VBA was 32-bit and PtrSafe and LongPtr/LongLong didn't exist. You can use pre-compliler directives to keep the same code working in both 32-bit & 64-bit versions.

 

 

'//The Win32 API Functions///
#If VBA7 Then Private Declare PtrSafe Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long Private Declare PtrSafe Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long #Else Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long #End If

 

'//The Structure
#If VBA7 Then
Private Type OPENFILENAME
    lStructSize As Long
    hwndOwner As LongPtr
    hInstance As LongPtr
    lpstrFilter As String
    lpstrCustomFilter As String
    nMaxCustFilter As Long
    nFilterIndex As Long
    lpstrFile As String
    nMaxFile As Long
    lpstrFileTitle As String
    nMaxFileTitle As Long
    lpstrInitialDir As String
    lpstrTitle As String
    flags As Long
    nFileOffset As Integer
    nFileExtension As Integer
    lpstrDefExt As String
    lCustData As Long 'LongPtr
    lpfnHook As LongPtr
    lpTemplateName As String
End Type
#Else
Private Type OPENFILENAME
    lStructSize As Long
    hwndOwner As Long
    hInstance As Long
    lpstrFilter As String
    lpstrCustomFilter As String
    nMaxCustFilter As Long
    nFilterIndex As Long
    lpstrFile As String
    nMaxFile As Long
    lpstrFileTitle As String
    nMaxFileTitle As Long
    lpstrInitialDir As String
    lpstrTitle As String
    flags As Long
    nFileOffset As Integer
    nFileExtension As Integer
    lpstrDefExt As String
    lCustData As Long
    lpfnHook As Long
    lpTemplateName As String
End Type
#End If

 

 

Message 45 of 52

chris.leighton
Explorer
Explorer

Do you just add this to the end of the class file?

0 Likes
Message 46 of 52

Ed__Jobe
Mentor
Mentor

No, it would go at the beginning.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 47 of 52

jean-marc.stpierre
Observer
Observer

If you just need to open the file dialog box from code just use Thisdrawing.sendcommand("_open")

Jean-Marc

0 Likes
Message 48 of 52

jean-marc.stpierre
Observer
Observer

Use ThisDrawing.sendcommand("_open ") with a space after the n.

0 Likes
Message 49 of 52

Ed__Jobe
Mentor
Mentor

Recently, there have been some more questions about this class so I'm posting a completed (includes the comments so far) class file (FileDialogs.cls) that you can import into your vba project. Below is an updated sample of how to run the class.

Public Sub OpenFile()
    'sample to show how to use FileDialogs
    Dim objFile As FileDialogs
    Dim strFilter As String
    Dim strFileName As String

    Set objFile = New FileDialogs
    objFile.OwnerHwnd = ThisDrawing.HWND32
    'desc,filter combinations must all be separated with pipe char "|"
    strFilter = "Drawings (*.dwg)|*.dwg|All Files (*.*)|*.*"
    objFile.Title = "Open a drawing"
    'default dir is CurDir
    objFile.StartInDir = "c:\"
    objFile.Filter = strFilter
    'return a valid filename
    strFileName = objFile.ShowOpen
    If Not strFileName = vbNullString Then
        'use this space to perform operation
        MsgBox strFileName
    End If
    Set objFile = Nothing

End Sub

 

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Message 50 of 52

Anonymous
Not applicable

Here is a sample.

 

'---getFolderPath----------------------------------------------------------

Function getFolderPath() As String
Set myFolder = CreateObject("Shell.Application").BrowseForFolder(0, "GetFolder", 0)
If Not myFolder Is Nothing Then myPath$ = myFolder.Items.Item.path Else MsgBox "Folder not Selected": Exit Function
If Right(myPath, 1) <> "" Then myPath = myPath & ""
getFolderPath = myPath
End Function

https://www.twblogs.net/a/5b83353b2b71771e35c18a62 

0 Likes
Message 51 of 52

thijs_js
Community Visitor
Community Visitor

Hi Sccadmember,

 

Thanks for this script, it works perfectly.

I have a question, is there an easy way to modify this scirpt to promp a user for a folder location only?

 

Best regards,

 

Thijs

0 Likes
Message 52 of 52

Ed__Jobe
Mentor
Mentor

Yes, Take a look at my sticky post at the top of this forum.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes