See the top rated post in this thread. Click here

Results 1 to 3 of 3

Thread: Execute command on DocumentCreated event problem

  1. #1
    Member
    Join Date
    2011-04
    Posts
    2
    Login to Give a bone
    0

    Question Execute command on DocumentCreated event problem

    Hi!
    I'm trying to execute command after the file with a specific name is loaded. But, I get an "eNoDocument" exception. What am I doing wrong?
    Code:
    void DocumentManager_DocumentCreated(object sender, DocumentCollectionEventArgs e)
    {
        DocumentCollection docCollection = sender as DocumentCollection;
        if (docCollection != null && Path.GetFileName(docCollection.MdiActiveDocument.Name).ToLower() == "somefile.dwg")
        {
            try
            {
                docCollection.MdiActiveDocument.SendStringToExecute("MYMEGACOMMAND", false, false, false);
            }
            catch(System.Exception ex) { }
        }
    }

  2. #2
    AUGI Addict sinc's Avatar
    Join Date
    2004-02
    Location
    Colorado
    Posts
    1,986
    Login to Give a bone
    1

    Default Re: Execute command on DocumentCreated event problem

    Try using e.Document instead of docCollection.MdiActiveDocument.

    The DocumentCreated event is called before the document is made current.

  3. #3
    Member
    Join Date
    2011-04
    Posts
    2
    Login to Give a bone
    0

    Lightbulb Re: Execute command on DocumentCreated event problem

    Thank you, for reply.
    I'm find the solution for my problem:
    I subscribed to event DocumentActivated and use ExecuteInApplicationContext.

    Code:
    private void DocumentManager_DocumentActivated(object sender, DocumentCollectionEventArgs e)
    {
        DocumentCollection docCollection = sender as DocumentCollection;
    
        if (docCollection != null && Path.GetFileName(docCollection.MdiActiveDocument.Name).ToLower() == "somename.dwg")
        {
            Application.DocumentManager.ExecuteInApplicationContext(ExecuteCommand, sender);
        }
    }
    private void ExecuteCommand(object sender)
    {
        DocumentCollection docCollection = sender as DocumentCollection;
        docCollection.MdiActiveDocument.SendStringToExecute("MyMegaCommand ", true, false, false);
    }

Similar Threads

  1. Replies: 9
    Last Post: 2014-07-07, 02:40 AM
  2. SAVE command execute the PLOT command after...
    By HDC0NY in forum CAD Management - General
    Replies: 1
    Last Post: 2014-01-21, 08:07 PM
  3. Problem Can’t execute a command
    By rrdavalos in forum AutoLISP
    Replies: 22
    Last Post: 2007-08-20, 09:13 PM
  4. VB.Net DocumentCreated event
    By KevinBarnett in forum Dot Net API
    Replies: 0
    Last Post: 2006-01-31, 08:11 AM
  5. Create toolbar button to execute command
    By rwbaker in forum AutoCAD Customization
    Replies: 6
    Last Post: 2005-11-18, 04:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •