Performancing Metrics

Tuesday, August 1, 2017

Can I write a macro to open files automatically in Autocad?

I need a macro that will open a file based on inputs from a drop-down box. Box 1 specifies the first folder, box two the second, and so on until it reaches a file. It then needs to open that file, copy it, and paste it into an open document. Is that possible? If I write an excel macro to do it, will it function in AutoCad?

If you found this post helpful, do my a favor and please press the Facebook “like” button below. 


2 Responses to “Can I write a macro to open files automatically in Autocad?”
  1. Darren B says:

    I would use an Lisp script for that. Are you needing to insert other types of files into the drawing like a txt|doc|xls etc?

  2. Sven says:

    You can write the program in VBA. If you press alt-F11, it will open the visual basic programming tool. You would have to create a form and on that form create the controls (drop downs) you need. Each control would contain it’s own code to make it do whatever it is you want it to do. It can take a while to figure out how it all works but if it’s something you use over and over, the time savings are huge.

    In answer to your Excel question, you can open a drawing from within an Excel macro. (I assume you want to open a drawing file when you say “open a file”.) This will only work if your computer is set up to open dwg files with autocad when you double click on a dwg file….

    Put this code on a form in the Excel macro. The form needs one button called ‘CommandButton1″…

    Private Sub CommandButton1_Click()
    Dim OleObj As Object
    StartDoc = ShellExecute(hwnd, “open”, “Drawing1.dwg”, “”, “C:”, SW_SHOWNORMAL)
    End Sub

    Create a Module and put this code in the general / declarations area…

    Declare Function ShellExecute Lib “shell32.dll” Alias _
    “ShellExecuteA” (ByVal hwnd As Long, ByVal lpOperation _
    As String, ByVal lpFile As String, ByVal lpParameters _
    As String, ByVal lpDirectory As String, ByVal nShowCmd _
    As Long) As Long

    Now, when you run the form and click the button, it will open a drawing on the root directory of your C: drive with the name Drawing1.dwg. You can have your program put any thing you want in place of this drawing name including the entire drive path and file name.

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

What is 11 + 15 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)