Ahmed Salijee

Macros are nice!

This should have been an old post. During TechED last year I had to do a keynote demo. In fact during TechED time there is stacks of little stuff I learn ? just too little time to blog. The thing is they wanted to limit the amount of time for my VSTO demo ? how dare they J.  BTW VSTO totally rocks!!

I started to look at ways of speeding up my demo (yeah a nice 64 bit machine with 4GB RAM ? I wish). I started to investigate the macro functionality. I always knew macros were there. I just never used them.

In this case as part of my demo I had to add a couple of projects and then add references to these projects as well. This is a few mouse clicks and I reckoned this was the best thing to automate. My best experience with macros was with Excel VBA. The macro recorder in Excel is nice and I was expecting the same thing. Unfortunately the recorder in this case did not even come close and I was a bit disappointed. None the less it did provide some useful hints. I started to investigate. I ultimately ended up with the code below. Yes the paths are hardcoded ? the app did not need to go into production J. I did have one additional line of code (not included here) to automatically turn on XML comments for the vbproj control (turns out by default that XML comments for VB based controls are off).

The code assumes you have 1 project already created hence the reference to DTE.Solutions.Projects.Item(1). This will add the 2 projects to the solution and add references to them as well (a project to project reference)

Option Strict Off

Option Explicit Off

Imports System

Imports EnvDTE

Imports EnvDTE80

Imports System.Diagnostics

Public Module AddRef


    Sub TemporaryMacro()

        Dim proj As EnvDTE.Project

        Dim vsproject As VSLangProj.VSProject


        proj = DTE.Solution.Projects.Item(1)

        vsproject = CType(proj.Object, VSLangProj.VSProject)

        Dim t As Project = DTE.Solution.AddFromFile("C:\Presentations\Keynote\EstateApp\EstateApp.csproj")

        Dim t1 As Project = DTE.Solution.AddFromFile("C:\Presentations\Keynote\WoodgroveBankControl\WoodgroveBankControlLibrary.vbproj")

        vsproject.References.AddProject(t)

        vsproject.References.AddProject(t1)

    End Sub

End Module