Selecting file with standard AutoCAD file/open dialog

By Xiaodong Liang

Issue
Can I gain access to the "Open File Dialog" (with preview) found only in AutoCAD with VBA?

Solution
You can do this through the communication interface with AutoLISP and AutoCAD. The AutoLISP function, getfiled, behaves as the AutoCAD "Open File Dialog" and allows .DWG files to be previewed.

VBA:

Public Sub OpenDialog()
   Dim fileName As String
   'Using the SendCommand method, send getfiled AutoLISP expressions to the AutoCAD command line.
   'Set the return value to a user-defined system variable USERS1.
   ThisDrawing.SendCommand "(setvar " & """users1""" & "(getfiled " & """Select a DWG File""" & """c:/program files/acad2012/""" & """dwg""" & "8)) "
   'Use the GetVariable method to retrieve this system variable to store the selected file name
   fileName = ThisDrawing.GetVariable("users1")
   MsgBox "You have selected " & fileName & "!!!", , "File Message"
End Sub

VB.NET

 

Public Sub OpenDialog(AcadApp As AcadApplication)
        Dim ThisDrawing As AcadDocument
        ThisDrawing = AcadApp.ActiveDocument
 
        Dim fileName As String
        'Using the SendCommand method, send getfiled               ‘AutoLISP expressions to the AutoCAD command 
       ‘line.Set the return value to a user-defined  
       ‘system variable USERS1.
        ThisDrawing.SendCommand("(setvar " &  
                                 """users1""" & 
                                  "(getfiled " & 
                          """Select a DWG File""" & 
                    """c:/program files/acad2012/""" & 
                   """dwg""" & "8)) ")
        'Use the GetVariable method to retrieve this  
        ‘system variable to store selected file name
        fileName = ThisDrawing.GetVariable("users1")
        MsgBox("You have selected " &
                fileName & "!!!", , 
                 "File Message")
    End Sub

Comments

3 responses to “Selecting file with standard AutoCAD file/open dialog”

  1. Tony Tanzillo Avatar
    Tony Tanzillo

    It’s truly shameful that VBA programmers must resort to this sort of kludgery to solve a simple problem like this.
    But, if they have to, then VL.Application would be a better way to go about it.

  2. but what if I want the user to select multiple files (as per the old commondialog.
    I have tried to use the API but fails on the call to GetOpenFileName. returns 0 without showing dialog.

  3. Wow… Where’s this beauty in the Developer Documentation?

Leave a Reply to BlackBoxCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading