How to Write Your First Revit Macro in 7 Easy Steps

(This article was originally published on September 21, 2016. It was updated on May 24, 2018)

Macros are one of the easiest ways to Automate Revit. They let you get under the hood of your software and put it to work for you. Macros do not require any additional software other than Revit and are a great way for beginners to learn programming.

So what exactly is a macro? A macro is a user-created command that is coded using Revit’s API. Macros are run directly inside of Revit and are saved in the project file. Other applications, like MS Office, provide the ability to record macros directly from your actions on the screen. Unfortunately, Revit does not have this functionality. You must code your Revit macros directly.

Exclusive Bonus! Click here to download an enhanced PDF version of this tutorial along with a list of resources to help you write your own time-saving Revit macros (click to download).

Your First Revit Macro

Ready to write your first macro? As you’ll see, the process is very easy. Follow the steps below and you’ll be on your way to macro mastery.

1. Open the Revit Macro Manager

Create a new project file. Click the Manage ribbon then click the Macro Manager icon. This will open the Macro Manager dialog.

Macros can reside in a project file or within the Revit application. Macros saved in the project file can be used by any user who opens that file. Macros saved in the application are saved to the user’s Revit configuration. These macros can be used on any model file but only by the user who created the macro.

2. Create a New Module

Macros are organized in modules. When creating a macro in a new project file, you must first create a module. A module is simply a collection of macros. A single project file can contain several modules with each module having its own macros. Module names cannot contain spaces or special characters. To create a module, click the “Project 1” tab then click the Module button in the “Create” section. In the “Create a New Module” dialog box, title your module “MyFirstModule. You can write macros in C#, VB.Net, Python or Ruby. For this exercise, choose C# as the module’s language. Click OK to create the module. Once Revit has created the module, SharpDevelop will launch. SharpDevelop is an open-source development environment that is built into Revit for programming macros.

3. Create a New Macro

Now that you have a module, you can create a macro inside the module. Click the Macro button in the “Create” section of the Macro Manager dialog. In the “Create a New Macro” dialog, title your first macro “MyFirstMacro” and set the language to VB.NET. Click OK to create the macro.

4. Write the Macro

Switch over to SharpDevelop. You’ll see the standard C# code that is automatically generated when you create a new module. Toward the bottom you’ll see the starting code for “MyFirstMacro”. Your first macro is simply going to popup a message box in Revit. It only takes one line of code. After the “public void MyFirstMacro()”, type the following between the brackets:

5. Build the Macro

Once you’ve typed the code, you’re ready to compile or “build” the macro. All macros must be built before Revit can run them. In the SharpDevelop menu bar, select “Build” then “Build Solution”. SharpDevelop will compile your VB.NET code into the .Net intermediate code. Any errors or warning will show up in the Errors and Warning window located at the bottom of the SharpDevelop interface. If you have an error, double-check your code. The code window will list errors by line number so they are easy to pinpoint.

6. Run the Macro

If your macro compiled correctly, go back to Revit and open the Macro Manager dialog (Manage > Macro Manager). You should see “MyFirstMacro” in the list below “MyFirstModule”. Select “MyFirstMacro” from the list then click the Run button. This will execute your macro. You should see the following on your screen: You did it! You wrote your first Revit macro.

7. Make Some Changes

To take this further, you can modify the code to report back something more useful. Change your code to the following:

The “this.Application.ActiveUIDocument” object represents the current model file. The “Document” object contains data pertaining to the current file itself. To see the active view in the current project file, change “Document.PathName” to “ActiveView.Name”.

Your Next Revit Macro

Our first macro was useful for illustrating the process for creating a macro but let’s take what we just learned and put it to use on a macro that is more useful. The following code deletes unused views in the current model file. If a view is not on a sheet, it is deleted. Note this macro does not work with dependent views. To test out your new macro coding skills, create a new macro and type the following code:

After typing the code, go to Build > Build Solution to compile it. You may get an error or two, that’s normal. Use the steps outlined in the troubleshooting section above and try to resolve the errors. Check for typos in your code – that’s what usually gets me! Once the code builds successfully, switch over to Revit and run the macro.

Next Steps

Congratulations! You’re on your way to Revit macro mastery! While this tutorial is very basic, it illustrates the principles of writing Revit macros. For a next step, I recommend downloading and installing the Revit Software Development Kit or SDK. The SDK contains help files and sample code that will assist you as you learn to program macros. The Revit SDK be installed from the main page of the Revit installer or it can be downloaded from the Autodesk Developer Network website. The SDK will install on your hard drive and create a bunch of subfolders and files. Take some time to review the files. The macro samples are particularly useful as you get started creating your own macros.

Conclusion

Learning to write macros and automate Revit will drastically improve your efficiency. A well-written macro can do more in five minutes than a regular user can accomplish in one hour. Learning to program takes time and patience. Start small and work systematically. You’ll be on your way to macro mastery in no time!

Exclusive Bonus! Click here to download an enhanced detailed PDF version of this tutorial along with a list of resources to help you write your own time-saving Revit macros (click to download).

Liked this article?

Comments

Hi, I’m working for architecture company in Korea. I’m learning C# language everyday step by step. Like you mentioned in your article, language study needs a lot of patience. But, I could see the value when I read your article. Before studying C#, the code part makes me dizzy, but now I can read the code! Only reading is possible, later I will be able to write a lot of useful macros. Because I don’t give up. That was very useful article. Thank you for your posting.

Thank you for all you share. I have an interesting question. I have looked a little into your Revit Macro training.
I am trying to learn the API. What I need is to develop a macro to changes walls which have been model starting at level 1 and going to level 10 for example, I want to change the wall to set at the next level, and then copy the same to the next levels.

Can your training give me the ability to do this? Hope to hear from you soon.

Rafael,
We don’t cover this specific example in the training but we go over everything you’d need to know in order to do this. You want to take walls that span multiple levels and instead create individual walls that go from one level to the next for the span of the original walls. Am I understanding that correctly?
Michael