Close Method (ActiveX)

Closes the specified drawing, or all open drawings.

Supported platforms: Windows only

Signature

VBA:

object.Close [SaveChanges] [, FileName]
object

Type: Document, Documents

The objects this method applies to.

SaveChanges

Access: Input-only; optional

Type: Variant

Specifies if the drawing is to be saved or not. (Not applicable when called from Documents collection.)

  • True: Save the drawing.
  • False: Do not save the drawing.
FileName

Access: Input-only; optional

Type: String

The name to assign to the drawing. If no name is provided when saving a drawing for the first time, the drawing will be saved using the VBA project information: ThisDrawing.Path\ThisDrawing.Name. (Not applicable when called from Documents collection.)

Return Value (RetVal)

No return value.

Remarks

If there are no changes to the drawing, SaveChanges and FileName are ignored. If there are changes to the drawing, SaveChanges specifies whether changes should be saved. The default value for the SaveChanges parameter is True.

Calling this method from the Documents collection in MDI mode will close all open drawings. To close a single drawing, call this method from the drawing to be closed.

You cannot close a drawing from inside an event handler for that drawing.

Note: Closing a drawing destroys the Document object. Never try to reference an object once it has been destroyed, or in this case, closed. In-process clients (VBA macros) may notice that objects are not destroyed until the subroutine is exited. However, references to destroyed objects are not recommended at all, even in the in-process code.

Examples

VBA:

Sub Example_Close()
    ' This example cycles through the documents collection
    ' and closes all open drawings using the Close method.

    Dim DOC As AcadDocument
    
    ' If there are no open documents, then exit
    If Documents.count = 0 Then
        MsgBox "There are no open documents!"
        Exit Sub
    End If
    
    ' Close all open documents
    For Each DOC In Documents
        If MsgBox("Do you wish to close the document: " & DOC.WindowTitle, vbYesNo & vbQuestion) = vbYes Then
            If DOC.FullName <> "" Then
                DOC.Close
            Else
                MsgBox DOC.name & " has not been saved yet, so it will not be closed."
            End If
        End If
    Next
End Sub

Visual LISP:

(vl-load-com)
(defun c:Example_Close()
    ;; This example cycles through the documents collection
    ;; and closes all open drawings using the Close method
    ;; except the current drawing.
    (setq acadObj (vlax-get-acad-object))
    (setq curDoc (vla-get-ActiveDocument acadObj))
    (setq docs (vla-get-Documents acadObj))

    ;; Close all open documents and discard changes, except for the current drawing
    (vlax-for doc docs
        (if (/= (vla-get-Name doc) (vla-get-Name curDoc))
	    (progn
	        (alert (strcat "Closing " (vla-get-Name doc) " file."))
                (vla-Close doc :vlax-false)
	    )
        )
    )
)