Open, Save, and Close Drawings

AutoCAD ActiveX

 
Open, Save, and Close Drawings
 
 
 

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

This example uses the Open method to open an existing drawing. The VBA Dir function is used to check for the existence of the file before trying to open it. You should change the drawing file name or path to specify an existing AutoCAD drawing file on your system.

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.

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.

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:

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