Message 1 of 5
Revit 2025 and Visual Studio Code DocumentOpened event
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I'm very green with C# and am testing some code but am encountering an error when building the solution, I have a variant of this code running under Revit 2022.
The error I'm getting is
C:\ProgramData\Autodesk\Revit\Macros\2025\Revit\AppHookup\Project_FilePath\Source\Project_FilePath\
ThisApplication.cs(36,5): error CS0127: Since 'ThisApplication.DocumentOpenedHandler(object?, Docum
entOpenedEventArgs)' returns void, a return keyword must not be followed by an object expression [C
:\ProgramData\Autodesk\Revit\Macros\2025\Revit\AppHookup\Project_FilePath\Source\Project_FilePath\P
roject_FilePath.csproj]
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.UI.Selection;
namespace Project_FilePath
{
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
[Autodesk.Revit.DB.Macros.AddInId("2F4ED035-9D44-4043-B837-7844A9AA861E")]
public partial class ThisApplication
{
private void Module_Startup(object? sender, EventArgs e)
{
// Subscribe to the DocumentOpened event
Application.DocumentOpened += new EventHandler<DocumentOpenedEventArgs>(DocumentOpenedHandler);
}
private void Module_Shutdown(object? sender, EventArgs e)
{
// Unsubscribe to the DocumentOpened event
Application.DocumentOpened -= new EventHandler<DocumentOpenedEventArgs>(DocumentOpenedHandler);
}
public void DocumentOpenedHandler(object? sender, DocumentOpenedEventArgs e)
{
if (e.Status == RevitAPIEventStatus.Succeeded)
{
try{
// Display a message box when a document is opened
TaskDialog.Show("Test Macro","Revit File Opened");
}
catch (Exception) {
TaskDialog.Show("Error","There was a problem opening the document");
return Result.Succeeded;
}
}
}
}
}.