Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug custom dll that is being referenced in visual studio macro

I previously asked: Add dll reference to visual studio macros

the idea of creating the macros in my language (C#) makes it easier to create the macros. The problem is that I cannot debug the dll

To solve the problem I have tried:

  1. I placed myClassLibrary.pdb next to myClassLibrary.dll hoping I where going to be able to debug the methods in the dll by steping in to them.

  2. Created a WCF service. Because I did not knew how to reference the service from vba I reference it from the class library. The problem is that I need to use variables such as DTE.ActiveDocument and those variables are not serializable meaning I could not pass them to the wcf service.

the idea of working in C# is very nice but not being able to debug and see what is going on makes it somewhat difficult. I might have to go to my older option where I created my code on C# compiled then decompiled into vba with reflector.


Edit

I think I am close on getting a solution. I thought why not create the macro in a console application? I am able to get the active document text but not able to change it.

        EnvDTE80.DTE2 MyDte;
        MyDte = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject( "VisualStudio.DTE.10.0" );
        Console.WriteLine( "The Edition is " + MyDte.Edition );

        Console.ReadLine( );

        // write to the console the text that is selected. "sometimes it does not work don't know why"
        Console.WriteLine(
            MyDte.ActiveDocument.Selection.Text
        );

note I added the following references plus the onces that vba macros have:

enter image description here

like image 790
Tono Nam Avatar asked Jul 25 '12 15:07

Tono Nam


People also ask

How do I debug a macro in Visual Studio?

Open up a source file from your manage project in Visual Studio and set a breakpoint on a line. Start debugging in Visual Studio by pressing F5. In Excel, open up your worksheet and start debugging your VBA code using Excel's debugger.


1 Answers

Finally here is the solution:

On the following steps I will describe how it will be possible to debug the dll that will be executed by a macro.

If you want to be able to do something like:

enter image description here(Note I am debuging a macro on c# on visual studio!!!)

  1. Create a new Solution in visual studio enter image description here

  2. Now add a new class library Project to that solution. (This is the class that will execute the macros) enter image description here

  3. Add the references EnvDTE, EbvDTE100, EnvDTE80, EnvDTE90, EnvDTE90a. Basically the same references that visual studio macros have: enter image description here

  4. Create a method that will execute the macro you plan to use on the class library.

       namespace ClassLibrary1
       {
           public static class Class1
           {
               public static void Macro1(EnvDTE80.DTE2 DTE)
               {
                   // make sure an active text document is open before calling this method
                   DTE.ActiveDocument.Selection.Insert("Hello World!!!");
               }
           }
       }
    
  5. Add another project (Visual Studio Add-in)enter image description here

  6. Follow the wizzard leave the defaults except on page 4 select: enter image description here

  7. Continue selecting the default options on the wizard until the project is created: enter image description here

  8. Set that project as the startup project so that when we press f5 the addin runs.

  9. Add a reference from MyAddin1 to the class library

  10. Once we have that reference we should be able to execute the macro from the addin. In order to do so open Connect.cs and navigate to the method Exec add ClassLibrary1.Class1.Macro1(_applicationObject); so it looks like: enter image description here

  11. Add a break point at the start of the Exec method so we can debug it.

  12. Execute MyAddin1 by pressing F5. A new instance of visual studio should open.

  13. On the new instance of visual studio open any solution. In this case I am opening the same solution again>

  14. Got to tools then click on MyAddin1 but make sure a document is open: enter image description here

  15. Once you click on my addin you should hit the breakpoint! enter image description here

15. NOTE! For some reason I had to comment the line ClassLibrary1.Class1.Macro1(_applicationObject);

So I comment out that line and on that line I placed:

 var textDoc = (TextDocument)(_applicationObject.ActiveDocument.Object(string.Empty));
 textDoc.Selection.Insert("Hello world");

finally when I click on MyAddin1 located on tools Hello world will be inserted!


Once I know the macro is running fine I could export the class to a class library and have the macro call the method on the dll instead of the plug in.

like image 124
Tono Nam Avatar answered Oct 16 '22 12:10

Tono Nam