C# Document-Level Macro Code Example

The code for successfully built document-level macros are stored in the RVT file after you save the RVT file. The project files are removed from the temporary location when you exit Revit.

In the IDE, use the following code for the method:

public
void MyFirstMacroDocCS()
{
    Autodesk.Revit.DB.XYZ origin = Document.Application.Create.NewXYZ(0.0, 0.0, 0.0); 
    string strText = "My First Macro, Doc level, C#!";
    double lineWidth = 4.0 / 12.0;

    ElementId defaultTextTypeId = Document.GetDefaultElementTypeId(ElementTypeGroup.TextNoteType);
    TextNoteOptions opts = new TextNoteOptions(defaultTextTypeId);

    using (Autodesk.Revit.DB.Transaction transaction = new Autodesk.Revit.DB.Transaction(Document, "NewTextNote"))
    {
        transaction.Start();
        Autodesk.Revit.DB.View View = Document.ActiveView;
        TextNote.Create(Document, View.Id, origin, lineWidth, strText, opts);
        transaction.Commit();
    }
} 

Tip: Be sure to build your project in the Revit macro IDE, before trying to run it from the Macro Manager.

For this example, when you build the project in the Revit macro IDE, also notice that you are building the DocCSharp project. Your document-level C# macro's code resides in ThisDocument.cs. You can use the IDE's Project Explorer to see its temporary location on disk.

The macro placed text box