Visual Basic Customization

Visual Basic Customization

Reply
*markP
Message 1 of 13 (146 Views)

file dialog control in vba?

146 Views, 12 Replies
03-21-2001 10:56 AM
I know this is probably a really dumb question, but I can't find the stock
file dialog box in vba. I assume there is one but I've been searching all
the help files and reference manuals and books and I cant' find a built in
generic file selection dialog box like you get with getfiled lisp function.
Surely there is a way? Msdn talks about common dialog and showopen method
so
I added the common dialog controls to the controls toolbox and created a new
form then dragged a common control to the form.
then in the code for the form I put
Sub test()
ShowSave.me
'ShowOpen.me
End Sub
and tried running the sub but the form shows up blank. I thought the method
was going to show an acutal open file dialog box ready made or an Saveas
dialog box, depending on which method you used.
whats weirder is the following lines all have the same effect

showsave.me
showsave
form1.showsave
commondialog1.show

how is that possible???

Obviously I missing something here. ...yeah like a brain maybe???....
So far I've been creating forms, using drivelistboxes, dirlistboxes, and
filelistboxes, linked with events to use as a workaround but I have the
feeling I'm just being really dumb here and there's some ready made and easy
to use thingamagigger here somewhere?
Any help?

I also tried
Sub test()
ThisDrawing.SendCommand "(getfiled nil nil nil nil)" & vbCr
End Sub
but get the error bad argument type: stringp nil in autocad

and this
ThisDrawing.SendCommand "(getfiled "Directory Listing" "" "" 2)"& vbCr
and get the error expected end of statement in the vbaide.
but
(getfiled "Directory Listing" "" "" 2)
works at the command line.
I'se confuzzed....
*Walter
Message 2 of 13 (146 Views)

Re: file dialog control in vba?

03-21-2001 11:49 AM in reply to: *markP
Explore around vbDesign http://hypermart.vbdesign.net
See the 'Tutorials' and 'Source Code' pages.

-- Walter -- http://www.ActiveDwg.com --
*Rath, Randall
Message 3 of 13 (146 Views)

Re: file dialog control in vba?

03-21-2001 11:56 AM in reply to: *markP
Hi Mark,

'From A Code A Day, March 2, 2000 - Guest Author Frank Zander'
GUEST AUTHOR
Frank Zander
In yesterday's letter I provided a solution for setting the common dialog to
accept multiple file selections, and then a method to place all of the file
names into a list box. Frank Zander offers this VERY nice solution that
trims back the amount of code used and places the filenames directly into
the list box (rather than into an array then into the list box) Frank has a
great site that you can visit for information and examples on VBA and VB
programming:
http://www.contractcaddgroup.com (Mark, you really should stop by Frank's
site, there are some great utilities there!)
Take it away Frank,
This solution returns the Path and file name information to a list box
Name the CommonDialog as CommonDialog1 and the list box as lstPrograms
Private Sub cmdADD_Click()
Dim SelectedFiles As Variant
Dim myPos As Integer
Dim myDir As String
Dim strFileName As String
Dim strFiles As String
Dim strPath As String
On Error GoTo ErrHandler
' empty the filename in the dialog box
CommonDialog1.DialogTitle = "Select AutoCAD Appllications arx, lsp, dvb,
dbx, vlx or fas Files"
CommonDialog1.FileName = ""
CommonDialog1.MaxFileSize = 32000
CommonDialog1.Filter = "AutoCAD Apps
(*.arx;*.lsp;*.dvb;*.dbx;*.vlx;*.fas)|*.arx;*.lsp;*.dvb;*.dbx;*.vlx;*.fas|AR
X (*.arx)|*.arx|VBA projects (*.dvb)|*.dvb|Dialogue Control
(*.dcl)|*.dcl|All Files (*.*)|*.*"
CommonDialog1.DefaultExt = "*.lsp"
CommonDialog1.FilterIndex = 1
' Set Cancel to True.
CommonDialog1.CancelError = True
' allow multi select = cdlOFNAllowMultiselect
' long filenames = cdlOFNLongNames
' explorer interface = cdlOFNExplorer
CommonDialog1.Flags = cdlOFNAllowMultiselect + cdlOFNExplorer +
cdlOFNNoDereferenceLinks + cdlOFNPathMustExist + cdlOFNHideReadOnly
' set the dialogbox to show open
CommonDialog1.ShowOpen

strFiles = CommonDialog1.FileName
strFileName = CommonDialog1.FileName
If InStr(strFileName, Chr$(0)) > 0 Then
strPath = Mid(strFiles, 1, InStr(strFiles, Chr$(0)) - 1)
If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
While InStr(strFiles, Chr$(0)) > 0
strFiles = Right(strFiles, Len(strFiles) - InStr(strFiles, Chr$(0)))
' Debug.Print strFiles
If InStr(strFiles, Chr$(0)) > 0 Then
strFileName = Mid(strFiles, 1, InStr(strFiles, Chr$(0)) - 1)
Else
strFileName = strFiles
End If
lstPrograms.AddItem strPath & strFileName
Wend
Else
lstPrograms.AddItem strFileName
End If

' Exit Sub
ErrHandler:
' User pressed Cancel button.
Exit Sub
End Sub

That's the end of that article - Now, how about skipping that whole nasty
common dialog control? Here is a class module and a calling sub that will
get you the name of a single file:

o--------------- FileDialog
'be sure to name the class FileDialog
Option Explicit

'//The Win32 API Functions///
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

'//A few of the available Flags///
Private Const OFN_HIDEREADONLY = &H4
'//The Structure
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

Private lngHwnd As Long
Private strFilter As String
Private strTitle As String
Private strDir As String
Private blnHideReadOnly As Boolean

Private Sub Class_Initialize()
'Set default values when
'class is first created
strDir = CurDir
strTitle = "Llamas Rule"
strFilter = "All Files" _
& Chr$(0) & "*.*" & Chr$(0)
lngHwnd = &O0 'Desktop
End Sub

Public Property Let OwnerHwnd(WindowHandle As Long)
'//FOR YOU TODO//
'Use the API to validate this handle
lngHwnd = WindowHandle
'R14 users who just want to use this code:
'Simple, don't set this property! the default
'of &0 will work fine for most of yor needs
End Property

Public Property Get OwnerHwnd() As Long
OwnerHwnd = lngHwnd
End Property

Public Property Let Title(Caption As String)
'don't allow null strings
If Not Caption = vbNullString Then
strTitle = Caption
End If
End Property

Public Property Get Title() As String
Title = strTitle
End Property

Public Property Let Filter(ByVal FilterString As String)
'Filters change the type of files that are
'displayed in the dialog. I have designed this
'validation to use the same filter format the
'Common dialog OCX uses:
'"All Files (*.*)|*.*"
Dim intPos As Integer
Do While InStr(FilterString, "|") > 0
intPos = InStr(FilterString, "|")
If intPos > 0 Then
FilterString = Left$(FilterString, intPos - 1) _
& Chr$(0) & Right$(FilterString, _
Len(FilterString) - intPos)
End If
Loop
If Right$(FilterString, 2) <> Chr$(0) & Chr$(0) Then
FilterString = FilterString & Chr$(0)
End If
strFilter = FilterString
End Property

Public Property Get Filter() As String
'Here we reverse the process and return
'the Filter in the same format the it was
'entered
Dim intPos As Integer
Dim strTemp As String
strTemp = strFilter
Do While InStr(strTemp, Chr$(0)) > 0
intPos = InStr(strTemp, Chr$(0))
If intPos > 0 Then
strTemp = Left$(strTemp, intPos - 1) _
& "|" & Right$(strTemp, _
Len(strTemp) - intPos)
End If
Loop
If Right$(strTemp, 1) = "|" Then
strTemp = Left$(strTemp, Len(strTemp) - 1)
End If
Filter = strTemp
End Property

Public Property Let HideReadOnly(blnVal As Boolean)
'Simple one
blnHideReadOnly = blnVal
End Property

Public Property Get HideReadOnly() As Boolean
HideReadOnly = blnHideReadOnly
End Property

'@~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~@
' Display and use the File open dialog
'@~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~@
Public Function ShowOpen() As String
Dim strTemp As String
Dim udtStruct As OPENFILENAME
udtStruct.lStructSize = Len(udtStruct)
'Use our private variable
udtStruct.hwndOwner = lngHwnd
'Use our private variable
udtStruct.lpstrFilter = strFilter
udtStruct.lpstrFile = Space$(254)
udtStruct.nMaxFile = 255
udtStruct.lpstrFileTitle = Space$(254)
udtStruct.nMaxFileTitle = 255
'Use our private variable
udtStruct.lpstrInitialDir = strDir
'Use our private variable
udtStruct.lpstrTitle = strTitle
'Ok, here we test our boolean to
'set the flag
If blnHideReadOnly Then
udtStruct.flags = OFN_HIDEREADONLY
Else
udtStruct.flags = 0
End If
If GetOpenFileName(udtStruct) Then
strTemp = (Trim(udtStruct.lpstrFile))
ShowOpen = Mid(strTemp, 1, Len(strTemp) - 1)
End If
End Function

'@~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~@
' Display and use the File Save dialog
'@~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~@
Public Function ShowSave() As String
Dim strTemp As String
Dim udtStruct As OPENFILENAME
udtStruct.lStructSize = Len(udtStruct)
'Use our private variable
udtStruct.hwndOwner = lngHwnd
'Use our private variable
udtStruct.lpstrFilter = strFilter
udtStruct.lpstrFile = Space$(254)
udtStruct.nMaxFile = 255
udtStruct.lpstrFileTitle = Space$(254)
udtStruct.nMaxFileTitle = 255
'Use our private variable
udtStruct.lpstrInitialDir = strDir
'Use our private variable
udtStruct.lpstrTitle = strTitle
'Ok, here we test our flag
If blnHideReadOnly Then
udtStruct.flags = OFN_HIDEREADONLY
Else
udtStruct.flags = 0
End If
If GetSaveFileName(udtStruct) Then
strTemp = (Trim(udtStruct.lpstrFile))
ShowSave = Mid(strTemp, 1, Len(strTemp) - 1)
End If
End Function

o------------- the call that sets it in motion

Public Sub DialogTest()
Dim strFile As String
Dim objDialog As FileDialog
Set objDialog = New FileDialog
objDialog.HideReadOnly = True
objDialog.Filter = "Text Files (*.txt)|*.txt"
objDialog.OwnerHwnd = ThisDrawing.HWND
objDialog.Title = "Sample Dialog"
strFile = objDialog.ShowOpen
MsgBox strFile
Set objDialog = Nothing
End Sub

Randall Rath
*Rath, Randall
Message 4 of 13 (146 Views)

Re:

03-21-2001 12:01 PM in reply to: *markP
Hi Walter,
I brought the site with me!

Just a general comment to anyone who reads this thread -
You have complete freedom to do what you want with the code you find at VB
Design, and you don't have to mention the site!
I built it for ALL OF YOU, and without the questions you ask I would never
have made even a tenth of what's there!

Randall Rath
*Walter
Message 5 of 13 (146 Views)

Re:

03-21-2001 12:30 PM in reply to: *markP
Hi Randall,
That's great! But I prefer to explore...

-- Walter -- http://www.ActiveDwg.com --
*Rath, Randall
Message 6 of 13 (146 Views)

Re:

03-21-2001 12:41 PM in reply to: *markP
Hi Walter,
Please do, after all ActiveDWG is a PRIMARY SUPPORTER of VB Design..
Hey, where is your entry for the "spinning busy thingy"?

Randall Rath
*Goodfellow, John
Message 7 of 13 (146 Views)

Re: file dialog control in vba?

03-21-2001 10:40 PM in reply to: *markP
FYI, you may find the help file for Comdlg32.dll in
C:\WINDOWS\HELP\CMDLG98.CHM.
--
John Goodfellow
irtf'nm
use 'microtouch' in address to email

"markP" wrote in message
news:smileyvery-happy:F8F016D5C8531E85FD0238FB85332EB@in.WebX.maYIadrTaRb...
> I know this is probably a really dumb question, but I can't find the stock
> file dialog box in vba. I assume there is one but I've been searching all
> the help files and reference manuals and books and I cant' find a built in
> generic file selection dialog box like you get with getfiled lisp
function.
> Surely there is a way? Msdn talks about common dialog and showopen method
> so
> I added the common dialog controls to the controls toolbox and created a
new
> form then dragged a common control to the form.
> then in the code for the form I put
> Sub test()
> ShowSave.me
> 'ShowOpen.me
> End Sub
> and tried running the sub but the form shows up blank. I thought the
method
> was going to show an acutal open file dialog box ready made or an Saveas
> dialog box, depending on which method you used.
> whats weirder is the following lines all have the same effect
>
> showsave.me
> showsave
> form1.showsave
> commondialog1.show
>
> how is that possible???
>
> Obviously I missing something here. ...yeah like a brain maybe???....
> So far I've been creating forms, using drivelistboxes, dirlistboxes, and
> filelistboxes, linked with events to use as a workaround but I have the
> feeling I'm just being really dumb here and there's some ready made and
easy
> to use thingamagigger here somewhere?
> Any help?
>
> I also tried
> Sub test()
> ThisDrawing.SendCommand "(getfiled nil nil nil nil)" & vbCr
> End Sub
> but get the error bad argument type: stringp nil in autocad
>
> and this
> ThisDrawing.SendCommand "(getfiled "Directory Listing" "" "" 2)"& vbCr
> and get the error expected end of statement in the vbaide.
> but
> (getfiled "Directory Listing" "" "" 2)
> works at the command line.
> I'se confuzzed....
>
*Propst, Mark
Message 8 of 13 (146 Views)

file dialog control in vba? Thanks Randall

03-22-2001 06:07 AM in reply to: *markP
Thanks, Randall
geezzzze, ya mean I'm not missing some obvious built in dialog?
so basically one word in lisp "getfiled" = 150 lines of vba and api and etc!
yikes! i'm gonna have to study this one for a while.
Mark

Randall Rath wrote in message
news:4D7CD10CCE867E263B4FEAD83A35BC00@in.WebX.maYIadrTaRb...
> Hi Mark,
>snip>
*Propst, Mark
Message 9 of 13 (146 Views)

file dialog control in vba? Thanks John but I still don't get it!

03-22-2001 09:53 PM in reply to: *markP
Thanks John,
Yes, I have read that several times and even cut and pasted the example code
into a form code page to test it but I'm missing something really basic due
to my innate ignorance!
I inserted a new form into a blank project.
Then I inserted a commondialog control onto the form.
Then I pasted the example code from the help file you mention into the code
page for that form.
Then I ran that sub example.
Then blank userform1 appears but no open file dialog.
I also tried pasting it into a module code page. but get the variable not
defined error...
What am I doing wrong?
thank everyone for their help.
Mark

John Goodfellow wrote in message
news:076ED995C49891E2B82A258094D589E2@in.WebX.maYIadrTaRb...
> FYI, you may find the help file for Comdlg32.dll in
> C:\WINDOWS\HELP\CMDLG98.CHM.
> --
> John Goodfellow
> irtf'nm
> use 'microtouch' in address to email
>
> "markP" wrote in message
> news:smileyvery-happy:F8F016D5C8531E85FD0238FB85332EB@in.WebX.maYIadrTaRb...
> > I know this is probably a really dumb question, but I can't find the
stock
> > file dialog box in vba. I assume there is one but I've been searching
all
> > the help files and reference manuals and books and I cant' find a built
in
> > generic file selection dialog box like you get with getfiled lisp
> function.
> > Surely there is a way? Msdn talks about common dialog and showopen
method
> > so
> > I added the common dialog controls to the controls toolbox and created a
> new
> > form then dragged a common control to the form.
> > then in the code for the form I put
> > Sub test()
> > ShowSave.me
> > 'ShowOpen.me
> > End Sub
> > and tried running the sub but the form shows up blank. I thought the
> method
> > was going to show an acutal open file dialog box ready made or an Saveas
> > dialog box, depending on which method you used.
> > whats weirder is the following lines all have the same effect
> >
> > showsave.me
> > showsave
> > form1.showsave
> > commondialog1.show
> >
> > how is that possible???
> >
> > Obviously I missing something here. ...yeah like a brain maybe???....
> > So far I've been creating forms, using drivelistboxes, dirlistboxes, and
> > filelistboxes, linked with events to use as a workaround but I have the
> > feeling I'm just being really dumb here and there's some ready made and
> easy
> > to use thingamagigger here somewhere?
> > Any help?
> >
> > I also tried
> > Sub test()
> > ThisDrawing.SendCommand "(getfiled nil nil nil nil)" & vbCr
> > End Sub
> > but get the error bad argument type: stringp nil in autocad
> >
> > and this
> > ThisDrawing.SendCommand "(getfiled "Directory Listing" "" "" 2)"& vbCr
> > and get the error expected end of statement in the vbaide.
> > but
> > (getfiled "Directory Listing" "" "" 2)
> > works at the command line.
> > I'se confuzzed....
> >
>
*Propst, Mark
Message 10 of 13 (146 Views)

file dialog control in vba?Thanks walter but url doesn't work for me.

03-22-2001 09:59 PM in reply to: *markP
Thanks Walter,
I'd like to check that site out but I keep getting the server not found
error???
Maybe it's temporarily down? or what could I be doing wrong?
I'm cutting and pasting the url so it isn't typo...I tried adding www also,
still no workie.
Mark

Walter wrote in message
news:410609DFFA5909697BC4C8037A8F2C63@in.WebX.maYIadrTaRb...
> Explore around vbDesign http://hypermart.vbdesign.net
> See the 'Tutorials' and 'Source Code' pages.
>
> -- Walter -- http://www.ActiveDwg.com --
>
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.