getfiled in VBA

getfiled in VBA

Anonymous
Not applicable
851 Views
6 Replies
Message 1 of 7

getfiled in VBA

Anonymous
Not applicable
anybody has some sample code
on the equivalent of getfiled in VBA

tia
mark
0 Likes
852 Views
6 Replies
Replies (6)
Message 2 of 7

Anonymous
Not applicable
Hi i'm not sure about what you're asking,
Are you asking about the getfield or user input..
if so you can use acadutility methods in VBA.
ThisDrawing.Utility.GetInput
cheers,
varadan

"Mark" wrote in message news:f12747a.-1@WebX.maYIadrTaRb...
>
> anybody has some sample code
> on the equivalent of getfiled in VBA
>
> tia
> mark
>
>
0 Likes
Message 3 of 7

Anonymous
Not applicable
Unfortunately, it's not nearly as easy to get a file, Mark.

The consensus seems to point to using Windows API calls. I've seen a few
examples on different websites, but due to my limited knowledge of VBA,
they're too complicated for me to use. (I don't like using code that I
don't understand, yet.)

I've recently been using the Microsoft Scripting Runtime's FileSystemObject
for going through drives, folders, etc. So far, I haven't gone beyond
displaying a list of drives in a form, but if you'd care to see how I did
that, let me know. It's quite simple, and I don't think creating a getfiled
type form this way would be too difficult.

"Mark" wrote in message news:f12747a.-1@WebX.maYIadrTaRb...
>
> anybody has some sample code
> on the equivalent of getfiled in VBA
>
> tia
> mark
>
>
0 Likes
Message 4 of 7

Anonymous
Not applicable
It all depends on what you want to do with the file, but here's some
cut&paste:

If you don't mind making a reference to Excel, this is the easiest way I've
found:
Autocad:
sFile = Excel.Application.GetOpenFilename _
("AutoCad Drawing Files (*.dwg), *.dwg", , _
"Select Drawing", , False)

Excel:
sFile = Excel.Application.GetOpenFilename _
("Excel Files (*.xls), *.xls", , _
"Select File", , False)


Or, you can use the windows api to call the open file dialog, this example
is a bit more complex:

'windows API calls to the operating system
Private Declare Function GetOpenFileName Lib "COMDLG32" Alias
"GetOpenFileNameA" _
(fileF As uFile) As Long
Private Declare Function GetSaveFileName Lib "COMDLG32" Alias
"GetSaveFileNameA" _
(fileF As uFile) As Long


'user defined type - information to collect during file browse
'all are required when calling system level .dll's, only some are used here
Private Type uFile
lSizeF As Long
lOwnerF As Long
lTempF1 As Long
sFilter As String
sTempF1 As String
lTempF2 As Long
lFilterF As Long
sFileF As String
lFileLength As Long
sFileTitleF As String
lTitleLength As Long
sDirInitF As String
sDiaTitleF As String
lFlag As Long
iTempF1 As Integer
nFileExtension As Integer
sExtF As String
lTempF5 As Long
lTempF6 As Long
lTempF7 As Long
End Type

'Passed variables:
'sTagExt = extension desired in search
'sCaption = text to appear in title bar of dialog
'bSave = flag telling control to set button to save, not open
Public Function FILE_BROWSE(Optional sTagExt As String, _
Optional sCaption As String, Optional bSave As Boolean) As
String

'maximum string length
Const lMax = 260
'desktop is starting point for browse
Const sDesktop = &H0
'flags:
'overwrite prompt
Const lovrwriteyes = &H2
'path must exist
Const lpathyes = &H800
'file must exist
Const lfileyes = &H1000
'hide read only
Const lreadonlyno = &H4

'new instance of user defined type
Dim uNewFile As uFile

'start with nothing
FILE_BROWSE = ""

'set up the udt given information
With uNewFile
.lSizeF = Len(uNewFile)
If bSave Then
.lFlag = (lfileyes) And (lpathyes) And (lreadonlyno) And
(lovrwriteyes)
Else
.lFlag = (lfileyes) And (lpathyes) And (lreadonlyno)
End If
.lOwnerF = 0
.sDirInitF = ""
.sExtF = ".xls"
If Len(sCaption) > 1 Then
.sDiaTitleF = sCaption
Else
.sDiaTitleF = sBrzFiTitle
End If
'need to adjust filter to match format selection
.sFilter = EXT_TO_FILTER(sTagExt)
.lFilterF = 0
.sFileF = Space(lMax)
.lFileLength = lMax
.sFileTitleF = Space(lMax)
.lTitleLength = lMax

'mode set by saving or opening
If bSave Then
'save dialog
If GetSaveFileName(uNewFile) Then
FILE_BROWSE = LEFT_NULL(.sFileF)
Else
FILE_BROWSE = ""
End If
'open dialog
Else
If GetOpenFileName(uNewFile) Then
FILE_BROWSE = LEFT_NULL(.sFileF)
Else
FILE_BROWSE = ""
End If
End If

End With

End Function



--
Kevin


"Mark" wrote in message news:f12747a.-1@WebX.maYIadrTaRb...
>
> anybody has some sample code
> on the equivalent of getfiled in VBA
>
> tia
> mark
>
>
0 Likes
Message 5 of 7

Anonymous
Not applicable
"Mark" wrote ...
> anybody has some sample code on the equivalent of getfiled in VBA?

Woooo - I had to dig 'way back in my archives for this one. I wrote this back
in early 2000 I think, it was picked up by Autodesk to re-print in their VBA
Technical Newsletter. That's the version listed below, so it's been reformatted a few
times and the comments tend to run into code awkwardly, but hopefully it will help.
I've been out of VBA for a while now, so be careful of the section 'Check in
Drawing's Directory' - the ThisDrawing.Name and ThisDrawing.FullName properties may
return different strings that they used to in the version of ACAD I used back then.

- - - - - copy of code below - - - - -
The code shown here has been modified in format only
to fit within the confines of the newsletter format.

'----FindFile----------------------------------------
' This function looks for the specified filename in
' the current directory, the current drawing's
' directory, any of AutoCAD's support paths, and the
' directory ACAD is running from. If it finds
' the file, it will return the full path+filename.
' If not, it will return an empty string. If the
' specified filename includes any backslashes ( \ ),
' it is assumed that it's a full path+filename
' which must be checked to see if it exists, rather
' than searching amongst AutoCAD's paths. This
' function does not really work with wildcards. If it
' finds a file which matches the filespec, it
' will return the path plus the original filespec,
' eg. C:\*.bat, rather than C:\Autoexec.bat .
'-----------------------------------------------------
Public Function FindFile(FileName As String) As String
Dim Path As String 'holds path to check
Dim SupportPath As String 'copy of ACAD's support paths
Dim Pos As Integer 'position within SupportPath of separator
'----if supplied name has embedded '\' assume it is full path+filename
If InStr(1, FileName, "\") > 0 Then 'look for embedded backslash
FindFile = FileName 'copy supplied path+filename for result
On Error Resume Next 'in case of disk error
'if doesn't exist, change result to ""
If Dir$(FileName) = "" Then FindFile = ""
If Err Then FindFile = "" 'if some file error, indicate failure
Exit Function 'return result to calling routine
End If
'----check in current directory---------------------
'construct path+filename to check
FindFile = CurDir$ & "\" & FileName
'if exists, return it as the result
If Dir$(FindFile) <> "" Then Exit Function
'----check in drawing's directory-------------------
'in ACAD 14.01, same as ThisDrawing.Path
Path = ThisDrawing.FullName
'ensure not a new, unnamed drawing
If Path <> "" Then
'chop drawing name from end of 'path'
Path = Left$(Path, Len(Path) - Len(ThisDrawing.Name))
'construct path+filename
FindFile = Path & FileName
'if exists, return it as the result
If Dir$(FindFile) <> "" Then Exit Function
End If
'----check ACAD's support directories----------------
'copy of support paths
SupportPath = ThisDrawing.Application.Preferences.SupportPath
'ensure has trailing '\'
If Right$(SupportPath, 1) <> "\" Then SupportPath = SupportPath & "\"
'check each one until none left
Do While Len(SupportPath) > 0
'look for separator between paths
Pos = InStr(1, SupportPath, ";")
'if found a separator character...
If Pos > 0 Then
'...extract path before it
Path = Left$(SupportPath, Pos - 1)
'no separator, so...
Else
'...must be last path in SupportPath
Path = SupportPath
End If
'make sure has trailing backslash
If Right$(Path, 1) <> "\" Then Path = Path & "\"
'construct path+filename to check
FindFile = Path & FileName
'if exists, return as result
If Dir$(FindFile) <> "" Then Exit Function
'chop off path just checked
SupportPath = Right$(SupportPath, Len(SupportPath) - Len(Path))
'probably separator still there
If Left$(SupportPath, 1) = ";" Then
'chop it off too
SupportPath = Right$(SupportPath, Len(SupportPath) - 1)
End If
'check next path in SupportPath
Loop
'----check in AutoCAD's program directory-------------
'get path to ACAD.EXE
Path = ThisDrawing.Application.Path
'make sure has trailing backslash
If Right$(Path, 1) <> "\" Then Path = Path & "\"
'construct path+filename to check
FindFile = Path & FileName
'if exists, return as result
If Dir$(FindFile) <> "" Then Exit Function
'----can't find file, return empty string to indicate failure
FindFile = ""
End Function

'----TestFindFile----------------------------------------
' This routine is used to test the FindFile function.
' It brings up an InputBox so the user can
' enter the name of a file to check for, calls FindFile(),
' then displays the result in a MsgBox.
'--------------------------------------------------------
Public Sub TestFindFile()
Dim res As String 'holds the result of function calls
Dim msg As String messages to present to user
'----prompt user for filename to test
msg = "Enter the filename you want to try with the FindFile function." &
vbCrLf & vbCrLf
msg = msg & "FindFile will look in the current directory, the directory" &
vbCrLf
msg = msg & "containing the current drawing (if any), AutoCAD's support" &
vbCrLf
msg = msg & "directories, and the directory AutoCAD is running from." &
vbCrLf
res = InputBox(msg, "Testing FindFile", "acad14.cfg")
'----use the FindFile function retrieve full path+filename
'if user pressed 'Cancel', res = ""
If res <> "" Then
'search for file
res = FindFile(res)
'FindFile returns "" if can't find file
If res = "" Then
'compose message to that effect and
msg = "Couldn't find that file."
'tell user what happened
MsgBox msg, vbInformation, "Testing FindFile"
Else
'FindFile returned full path+filename
msg = "Found that file in: '" & res & "'."
'show result to user
MsgBox msg, vbInformation, "Testing FindFile"
End If
End If
End Sub
0 Likes
Message 6 of 7

Anonymous
Not applicable
Yikes - Now that posting demonstrates just how long I've been away from VBA &
AutoLisp in ACAD! I mis-read the question as a request for a 'findfile'
replacement, not a 'getfiled' replacement. Sorry for the wanton waste of bandwidth...

...Colin French
0 Likes
Message 7 of 7

Anonymous
Not applicable
GO to AcadX.com, and from memory look in the code section at CommonDialog.cls, a useful class written up by Frank Oquendo that is now in all my routines that need file/folder selection.
0 Likes