Click here to Skip to main content
15,891,204 members
Articles / Operating Systems / Windows

Refreshing project references with a Macro

Rate me:
Please Sign up or sign in to vote.
4.45/5 (13 votes)
24 Feb 2004CPOL1 min read 90.9K   13   21
How to write a macro in order to refresh all the references of all the project of the solution

Introduction

This article show you how to write a macro that refreshes all the references of a solution. This is useful when you have a lot of projects with interrelated DLL references between them. So when you have DLL versions problems, you refresh all the references of the solution an solve it.

Using the code

Take the code showed below and paste it in the Macro IDE (Alt + F11) to create a macro.

RefreshProjectReferences Sub

This function runs the macro code. Obtains the solution projects, display a processing message and iterates between projects refreshing the references. Uses the DTE Status Bar to show a progress bar and a progress message.

VB
'Refresh the references of all projects in the solution
Sub RefreshProjectReferences()
    ' Retrieve the VSProject object.
    Dim oVSProject As VSProject

    'Create an popup message window
    Dim frmMessage As PopupMessage
    Try
        frmMessage = New PopupMessage("Refreshing references")
        frmMessage.Show()

    Catch ex As Exception
        'Handle exceptions here
    End Try

    'Iterate solution's projects
    For i = 1 To DTE.Solution.Projects.Count
        'Update progress bar
        DTE.StatusBar.Progress(True, "Refreshing projects references", _
          i, DTE.Solution.Projects.Count)
        'Obtain the project object
        oVSProject = CType(DTE.Solution.Projects.Item(i).Object, VSProject)
        If Not oVSProject Is Nothing Then
            'Refresh references
            oVSProject.Refresh()
        End If
    Next
    'Refreshing finished
    DTE.StatusBar.Progress(False)
    DTE.StatusBar.Text = "Refreshing succeed"
    DTE.StatusBar.Highlight(True)

    Try
        'Destroy objects
        frmMessage.Close()
        frmMessage.Dispose()
        frmMessage = Nothing
    Catch ex As Exception
        'Handle exceptions here
    End Try
End Sub

PopupMessage Class

This class defines a Popup Window that show a message during refresh execution.

Note: The look & feel of this window is very simple, you can work on it for a best view.

VB
'Popup Window with a Waiting Message
Public Class PopupMessage
    Inherits System.Windows.Forms.Form

    Private txtMessage As New System.Windows.Forms.TextBox

    Public Sub New(ByVal pMessage As String)
        Try
            'Form format
            Me.TopMost = True
            Me.ControlBox = False
            Me.FormBorderStyle = _
                   System.Windows.Forms.FormBorderStyle.Fixed3D
            Me.Text = ""
            Me.ShowInTaskbar = False

            'Textbox format
            txtMessage.Top = 20
            txtMessage.Left = 40
            txtMessage.Width = 160
            txtMessage.Font = New System.Drawing.Font("Arial", 10, _
               System.Drawing.FontStyle.Bold)
            txtMessage.BackColor = System.Drawing.SystemColors.Control
            txtMessage.ForeColor = System.Drawing.Color.Black
            txtMessage.BorderStyle = _
                System.Windows.Forms.BorderStyle.None
            txtMessage.Text = pMessage
            txtMessage.AutoSize = True
            txtMessage.SelectionLength = 0
            Me.Controls.Add(txtMessage)

            Me.Height = txtMessage.Height + 40
            Me.Width = txtMessage.Width + 80
            Me.StartPosition = _
               System.Windows.Forms.FormStartPosition.CenterScreen
            txtMessage.Refresh()
            Me.Refresh()

        Catch ex As System.Exception
            'Handle exceptions here
        End Try

    End Sub

End Class

Tip

After you paste the code in the Macro Editor is useful assign a key combination to the macro (if you plan to use very much times the macro).

To do this:

  1. Open the menu Tools/Options
  2. Go to the folder Enviroment and select the Keyboard item
  3. Then select the macro name (e.g. Macros.Samples.VSEditor.RefreshProjectReferences)
  4. Select the shortcut key combination and assign it

When you select the shortcut key combination, VS will show you if it's already used and in the "Use new shortcut in" you can select the valid shortcut context.

Points of Interest

The only thing I want to remark is that macros makes extensible the Visual Studio environment and if you take a minutes to write a little code you can automate several repetitive actions.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Argentina Argentina
Gustavo is Information Systems Enginner and
Information Systems Analyst. He born in Colonia Caroya, Cordoba, Argentine.
He worked in the Universidad Tecnologica Nacional in Cordoba, Argentine like Research Group Coordinator in the Business Intelligence area.

After that worked like a professor of Visual Basic, SQL Server 2000, .NET Plataform, Visual Basic.NET and C#

His experience in programming includes: QuickBasic, C, C++, FoxPro, Visual Basic, VB.NET, C# and ASP.NET. He has working with XML, XSD, XSLT and other related technologies.

He is now working in Pectra Technology, in the development and the software engineering area in a product named Pectra BPM Studio Framework (Business Process Management Solution) using Microsoft.NET and XML technologies.

His hobbies are play the piano and reading. He is a science lover

Comments and Discussions

 
QuestionError when running in VS 2008 Pin
D P Pham6-Jul-09 13:29
D P Pham6-Jul-09 13:29 
Hi Gustavo,

Thank you for the codes. I am running into a little problem and hoping you could help me. I really need to refresh the references for 74 projects in my solution. I am running Visual Studio 2008, however. When I run the macro, I got this error: "Unable to cast COM object of type 'System._ComObject' to interface type 'VSLangProj.VSProject'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{A SERIES OF NUMBER}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE))."

After getting this error, my solution was frozen. I had to end the process to get back in. In addition, I can't find System._ComObject any where else. Could you help please? Thank you very much in advance.
AnswerRe: Error when running in VS 2008 Pin
sunilshinde15-May-13 0:18
sunilshinde15-May-13 0:18 
GeneralSmall Glitch Pin
Michael Lang26-Apr-04 21:35
Michael Lang26-Apr-04 21:35 
GeneralRe: Small Glitch Pin
Gustavo Bonansea27-Apr-04 11:05
Gustavo Bonansea27-Apr-04 11:05 
GeneralRe: Small Glitch Pin
Michael Lang28-Apr-04 16:22
Michael Lang28-Apr-04 16:22 
Generalslight compiling problem Pin
eltwo19-Mar-04 2:00
eltwo19-Mar-04 2:00 
GeneralRe: slight compiling problem Pin
Gustavo Bonansea22-Mar-04 1:19
Gustavo Bonansea22-Mar-04 1:19 
GeneralRe: slight compiling problem Pin
eltwo22-Mar-04 2:42
eltwo22-Mar-04 2:42 
GeneralRe: slight compiling problem Pin
Gustavo Bonansea22-Mar-04 3:59
Gustavo Bonansea22-Mar-04 3:59 
QuestionNewbie: how to use ?? Pin
D.T3-Mar-04 16:41
D.T3-Mar-04 16:41 
AnswerRe: Newbie: how to use ?? Pin
Gustavo Bonansea4-Mar-04 5:15
Gustavo Bonansea4-Mar-04 5:15 
GeneralRe: Newbie: how to use ?? Pin
D.T10-Mar-04 16:02
D.T10-Mar-04 16:02 
GeneralRe: Newbie: how to use ?? Pin
Gustavo Bonansea11-Mar-04 1:36
Gustavo Bonansea11-Mar-04 1:36 
GeneralRe: Newbie: how to use ?? Pin
D.T22-Mar-04 21:21
D.T22-Mar-04 21:21 
GeneralRe: Newbie: how to use ?? Pin
Gustavo Bonansea22-Mar-04 23:52
Gustavo Bonansea22-Mar-04 23:52 
Generalphoto Pin
na_isabel26-Feb-04 0:59
na_isabel26-Feb-04 0:59 
GeneralRe: photo Pin
Gustavo Bonansea26-Feb-04 1:26
Gustavo Bonansea26-Feb-04 1:26 
GeneralRe: photo Pin
na_isabel26-Feb-04 7:29
na_isabel26-Feb-04 7:29 
GeneralRe: photo Pin
Gustavo Bonansea26-Feb-04 8:25
Gustavo Bonansea26-Feb-04 8:25 
Generalquestion Pin
Bruno Capuano25-Feb-04 3:04
Bruno Capuano25-Feb-04 3:04 
GeneralRe: question Pin
Gustavo Bonansea25-Feb-04 3:24
Gustavo Bonansea25-Feb-04 3:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.