Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Macro: How to perform "File -> Save All" programatically

I am looking for the equivalent of running "File -> Save All" before certain Rake macros.

What I have so far is:

Private Sub Pre_Rake()
        Dim i As Integer

        DTE.Documents.SaveAll()

        For i = 1 To DTE.Solution.Projects.Count
            If Not DTE.Solution.Projects.Item(i).Saved Then
                DTE.Solution.Projects.Item(i).Save()
            End If
        Next
End Sub

DTE.Documents.SaveAll works fine, but the for loop does not save the project files as I would expect.

If I make a copy of a file in the solution explorer, that file is not included in the project file (.CSPROJ) after Pre_Rake() runs. I would still have to press CTRL-SHIFT-S or File -> Save All.

So, how to Save All with a Visual Studio Macro?

like image 625
Sean B Avatar asked May 28 '10 20:05

Sean B


2 Answers

Apparently DTE.Documents.SaveAll doesn't save all open documents (maybe it saves only ones belonging to open projects). Try using

DTE.ExecuteCommand("File.SaveAll")

which is exactly like doing File -> Save All.

like image 83
Amnon Avatar answered Sep 28 '22 01:09

Amnon


If you're interested in understanding why the For loop didn't work, it is because we also have to loop through the project items:

Sub SaveAllFiles()
    For i = 1 To DTE.Solution.Projects.Count
        If Not DTE.Solution.Projects.Item(i).Saved Then
            DTE.Solution.Projects.Item(i).Save()
        End If
        For j = 1 To DTE.Solution.Projects.Item(i).ProjectItems.Count
            If Not DTE.Solution.Projects.Item(i).ProjectItems.Item(j).Saved Then
                DTE.Solution.Projects.Item(i).ProjectItems.Item(j).Save()
            End If
        Next
    Next
End Sub
like image 37
jonathan.s Avatar answered Sep 28 '22 01:09

jonathan.s