So you wrote a super useful Revit macro that automates some really tedious tasks in Revit. You’ve saved yourself at least twenty hours on your last project. Now you want to use that same macro on your current project. How can you copy the macro code over to another RVT file?
Unfortunately there’s no magic “copy macro” button (at least not yet). Because the macro is embedded in the Revit project file, it is not obvious how to copy that macro to another file. Here are three methods I use to copy my Revit macros to other project files:
1. Cut and Paste the Macro Code
Open the Revit project file containing the macro you want to copy as well as the file you want to copy the macro into. Go to the Macro Manager (Manage > Macro Manager) and edit the existing macro to open SharpDevelop.
In the existing macro, select the code you want to copy then go to Edit > Copy or press Control + C to copy the code to the clipboard.
In the file you want to copy the macro into, create a new module and macro of the same name. Make sure you select the same programming language as the existing macro.
In the new SharpDevelop window, paste the code from the existing macro into the new macro. Build your solution to ensure it works properly then save the new macro file.
This method works great for simple macros that do not use forms or multiple class files.
2. Export the Macro Code from SharpDevelop
If your macro uses forms or has multiple class or module files, you can use this method to export all the associated files.
Open the Revit project file containing the macro you want to copy. Go to Manage > Macro Manager and edit the existing macro to open SharpDevelop.
In the “Projects” window of SharpDevelop, select the file that contains the code you want to copy.
Go to File > Save As to export the selected code file.
Create a new module in the file you want to copy the macro into. Make sure you set the language to the same as the macro you want to copy. This will open a new session of SharpDevelop.
In the new SharpDevelop window, select the module name in the “Projects” window, right-click and select Add > Existing.
Browse to the location where you save the code file and import it.Build the code to ensure it’s working correctly.
If the macro you’re copying has multiple classes or forms, you’ll need to export and import each file individually.
3. Import the Source Code from Temp Folder
Don’t feel like importing and exporting your macro’s various files? Use this method to import a whole folder of code files.
Whenever you open a macro in SharpDevelop, the source code is saved to a temporary folder on the local computer. You can access this folder by selecting the macro in the SharpDevelop “Projects” window, right-clicking and choosing “Open Folder in Explorer”.
Because it is a temporary folder, the folder name is going to be long and (really) obtuse.
Once you know the folder path of the existing macro, create a new module in the file where you want to copy the macro. Again, make sure you set the language to the same as the macro you want to copy.
In the new SharpDevelop window, select the module name in the “Projects” window, right-click and select Add > Existing Folder. Browse to the existing macro’s folder. Unfortunately you can’t just cut and paste the folder location, you need to browse to it directly.
Once you’ve found the folder, click “OK” to import it into your new macro. Choose “Copy” to copy the folder to the target macro location.
This will likely create multiple instances of some objects in the new macro. Delete the existing version of “ThisDocument” as well as the “Properties” folder. Build the macro to ensure it compiles correctly.
What About Application Macros?
The three methods outlined above work great for document macros that are saved in RVT files but what about application macros? Can you copy a document macro as an application macro?
An application macro operates at the application level. When you create an application macro, you’ll see that the macro code creates a “ThisApplication” class rather than “ThisDocument”. You can use any of the methods above to copy a document macro to your application macros, however, you will need to make some modifications to your code.
Check out the video below for a step-by-step guide to copying a project macro to your application macros.
Copying a macro to a new RVT file or application macro is not an automatic process but it can be accomplished using the steps outlined above. As long as you are systematic, you should not have too much difficulty transferring your macros.
If you know of another method to copy macros, please let me know in the comments below! I’m always looking for an easier method.