About Opening, Saving, and Closing Drawings (ActiveX)

The Documents collection and Document object provide access to the AutoCAD file functions.

To create a new drawing, or open an existing drawing, use the methods on the Documents collection. The Add method creates a new drawing and adds that drawing to the Documents collection. The Open method opens an existing drawing. There is also a Close method on the Documents collection that closes all the drawings open in the AutoCAD session.

Use either the Save or SaveAs method to save a drawing. Occasionally you will want to check if the active drawing has any unsaved changes. It is a good idea to do this before you quit the AutoCAD session or start a new drawing. Use the Saved property to make sure that the current drawing does not contain any unsaved changes.

To import and export drawings, use the Import and Export methods on the Document object.

Open an existing drawing

The Open method can be used to open an existing drawing. You can check for the existence of the file before trying to open it with Dir function in VBA and the findfile function in AutoLISP.

Note: For the example, make sure to change the drawing file name or path to specify an existing AutoCAD drawing file on your system.
AutoLISP
(vl-load-com)
(defun c:Ch3_OpenDrawing ( / )
    (setq acadObj (vlax-get-acad-object)
          docs (vla-get-Documents acadObj))
  
    (setq dwgName "c:\\Autodesk\\campus.dwg")
    (if (findfile dwgName)
        (vla-Open docs dwgName)
        (alert (strcat "File " dwgName " does not exist."))
    )
)
VBA (AutoCAD Only)
Sub Ch3_OpenDrawing()
  Dim dwgName As String
  dwgName = "c:\campus.dwg"
  If Dir(dwgName) <> "" Then
    ThisDrawing.Application.Documents.Open dwgName
  Else
    MsgBox "File " & dwgName & " does not exist."
  End If
End Sub

Create a new drawing

This example uses the Add method to create a new drawing based on the default template.

AutoLISP
(vl-load-com)
(defun c:Ch3_NewDrawing ( / )
    (setq acadObj (vlax-get-acad-object)
          docs (vla-get-Documents acadObj))

    (setq docObj (vla-Add docs))
)
VBA (AutoCAD Only)
Sub Ch3_NewDrawing()
  Dim docObj As AcadDocument
  Set docObj = ThisDrawing.Application.Documents.Add
End Sub

Save the active drawing

This example saves the active drawing under its current name and again under a new name.

AutoLISP
(vl-load-com)
(defun c:Ch3_SaveActiveDrawing ( / )
    (setq acadObj (vlax-get-acad-object)
          doc (vla-get-ActiveDocument acadObj))
  
    ;; Save the active drawing under the current name
    (vla-Save doc)

    ;; Save the active drawing under a new name
    (vla-SaveAs doc "MyDrawing.dwg")
)
VBA (AutoCAD Only)
Sub Ch3_SaveActiveDrawing()
  ' Save the active drawing under the current name
  ThisDrawing.Save

  ' Save the active drawing under a new name
  ThisDrawing.SaveAs "MyDrawing.dwg"
End Sub

Test if a drawing has unsaved changes

This example checks to see if there are unsaved changes and verifies with the user that it is OK to save the drawing (if it is not OK, skip to the end). If OK, use the Save method to save the current drawing, as shown here.

AutoLISP
(vl-load-com)
(defun c:Ch3_TestIfSaved ( / )
    (setq acadObj (vlax-get-acad-object)
          doc (vla-get-ActiveDocument acadObj)
          utilObj (vla-get-Utility doc))
    
    ;; Define and call InitializeUserInput to set up the keywords
    (setq keywordList "Yes No")
    (vla-InitializeUserInput utilObj 0 keywordList)
	  
    (if (= :vlax-false (vla-get-Saved doc))
        (if (/= "No" (vla-GetKeyword utilObj "\nDo you wish to save this drawing? [Yes/No] <Yes>: "))
            (vla-Save doc)
        )
    )
)
VBA (AutoCAD Only)
Sub Ch3_TestIfSaved()
  If Not (ThisDrawing.Saved) Then
    If MsgBox("Do you wish to save this drawing?", _
      vbYesNo) = vbYes Then
      ThisDrawing.Save
    End If
  End If
End Sub