0

I will like to create visual studio macros in c#. What am I doing now is to create the macros in a class library then execute them from vba. the problem is that I cannot debug the macros because I cannot step into the dll from vba.

Anyways I will like to create the macro in a console application for debuging purpuses. Once I know it works I will just place it on the dll.

Here is my code so far:

class Program
{        
    static void Main( string[] args )
    {            
        EnvDTE80.DTE2 MyDte;
        MyDte = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject( "VisualStudio.DTE.10.0" );
        Console.WriteLine( "The Edition is " + MyDte.Edition );

        Console.ReadLine( );

        Console.Write( MyDte.ActiveDocument.Selection.Text );


        Console.Read( );                                                                           
    }

}

note I added the references that visual studio macros uses:

enter image description here


Edit

Maybe I get an exception because I am trying to modify the document that I am currently debugging. If I could manage to get the dte of a second instance of visual studio document ( document that is not being debuged ) I might be able to solve this problem.

Tono Nam
  • 34,064
  • 78
  • 298
  • 470

2 Answers2

0

Have you seen this SO article: A Simple C# DLL - how do I call it from Excel, Access, VBA, VB6?

The two answers, especially the second, appear to largely address the concerns of calling/test vba script in C#. By extension, you may be able to reference the dll and use a messagebox or similar means in a console application to print error messages/results and the like.

~JOL

Community
  • 1
  • 1
Scott Conover
  • 1,421
  • 1
  • 14
  • 27
0

This answer should enable you to create a visual studio macro in any programing language in any type of application. I am creating one in a wpf application using the ide class located on the link:

https://stackoverflow.com/a/11713078/637142

Community
  • 1
  • 1
Tono Nam
  • 34,064
  • 78
  • 298
  • 470