Run a Command when a Specific Drawing Opens

Run a Command when a Specific Drawing Opens

Anonymous
Not applicable
1,761 Views
5 Replies
Message 1 of 6

Run a Command when a Specific Drawing Opens

Anonymous
Not applicable

I would like to have a command execute when a drawing is opened. Specifically I want this functionality to be associated with a particular file, not to be executed for all files that are opened. I know that modifications can be made to the ACADDOC.lsp file to change the startup behaviors of AutoCAD when a document opens, but from what I understand this can only be done such that every document that is opened adopts this startup behavior. This is not ideal in my situation.

 

My motivation is this:

 

My company uses an AutoCAD application to convert spreadsheets to blocks in AutoCAD. The blocks that are created are not linked to the excel file, so the user must update the block any time a change is made using a command built in to the application. The company is pretty set on not using OLE's or any different software for uploading spreadsheets. My boss has asked me to find a way to get the spreadsheet-update command to run when a drawing is opened because he finds the process of updating it manually tedious.

 

I've already solved the problem on the excel side with a macro that runs whenever the spreadsheet is saved, and I wrote a macro in AutoCAD's VBA editor to run the command. In excel I'm able to place my macro in the "ThisWorkbook" module and change the header such that the macro is associated with a particular behavior within the workbook (i.e. saving). But when I try to do something similar in AutoCAD I can't seem to get my macro to run correctly.

 

I've already run the AutoCAD macro from within the VBA editor and it executes correctly, but I can't seem to find the correct header that will make the macro run when that drawing is opened. I'm also unsure if I'm writing my macro into the correct location.buggy_CAD_macro.PNG

I've had a lot more experience writing macros for excel, and I seem to be having a hard time understanding how to run them in AutoCAD. I've got a little bit of experience with LISP, and if that's the only solution I can hopefully figure out how to get it working, but a solution with Visual Basic is definitely preferable. That being said, I'm also interested in learning how to write LISP routines for AutoCAD, so feel free to toss in any good links you might have for a beginner.

 

Definitely appreciate any help, sorry if this problem is oddly specific.

0 Likes
1,762 Views
5 Replies
Replies (5)
Message 2 of 6

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

>> I know that modifications can be made to the ACADDOC.lsp file to change the startup behaviors

>> of AutoCAD when a document opens, but from what I understand this can only be done such

>> that every document that is opened adopts this startup behavior.

LISP or VBA, in both cases you can run an event-handler for "opening a file", but not for "opening a specific file".

So the chance is to have such an event-handler and within that handler your code have to decide it the file now opened is such a "specific file" or not ... and if yes, then start your handling whatever you like to do.

 

Just be careful that the code (to identify if that are drawings needing some work) is small end does not need too much processor power, otherwise every drawing will open slow.

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 3 of 6

Anonymous
Not applicable

Alfred,

 

Thank you for the prompt reply. I definitely see the detriment in adding an event handler that would check every drawing to see if it is the one I'm interested in. I guess what I'm really trying to do is embed my code into the drawing which I am working with. Is it possible to associate code with a file, or is AutoCAD's code always specific to the user profile?

 

Again, thank you for the help!

0 Likes
Message 4 of 6

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

>> what I'm really trying to do is embed my code into the drawing

Using VBA there is an option to embed the code into the drawing, but contrary to Excel or Word it is not the commonly used option.

One one side not every AutoCAD has the VBA-Enabler installed, so even if your code is embedded it would not run.

On the other side embedded codes are risky (thinking of virus).

 

So you only can control the AutoCAD's you have in your environment, and there you can make sure you have either ACADDOC.lsp modified or there is the VBA-Enabler installed and the default macros already loaded with every start of AutoCAD.

 

HTH, - alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 5 of 6

Ed__Jobe
Mentor
Mentor

You mentioned that you have the spreadsheet do some updating when it is saved. You can modify this process to open acad with the specific dwg and update the dwg. You just need to know the dwg filename.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 6 of 6

Anonymous
Not applicable

Ed,

 

Thanks for the reply! This might work but the spreadsheet is a template which will be copied and filled out for several different jobs. I think the only way I could get this to work would be to hardcode the full path of the drawing whenever a new job is set up. Even still, it may end up being easy enough to just edit in this feature at the beginning of each job, this solution may be the way to go.

 

Thanks for your insight!

 

Ben

0 Likes