VBA Open File with Dialog Box

VBA Open File with Dialog Box

Anonymous
Not applicable
26,863 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
26,864 Views
51 Replies
Replies (51)
Message 2 of 52

Anonymous
Not applicable
I found this website very good and ended up using this as my module class for file dialogues:

http://vbnet.mvps.org/code/comdlg/fileopendlg.htm
0 Likes
Message 3 of 52

Anonymous
Not applicable
I've got the exact same routine from Joe Sutphin's book "AutoCAD 2004 VBA"...

I can get both the File Open and File Save" boxes to display and I can navigate the folder hierarchy, and even select a file...

But NOTHING happens, nothing opens, nothing gets saved...I think I am minus an important step somewhere...something to do with linking the dialog control with the actual AutoCAD session...

But what do I know!!!!! FRUSTRATING!!!!!!!!!!!!!!!!!!!!!!

Still trying...can anyone help?
0 Likes
Message 4 of 52

Anonymous
Not applicable
I've got the exact same routine from Joe Sutphin's book "AutoCAD 2004 VBA"...

I can get both the File Open and File Save" boxes to display and I can navigate the folder hierarchy, and even select a file...

But NOTHING happens, nothing opens, nothing gets saved...I think I am minus an important step somewhere...something to do with linking the dialog control with the actual AutoCAD session...

But what do I know!!!!! FRUSTRATING!!!!!!!!!!!!!!!!!!!!!!

Still trying...can anyone help?
0 Likes
Message 5 of 52

Anonymous
Not applicable
wrote in message news:5388504@discussion.autodesk.com...
I've got the exact same routine from Joe Sutphin's book "AutoCAD 2004
VBA"...

I can get both the File Open and File Save" boxes to display and I can
navigate the folder hierarchy, and even select a file...

But NOTHING happens, nothing opens, nothing gets saved...I think I am minus
an important step somewhere...something to do with linking the dialog
control with the actual AutoCAD session...

But what do I know!!!!! FRUSTRATING!!!!!!!!!!!!!!!!!!!!!!

Still trying...can anyone help?


Hi Pellacad,

Not sure what you mean by exact same routine...but if you mean a wrapper for
the api call i mentioned, the return value is a string.
The string is the name of the file(s) you selected.
What you do with that filename is up to you to program.
What exactly are you trying to accomplish and what code do you have so far?
Post what you have and where you're stuck and it would be easier for someone
to help.

for example IF you want acad to open the file,
and IF you have a reference to acadapplication oAcadApp
and IF you are in mdi mode,
and IF the return value was one filename,
and IF it was set to the variable sFileName
Then...

oAcadapp.Documents.Open sFileName

would open the drawing.
:-)


hth
Mark
0 Likes
Message 6 of 52

Anonymous
Not applicable
Hi,

All the file dialog does is return a string of the file name selected.

It's up to you to write code which uses that string.

--
Regards


Laurie Comerford
www.cadapps.com.au
www.civil3dtools.com
wrote in message news:5388551@discussion.autodesk.com...
I've got the exact same routine from Joe Sutphin's book "AutoCAD 2004
VBA"...

I can get both the File Open and File Save" boxes to display and I can
navigate the folder hierarchy, and even select a file...

But NOTHING happens, nothing opens, nothing gets saved...I think I am minus
an important step somewhere...something to do with linking the dialog
control with the actual AutoCAD session...

But what do I know!!!!! FRUSTRATING!!!!!!!!!!!!!!!!!!!!!!

Still trying...can anyone help?
0 Likes
Message 7 of 52

Civil3DReminders_com
Mentor
Mentor
I can get the code to work to open the dialog box and get the message to open. How do I get the OutputStr passed to other modules in the project?

I have attached the project I am working on, most of the code is copied from posts in the group which is beyond my level of competence. Thanks to all that I have copied.

Christopher Fugitt
Civil Reminders
http://blog.civil3dreminders.com/
http://www.CivilReminders.com/
Alumni
0 Likes
Message 8 of 52

Civil3DReminders_com
Mentor
Mentor
I figured out how to pass the variable to other modules, make it a global variable at the top.

When I make the OutputStr = LayFile it won't let me add anything to the string later on in the program, it just cuts it off. Is there a way to add to this string? What causes it not to be addable?
Civil Reminders
http://blog.civil3dreminders.com/
http://www.CivilReminders.com/
Alumni
0 Likes
Message 9 of 52

Anonymous
Not applicable
wrote in message news:5416106@discussion.autodesk.com...
I figured out how to pass the variable to other modules, make it a global
variable at the top.

When I make the OutputStr = LayFile it won't let me add anything to the
string later on in the program, it just cuts it off. Is there a way to add
to this string? What causes it not to be addable?

I haven't looked at your attachment but assuming you're using an api wrapper
as originally posted, the string is created in a fixed length buffer

from op
'Create a buffer
OFName.lpstrFile = Space(254)

you'd have to create a variable length string variable if you want more
characters

Dim sAnyString as String
0 Likes
Message 10 of 52

Anonymous
Not applicable
If i might ask, i am building a batch plot routine specific to what we do here and need this openfile routine to allow multiple selection and have the drawings names stored in a single column array variable.

I am a little new to VBA and expert at lisp and i have your code for a single file name working great. Thank you for that. Would you help me out with the multiple selection?

thank you for your help.
0 Likes
Message 11 of 52

Ed__Jobe
Mentor
Mentor
Attached is my own class for file dialogs. I started with some code from a website that is now defunct. It was similar to what what posted here in that is only dealt with single files. I've added code that allows for multiple file selection, filtering, as well as other properties. It also support the BrowseForFolder dialog.

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 12 of 52

Anonymous
Not applicable
Ed

Thanks for the file. i am a little new at VBA so maybe you might give me a few hints as to where to put these.

I am familar with the VBA interface and am just now learning public proceedures and public variables. I am taking a class in Office VBA.

I am expert at lisp but new to VBA.

I am assuming this does not all go in one place. Just mention where each part goes if you will. You are welcome to start out brief and if i have questions i can reply again.

jimd
0 Likes
Message 13 of 52

Ed__Jobe
Mentor
Mentor
These? Its one single file, a class file. Save the zip to your hd, extract it. Open your dvb and rt+clk in the Project Browser and select Import...

Now you can create instances of the class in other modules as discussed elsewhere in this thread, i.e.
Dim MyDialog As New FileDialog
'set dialog props as req'd
'issue Open method, etc.

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 14 of 52

Anonymous
Not applicable
Ed

i guess i don;t know as much as i thought i did. I am not at all clear on how to run this class that you gave me.

If you would help, here is what i am trying to accomplish.

I want the user to click a button and the dialog box open. the user can browse to a directory of his choice and select one or more drawings. then push an open button (command button) and then the routine will store the full path of all the seleceted drawings to an array or something that i can then make use of in the rest of my program.

If you can give me an example of how to write the event code that will make this happen i would appreciate it.

I would think this would be something nearly every autocad vba enthusiast would need. i am surpised it has been so hard to find examples of this kind of code.

your class seems to come the closest to what i need except i am not sure how to call it.

if you can help let me know. If you don't have the time i will just keep looking.

thanks for your help.

jimd
0 Likes
Message 15 of 52

Anonymous
Not applicable
Hi everybody,
Just checking for a possibility. Is it possible to show the dialogue box on double clicking the drawing container window? I have seen the functionality in some other applications. Is it possible using windows API?
0 Likes
Message 16 of 52

Ed__Jobe
Mentor
Mentor
Just add your code to the form's double click event.

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 17 of 52

Ed__Jobe
Mentor
Mentor
Here is a sample. For a better understanding, you need to read up on the api functions used at msdn.com
[code]

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
'desc,filter combinations must all be separated with pipe char "|"
strFilter = "All Files (*.*)|*.*|Drawings (*.dwg)|*.dwg"
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[/code]

However, this is not the class I had in mind. The Open dialog only retrieves one file at a time. I'll have to look again for the other work I did. The only change I made to this file was to allow for longer file names. The original had a 254 char limit.

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 18 of 52

Anonymous
Not applicable
Ed

thanks, i will try your sample code and i will study up on API's.

You mentioned this is not for multiple file selection afterall. If you find your work for multiple file selection i would appreciate having a look.

thanks for your help. I couldn't do this with out it.

jimd
0 Likes
Message 19 of 52

Ed__Jobe
Mentor
Mentor
Sorry, I was looking at an older version. The attached class does have a MultiSelect property.

If you set that property to true and the user selects multiple files, then the ShowOpen method will return a string in the format: "Path file1 file2" where the spaces in the string are actually vbNullChar. In the following msdn article, at the bottom in the Show() method, is some code to handle separating the various filenames from the single string. In the example I gave you, the MsBox will only show the path. You need to step through the code in break mode to see what the string actually looks like.

http://msdn2.microsoft.com/en-us/library/aa155724(office.10).aspx

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 20 of 52

Anonymous
Not applicable
Hi Ed Jobe,
Thanks for the reply. Unfortunately, AutoCAD does not have a double click action for the Application. If I have the application window handle (Application.hwnd), then how can I write code for the double click action for that object ? Is it possible using Windows API?. I am sorry to be an absolute beginner in Windows API programming. I also read that if the API programming is not done properly, it will backfire damaging the application files.

Thanks for any advice in this matter.
0 Likes