Category Archives: VBA

Set multiple startup projects in Visual Studio 2010 via macro

——RANT MODE: ON—–

In one C++ project I am currently working on (yes, C++ is not dead 😉 , I frequently have to switch between different startup projects in Visual Studio. Also, I sometimes want to start several executables at once. For this, Visual Studio supports setting of multiple startup projects.

However, settings those by hand can be a bit tedious, so I tried to make a visual basic macro for it. Ok, to be honest I did not try to make one – I tried to steal one from the internet.

A bit of googling revealed the following post which gives an approach for a single startup project and a “theoretical” solution for multiple startup projects:
http://matthiaskraaz.blogspot.com/2011/08/visual-studio-setting-multiple-startup.html

Turns out, setting multiple startup projects programmatically is not possible in Visual Studio 2005.

With Visual Studio 2010 however, it is possible. Here is how it works:

——RANT MODE: OFF—–

Public Sub SetMyStartupProjects()  

  'Create an array and fill it with the startup projects you would like to set
  Dim StartupProjectsArray As Object = System.Array.CreateInstance(GetType(Object), 3)
  StartupProjectsArray(0) = "MyProj1.vcxproj"
  StartupProjectsArray(1) = "MyProj2.vcxproj"
  StartupProjectsArray(2) = "MyProj3.vcxproj"

  'Set the startup projects of the current solution
  Dim CurrentSolutionBuild As SolutionBuild = DTE.Solution.SolutionBuild
  CurrentSolutionBuild.StartupProjects = StartupProjectsArray

End sub

The code for creating the array might look a bit clunky, but a collection or any array defined via literals doesn’t work.
The type you use on the right hand side of CurrentSolutionBuild.StartupProjects = really has to be of the type System.Array and the things inside the array really need to have the type Object. The rest is straightforward.

As a bonus, here is a little helper method that takes a usual VB collection:

Private Function SetStartupProjects(ByVal StartupProjects As Collection)
  Dim StartupProjectsArray As Object = System.Array.CreateInstance(GetType(Object), StartupProjects.Count)
  Dim i As Integer
  For i = 0 To StartupProjects.Count - 1
    StartupProjectsArray(i) = StartupProjects(i + 1)
  Next

  Dim CurrentSolutionBuild As SolutionBuild = DTE.Solution.SolutionBuild
  CurrentSolutionBuild.StartupProjects = StartupProjectsArray
End Function

You can call it like this:

Public Sub SetMyStartupProjects()
  'Create a collection that you would like to set
  Dim StartupProjects As New Collection
  StartupProjects.Add("MyProj1.vcxproj")
  StartupProjects.Add("MyProj2.vcxproj")
  StartupProjects.Add("MyProj3.vcxproj")

  'Set the startup projects of the current solution
  SetStartupProjects(StartupProjects)
End sub

I wonder why Microsoft doesn’t allow collections or straightforward String-arrays in the first place. Well, whatever, if you can encapsulate the ugly stuff, it is not ugly anymore.

Happy coding!


Excel VBA Code Updater

One problem when developing VBA applications is that bringing bugfixes and improvements to your users can be difficult since an Excel Workbook contains both, code and data. Certainly you cannot expect the typical MS Excel user to import .bas files or things like that.

To solve this problem, I wrote a small Excel tool called ‘VBA Updater’. It updates the VBA code of an Excel Workbook from another Workbook without touching the data in the Worksheets.

You can find a description and the tool at: VBA Updater

The VBA Updater Tool

Dynamic Dialogs in Excel VBA

The problem:
You want to have a User Form (a VBA-Dialog) which

  • controls (buttons, text fields etc.) are created at runtime based on information that is read from e.g. a table
  • event handling of those controls is created at runtime.
  • For a solution of these problems check the following file and/or read on.
    Example Code for Dynamic Dialogs in Excel VBA

    Consider the following example: There is a table which contains products of some type. Each product has some properties like description, prize etc. This table is created by some database queries. Hence, you do not know how much entries the table will have.
    You want to provide the user of your Excel application with an easy way to select those products and insert them in another table (e.g. an invoice). If a user wants to insert a product into the invoice, he clicks a button and you would like to present him a User Form that has one button for each product. If he clicks on one button, the concerning product is inserted into the invoice table with its description, price etc.

    Dynamic Dialogs in Excel at Work

    Continue reading