PDA

View Full Version : AutoCAD VBA DirListBox, FileListBox et DriveListBox



NickyB
2022-06-09, 11:47 AM
Hello to everyone,

I come back on a subject concerning an AutoCAD cartography routine which has not been solved and which still torments me...
It seems that the problem comes of the loss of 32-bit OCX control since moving to VBA 7 and 32/64-bit platforms.

When I run the routine, a DirListBox, a FileListBox and a DriveListBox should appear (see below on the left), but I get the window below on the right:

109119 109120

So I can't select my file for importing this famous .GDR file...

Do you have any idea how to get these commands to appear? or replace them with a button maybe?

I attach the .vbv file.

Thank you in advance for your flashes of genius.

Nicky

Ed Jobe
2022-06-14, 06:29 PM
That's a pretty archaic way of getting a file path. I recommend just using the Windows file dialogs api. I have a couple of classes for the FolderBrowse dialog and the FileBrowse dialog. I'll post them here later.

NickyB
2022-06-15, 07:21 AM
Hello Ed,

Thank you to look into the situation!
Indeed, these are old routines created by a friend of my associate who died several years ago... Since I'm trying to rerun them!

Anyway, I'm waiting for your classes.

Thanks very much

Ed Jobe
2022-06-15, 03:39 PM
Unzip the attached file (thanks to Alexander.Rivilis ) and Import the bas file into the Modules section of your project and then use the following code sample to call the browse folder dialog.

Public Sub GetFolderPath()
Dim sPath As String
sPath = BrowseFolder.FolderBrowse("Select Folder Containing Block Library", CurDir)
If sPath <> "" Then
If BrowseFolder.FolderExists(sPath) Then
MsgBox sPath
End If
End If
End Sub

The code below shows how to use the FileDialogs 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

NickyB
2022-06-20, 04:50 PM
Hello Ed,

Thank you very much for the files and codes.
Unfortunately, my VBA comprehension and writing skills are very limited compared to LISP.

I am learning the basics of VBA. Could you give me a little more detail on the procedure?
Should I use the FileDialogs.cls file?

Thanks in advance,

Ed Jobe
2022-06-20, 05:09 PM
save the files to disc and unzip them. Then open the VBAIDE, and in the project tree on the left, right click on the Modules section and select Import Module. Then select the bas file. Now right click on the Classes section and import the cls file. Now you can use the code. I believe there are code samples in them.

NickyB
2022-06-20, 06:15 PM
Ok, I understood that. What I don't understand is how to make the link between Module (browse folder dialog) and the FileDialogs class (OpenFile).

Where should I paste the codes?
I paste the GetFolderPath code on the "OPEN" CommandButton in my Form?
How to call OpenFile afterwards?

Ed Jobe
2022-06-20, 07:18 PM
Maybe you should start by explaining what you want to do.

Here's a command I wrote that uses the FileDialogs. It imports save views from a template into the current dwg.


Public Sub ImportViews()
On Error GoTo ErrHandler:
Dim strAppPath As String
Dim objFile As FileDialogs
Dim strFilter As String
Dim strFileName As String
Dim strTempName As String
Dim fso As Scripting.FileSystemObject
Dim fsoFile As Scripting.file
Dim objView As AcadView
Dim dbxdoc As Object 'AxDbDocument
Dim colSelected As Collection

Set fso = CreateObject("Scripting.FileSystemObject")
strAppPath = ThisDrawing.Application.Preferences.Files.TemplateDwgPath
Set objFile = New FileDialogs
'desc,filter combinations must all be separated with pipe char "|"
strFilter = "Templates (*.dwt)|*.dwt|Drawings (*.dwg)|*.dwg"
objFile.OwnerHwnd = ThisDrawing.hWnd 'bind the dialog to the window
objFile.Title = "Select file to views from."
objFile.StartInDir = strAppPath
objFile.Filter = strFilter
'return a valid filename
GetFile:
strFileName = objFile.ShowOpen
If Not strFileName = vbNullString Then
'check for dwt, ObjectDbx can only open dwg's
If fso.GetExtensionName(strFileName) = "dwt" Then
'copy template to temp folder as dwg
strTempName = ThisDrawing.Application.Preferences.Files.TempFilePath & fso.GetBaseName(strFileName) & ".dwg"
fso.CopyFile strFileName, strTempName, True
strFileName = strTempName
End If
'use ObjectDbx to get page setups in the background
'open dbxdoc
Set dbxdoc = AcadApplication.GetInterfaceObject("ObjectDBX.AxDbDocument")
dbxdoc.Open strFileName
'get a list of plot configs to import
Set frmImportViews.Views = dbxdoc.Views
frmImportViews.FillLists
frmImportViews.Show
Set colSelected = frmImportViews.SelectedViews
If colSelected.Count = 0 Then Exit Sub
Unload frmImportViews
'copy the views from the template
For Each objView In colSelected
ThisDrawing.Views.Add objView.Name
Next objView
Set dbxdoc = Nothing
'delete temp file if exists
If Not strTempName = vbNullString Then fso.DeleteFile strTempName
Else 'user clicked cancel in file dialog
End If
Set objFile = Nothing
Set fso = Nothing
Exit Sub

ErrHandler:
Select Case Err.Number
Case Is = 70, Is = -2147467259
MsgBox Err.Number & " - " & Err.Description & vbCrLf & "File may be in use or read-only.", vbCritical, "Import Views"
GoTo GetFile
Case Else
MsgBox Err.Number & " - " & Err.Description, vbCritical, "Import Views"
End Select


End Sub

Ed Jobe
2022-06-20, 07:37 PM
I forgot about your dialog at the beginning of this thread. Just add the code to open the dialog in the OnClick method of your Open button. I don't understand all that your dialog is supposed to do. The FileDialogs class just gets a string for the path of the selected file. You can use that with the Shell Object or the AcadApplication.Documents collection to open it.