How to Write Your First Revit Macro in 7 Easy Steps

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.

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.

Courtesy of ARCHSMARTER

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.

Courtesy of ARCHSMARTER

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 VB.Net as the module’s language. Click OK to create the module.

Courtesy of ARCHSMARTER

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.

Courtesy of ARCHSMARTER

4. Write the Macro

Switch over to SharpDevelop. You’ll see the standard VB.NET 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:

Courtesy of ARCHSMARTER

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”.

Courtesy of ARCHSMARTER

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.

Courtesy of ARCHSMARTER

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”.

Courtesy of ARCHSMARTER

Select “MyFirstMacro” from the list then click the Run button. This will execute your macro. You should see the following on your screen:

Courtesy of ARCHSMARTER

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:

Courtesy of ARCHSMARTER

The “Me.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”. Note the underscore (“_”) character in the code above. This character represents a line continuation symbol. This tells SharpDevelop that the code continues in the line below. Line continuation symbols are used when printing long lines of code to a page. When you are typing the code, you can omit the “_” and type the code on a single line.

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:

Courtesy of ARCHSMARTER

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 2017 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 2017 SDK be installed from the main page of the Revit installer or it can bedownloaded 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

About this author
Cite: Michael Kilkelly. "How to Write Your First Revit Macro in 7 Easy Steps" 19 Oct 2016. ArchDaily. Accessed . <https://www.archdaily.com/797619/how-to-write-your-first-revit-macro-in-7-easy-steps> ISSN 0719-8884

You've started following your first account!

Did you know?

You'll now receive updates based on what you follow! Personalize your stream and start following your favorite authors, offices and users.