VBA Open File with Dialog Box

VBA Open File with Dialog Box

Anonymous
Not applicable
27,739 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
27,740 Views
51 Replies
Replies (51)
Message 21 of 52

Ed__Jobe
Mentor
Mentor
I misunderstood you. I thought you wanted to dc in a form window. You don't want the application object. You need the Document object. It is the container for the Editor, which is where you will be dc'ing. I wrote the code below to modify the behavior of DOUBLECLICKEDIT. You may also need to add a check to make sure no other command is running.
[code]
Private Sub Doc_BeginDoubleClick(ByVal PickPoint As Variant)
'If item being clicked on is a dimension,
'then open the Mtext editor instead of the properties dialog.
Dim s1 As AcadSelectionSet
Dim oEnt As AcadEntity

Set s1 = ThisDrawing.PickfirstSelectionSet
If s1.Count > 0 Then
For Each oEnt In s1
Select Case oEnt.ObjectName
Case Is = "AcDbAlignedDimension"
ThisDrawing.SendCommand "ddedit "
Case Is = "AcDb2LineAngularDimension"
ThisDrawing.SendCommand "ddedit "
Case Is = "AcDb3PointAngularDimension"
ThisDrawing.SendCommand "ddedit "
Case Is = "AcDbDiametricDimension"
ThisDrawing.SendCommand "ddedit "
Case Is = "AcDbOrdinateDimension"
ThisDrawing.SendCommand "ddedit "
Case Is = "AcDbRadialDimension"
ThisDrawing.SendCommand "ddedit "
Case Is = "AcDbRotatedDimension"
ThisDrawing.SendCommand "ddedit "
End Select
Next oEnt
Else
End If
End Sub[/code]

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

Anonymous
Not applicable
Ed

Understand now that i am green. And i am reading about API's on msdn.com but in the mean time i am lost again.

As you suggested i have set multiselect to true but i must not be setting the correct thing becasue it is still only accepting one file.

Would you mind offereing an example of setting multiselect to true?

thanks for you help.

jimd
0 Likes
Message 23 of 52

Ed__Jobe
Mentor
Mentor
In the previous example, I created and object called objFile. Just set its MultiSelect prop.
objFile.MultiSelect = True

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

Anonymous
Not applicable
Beautiful. Thank you very much. this works great. i can handle the file names i am pretty sure and move on with my routine.

I would like to make the dialog box larger maybe twice. is this another property or variable i need to set? If you have a moment let me know.

I hope i can return these favors to someone. this is great.

thanks

jimd
0 Likes
Message 25 of 52

Ed__Jobe
Mentor
Mentor
They're standard windows dialogs. You should be able to stretch them normally.

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

Anonymous
Not applicable
Ed

Thanks again for your time. But the window will not re-size. I used the class you posted and then i also used your sample of a public routine to call the dialog box and i get a window so small that it can;t even display some of the file names completely.

Now this did not happen unitl i set it to be multiselect. Did i set something wrong.?

Private Sub CommandButton1_Click()
Call OpenFile
End Sub

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:\public\jobs\"
objFile.Filter = strFilter
objFile.MultiSelect = True ' I ADDED THIS LINE

'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
0 Likes
Message 27 of 52

Anonymous
Not applicable
Thanks again Ed Jobe, for your reply and sample code. Unfortunately sometimes I am not good at explaining something properly :). Actually I don't want a double click action on the document object or whatever comes inside the document. Consider this case; you opened AutoCAD with a drawing active. Now Let's close the active drawing. Then we have only the drawing container (which acts like an MDI form for the documents). I want to double click on this container form to show the Open Dialog box. Is this possible? You can see the same feature in Adobe Photoshop. What I have with me is the handle of Application Window. How can I trap events for that?
0 Likes
Message 28 of 52

Anonymous
Not applicable
I haven't looked at the posted class, but you need to set the explorer bit
in the dialog in order to get the XP-style dialog box. Below is a sample
from my class (hopefully the posted class has these enums and the Flag
property).

objFile.Flags = OFN_ALLOWMULTISELECT + OFN_EXPLORER + OFN_FILEMUSTEXIST +
OFN_PATHMUSTEXIST

--
R. Robert Bell


wrote in message news:5592892@discussion.autodesk.com...
Ed

Thanks again for your time. But the window will not re-size. I used the
class you posted and then i also used your sample of a public routine to
call the dialog box and i get a window so small that it can;t even display
some of the file names completely.

Now this did not happen unitl i set it to be multiselect. Did i set
something wrong.?

Private Sub CommandButton1_Click()
Call OpenFile
End Sub

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:\public\jobs\"
objFile.Filter = strFilter
objFile.MultiSelect = True ' I ADDED THIS LINE

'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
0 Likes
Message 29 of 52

Ed__Jobe
Mentor
Mentor
That's called the zero-document-state. No vba or lisp code can run then. Even the menus are not available for customization.

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

Ed__Jobe
Mentor
Mentor
Thanks Robert. The class has always worked for me. It ANDS multiselect with the current flags. Explicitly setting them wouls be better.

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

Anonymous
Not applicable
Forgive me if you find further discussion on this matter insane. I think the above mentioned zero-document-state is relative to the AutoCAD Application Context. What about the Operating System Context? As compared to that, can't be the AutoCAD application window be accessed using the API functions? If it's possible, I have my last question. Can the events be trapped for a window in the operating system through API programming ? I am sorry if you find any blunders in my questions. My knowledge in programming is quite limited.
0 Likes
Message 32 of 52

Anonymous
Not applicable
Robert or Ed

"objFile.Flags = OFN_ALLOWMULTISELECT + OFN_EXPLORER + OFN_FILEMUSTEXIST +
OFN_PATHMUSTEXIST"

The line above that Robert suggested i add didn't work. I am sure it is becasue i do not know what i am doing but when i put it in i get a "method or data member not foud" error.

Ed made the comment that it is a standard windows window and should be able to be resized but it isn't a standard window. the window comes up very small and the edges will not re-size. it is interesting because if i comment out the follwoing line in my proceedure that calls Ed's class:

objFile.MultiSelect = True

Then a window will come up that is a standard window, large enough and it can be resized. But i can only select one file at a time. But if i put this statement back in then i can select multiple files but the window is small and not resizable.

Any help would be greatly appreciated.

jimd
0 Likes
Message 33 of 52

Ed__Jobe
Mentor
Mentor
The line has to be added to the class. Look for the existing line that sets the Flags prop and add the suggested constants.

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

Anonymous
Not applicable
THX ROBERT. It works just perfect!
Though the weather sucks, this makes my day!
0 Likes
Message 35 of 52

Anonymous
Not applicable
exactly what I was looking for, thank you.
0 Likes
Message 36 of 52

Ed__Jobe
Mentor
Mentor
You're welcome.

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

Anonymous
Not applicable
Hi Sccadmember

I just came across your post; I am kind of the reverse of you, I am a VBA guy that is just learning some simple LISP.
However, I have figured out a way to use The Getfiled Method in LISP with VBA, if you are interested.
Let me know
Mark
0 Likes
Message 38 of 52

Anonymous
Not applicable
Just downloaded your code works great. Two questions, why does it display a completely different dialog box if multi-select is turned on?

Has anyone developed / found code that will allow adding folders to the "Places" bar on the left? I would like to duplicate the functionality of the AutoCAD Open File dialog. I am not up to speed on .net, all I use is VBA.
0 Likes
Message 39 of 52

Anonymous
Not applicable
>>As you suggested i have set multiselect to true but i must
>> not be setting the correct thing becasue it is still only >>accepting one file.

In addition to setting the flags, as already posted, when you accept Multiple files in the FILE OPEN dialog, the return is a VARIANT safearray; and if my memory serves me correctly,
the first index (0) is the PATH only, and the following indexes are null-terminated strings of the FILENAME only.

But you process the return the same; strip the nulls off each index, and prepend the path (index 0) to the filenames (index 1 to ....)
0 Likes
Message 40 of 52

Anonymous
Not applicable
Hey, Thanks a million man, Its working fine, I was trying for so many codes, I think this really work, i will make the changes as per my requirements.

Bairam
0 Likes