Valued Contributor
Posts: 95
Registered: ‎01-29-2007
Message 1 of 3 (629 Views)
Accepted Solution

How to use CloseAndDiscard in 2013?

629 Views, 2 Replies
12-05-2012 11:47 AM

I have a vb.net application that works fine in acad2012.  I converted it to 2013 (new references, including accoremgd.dll) and I'm aware of the document extension aspect of the CloseAndDiscard method, although I don't fully understand the "big split" and its details.  I'm using Visual Studio 2010 Express.


Everything works until the end of the application where I need to close the drawing.  I'll paste in the last bit of code (this thing wraps the longer code lines):


acDoc.Database.SaveAs(OPORPath,True, DwgVersion.AC1024, acDoc.Database.SecurityParameters)


Application.SetSystemVariable("ISAVEBAK", 1)


Application.SetSystemVariable("FILEDIA", 1)


MsgBox("POR layout has been saved to the Official folder")


'End the last transaction



'remove the documentlock








So, just before EndSub, I'm trying to close the drawing (not exit ACAD, just close the drawing).  Does anyone know what I'm missing here?  I've been looking online to find how other users do this and this code is what I've found.  But it's not working for me. 


Thanks so much,




Posts: 209
Registered: ‎09-15-2011
Message 2 of 3 (619 Views)

Re: How to use CloseAndDiscard in 2013?

12-05-2012 12:38 PM in reply to: edweberg



You should set CommandFlags.Session in your custom AutoCAD command. Otherwise, it will fail to close the current AutoCAD drawing session (the opened document, not AutoCAD application).


DocumentExtension.CloseAndSave(Application.DocumentManager.MdiActiveDocument) will save and the close the working document.

DocumentExtension.CloseAndDiscard will close document without saving. Here is the code for testing:


<CommandMethod("CloseAndSave", CommandFlags.Session)> _
Public Shared Sub CloseAndSave()
    Dim doc As Document = Application.DocumentManager.MdiActiveDocument
    Dim db As Database = doc.Database
    Using doc.LockDocument()
        Using trans As Transaction = db.TransactionManager.StartTransaction()
            Dim blockTable = DirectCast(trans.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
            Dim modelSpace = DirectCast(trans.GetObject(blockTable(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)
            Dim line = New Line(Point3d.Origin, New Point3d(5, 5, 0))
            trans.AddNewlyCreatedDBObject(line, True)
        End Using
    End Using
End Sub



Valued Contributor
Posts: 95
Registered: ‎01-29-2007
Message 3 of 3 (601 Views)

Re: How to use CloseAndDiscard in 2013?

12-05-2012 01:40 PM in reply to: khoa.ho

Such a tiny thing, but it made all the difference.  Thank you!


Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.