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:
I placed
myClassLibrary.pdb
next tomyClassLibrary.dll
hoping I where going to be able to debug the methods in the dll by steping in to them.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:
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:
(Note I am debuging a macro on c# on visual studio!!!)
Create a new Solution in visual studio
Now add a new class library Project to that solution. (This is the class that will execute the macros)
Add the references EnvDTE, EbvDTE100, EnvDTE80, EnvDTE90, EnvDTE90a. Basically the same references that visual studio macros have:
Create a method that will execute the macro you plan to use on the class library.
Add another project (Visual Studio Add-in)
Follow the wizzard leave the defaults except on page 4 select:
Continue selecting the default options on the wizard until the project is created:
Set that project as the startup project so that when we press f5 the addin runs.
Add a reference from MyAddin1 to the class library
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 methodExec
addClassLibrary1.Class1.Macro1(_applicationObject);
so it looks like:Add a break point at the start of the Exec method so we can debug it.
Execute MyAddin1 by pressing
F5
. A new instance of visual studio should open.On the new instance of visual studio open any solution. In this case I am opening the same solution again>
Got to tools then click on MyAddin1 but make sure a document is open:
Once you click on my addin you should hit the breakpoint!
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:
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.
The way to debug visual studio add-ins is to open another visual studio instance and attach to the one that will activate your add-in. It is described here. Also make sure that the add-in is built on the local machine and pdbs are available otherwise it will not hit your breakpoints.
Using DLL's from other publishers is easy. But it's important to understand the code. I use IlSpy. Its free and to use. It decompiles the dll to view all methodes, classes and namespaces.
I have yet another answer which is even better!
The only reason why I created the addin is because I needed a reference of the DTE. Why not reference the dte that I need.
The the algorithm is as follow:
Use class
Ide
to get the DTE of whatever instance of visual studio.Once you have that dte create the macro.
Here is the Ide class:
then if I have an instance of visual studio runing that contains a solution with the name
ConsoleApp1
then I will be able to do:and the text
My macro is working!
will be inserted in the active document. make sure there is an active document thoughTono,
Have you referenced your dll for the wcf application and made it a debug version?
From: http://msdn.microsoft.com/en-us/library/ms164704.aspx:
If you are attempting to debug a referenced dll, this SO article might be of assistance here: How to debug a referenced dll (having pdb)
~JOL