Revit 2025 and Visual Studio Code DocumentOpened event

Revit 2025 and Visual Studio Code DocumentOpened event

Eduardo.SaezDRTV4
Contributor Contributor
380 Views
4 Replies
Message 1 of 5

Revit 2025 and Visual Studio Code DocumentOpened event

Eduardo.SaezDRTV4
Contributor
Contributor

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;
			}
			
			}
		}
   }
}

0 Likes
381 Views
4 Replies
Replies (4)
Message 2 of 5

naveen.kumar.t
Autodesk Support
Autodesk Support

Hi @Eduardo.SaezDRTV4 ,

 

It looks like the error you're seeing is due to a return statement in a method that is declared to return void.

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");
            // No return is needed since the method is void
        }
    }
}

Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 3 of 5

jeremy_tammik
Alumni
Alumni

Welcome to C# and the Revit API.

     

Actually, the error message says exactly what the problem is. If I pare it down and reformulate it a bit, it says, "your method DocumentOpenedHandler is a void function, so the return keyword must not be followed by an object expression". In other words, you cannot write return Result.Succeeded;; you have to leave out the Result.Succeeded and write just return;

  

However, I would also suggest that if you are running into issues like this, it might make sense to play around a bit more with just pure C# in tutorials or online courses before starting to address things in the Revit API, where you will be confronted and confused by the combination of Revit API quirks and C# issues.

  

This forum is (mostly) dedicated to discussing the Revit API, and not C# beginner issues.

  

Good luck and have fun!

   

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 4 of 5

Eduardo.SaezDRTV4
Contributor
Contributor

Thank you both for your input it's really appreciated.
Jeremy, I fully intend on running through C# tutorials and or undertake a course to get the fundamentals down.
This query was for migration of an existing simple macro to C# as we're moving to Revit 2025

0 Likes
Message 5 of 5

Mohamed_Arshad
Advisor
Advisor

HI @Eduardo.SaezDRTV4 

 

Small suggestion from my side Before diving in the Revit API kindly refer the below starter kit link for the Revit API to understand the basics concepts of the Revit API.

 

https://thebuildingcoder.typepad.com/blog/2022/02/getting-started-once-again.html 

 

Hope this will Helps 🙂


Mohamed Arshad K
Software Developer (CAD & BIM)

0 Likes