Valued Contributor
Posts: 53
Registered: ‎07-07-2004
Message 1 of 4 (1,380 Views)
Accepted Solution

MDIActiveDocument and DocumentActivated

1380 Views, 3 Replies
09-21-2012 01:16 PM

I have set a handler for the DocumentActivated event message but it doesn't get called when I set the MDIActiveDocument on the document manager object. It gets called when I swich windows, but not when the active document is initially set. Any ideas?


[CommandMethod("IFX_NEW_ASSEMBLY", CommandFlags.Session)]

void NewAssembly()


     Document doc = Application.DocumentManagerr.Add("");

     Application.DocumentManager.MDIActiveDocument = doc;



Should the DocumentActivated event be fired with the above code. P.S. I am positive the event handler is hooked up correctly.


- Mike B

*Expert Elite*
Posts: 959
Registered: ‎04-27-2009
Message 2 of 4 (1,368 Views)

Re: MDIActiveDocument and DocumentActivated

09-21-2012 02:31 PM in reply to: mbujak

How do you wire up the DocumentActivated event?


Following code works for me:


using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;

[assembly: CommandClass(typeof(DocumentActevatedEvent.MyCommands))]
[assembly: ExtensionApplication(typeof(DocumentActevatedEvent.MyCommands))]

namespace DocumentActevatedEvent
    public class MyCommands : IExtensionApplication
        #region IExtensionApplication Members

        public void Initialize()
            Document dwg = Application.DocumentManager.MdiActiveDocument;
            Editor ed = dwg.Editor;


                Application.DocumentManager.DocumentActivated += DocumentManager_DocumentActivated;

            catch (System.Exception ex)


        private void DocumentManager_DocumentActivated(object sender, DocumentCollectionEventArgs e)
            e.Document.Editor.WriteMessage("\n{0} activated.", e.Document.Name);

        public void Terminate()



        [CommandMethod("AddNewDoc", CommandFlags.Session)]
        public static void RunMyCommand()
            Document dwg = Application.DocumentManager.Add(null);


 I use AutoCAD 2012 and VS2010.


Actually, the second line of code in the CommandMethod:




is not needed. When a new document is added, AutoCAD automatically set it as the active document. That is, the DocumentActivated event is triggered at the end of Add() method, not by the code App...MdiActiveDocument=dwg;


Valued Contributor
Posts: 53
Registered: ‎07-07-2004
Message 3 of 4 (1,347 Views)

Re: MDIActiveDocument and DocumentActivated

09-22-2012 06:52 AM in reply to: norman.yuan

No different than your code except I have an AppData class that manages custom DocData class collection. The AppData has an Initialization method where the wiring is done. This gets called by the extension class initialization.


I removed the assignment and it seems to fire now.





New Member
Posts: 2
Registered: ‎03-07-2013
Message 4 of 4 (1,148 Views)

Re: MDIActiveDocument and DocumentActivated

03-13-2013 09:17 PM in reply to: norman.yuan

Is anyone else having this same dwg activate problem, with R2013? I have a large project that has stopped working since the 2013 upgrade.


I have substituted the new call DocumentCollectionExtension.Add(acDocMgr,templatefile) - and I am using CommandFlags.Session - but the document is still not activated until the command has finished.


I've distilled it down to a couple of simple test commands:


    <CommandMethod("NewDrawing", CommandFlags.Session)> _
    Public Sub NewDrawing()
        'Create a new drawing and activate it

        Dim strTemplatePath As String = "acad-test.dwt" 'from support path

        Dim acDocMgr As DocumentCollection = Application.DocumentManager
        Dim acDoc As Document = DocumentCollectionExtension.Add(acDocMgr, _
        'the above line should activate the new added document but doesn't
        'acDocMgr.MdiActiveDocument = acDoc  'THIS LINE DOES NOT MAKE THE DWG ACTIVE
        Application.ShowAlertDialog("Active document is now " & acDocMgr.MdiActiveDocument.Name)
        'this stills shows drawing1 until after the command finishes

    End Sub

    <CommandMethod("ActiveDrawing", CommandFlags.Session)> _
    Public Sub ActiveDrawing()
        'Show which drawing is currently active

        Dim acDocMgr As DocumentCollection = Application.DocumentManager

        Application.ShowAlertDialog("Active document is now " & acDocMgr.MdiActiveDocument.Name)
        'this shows Drawing2 if this is run after the NewDrawing command has finished

    End Sub


Is there any updated sample code for 2013 anywhere? I could not get the code in the 2013 Dev Guide (here, search for "LockDoc") to compile as it has not been updated to use the new DocumentCollectionExtension methods. I have substituted the new method calls into that code but still have the activation problem.


You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

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 to get help installing your software.

Ask the Community