Search code examples
visual-studiovisual-studio-macros

How can I run old VB Macros in recent Visual Studio?


I need to run an old visual studio macro which is in VB. But I've found out macros are no longer supported natively in Visual Studio, but there is an extension for Macros here: https://marketplace.visualstudio.com/items?itemName=VisualStudioPlatformTeam.MacrosforVisualStudio

Any Ideas how can I run the following in Visual Studio 2017 community edition, or converting it to a javascript that the new extension supports it?

Sub TemporaryMacro()
DTE.ActiveDocument.Selection.StartOfDocument()
Dim returnValue As vsIncrementalSearchResult
While True
    DTE.ActiveDocument.ActiveWindow.Object.ActivePane.IncrementalSearch.StartForward()
    returnValue = DTE.ActiveDocument.ActiveWindow.Object.ActivePane.IncrementalSearch.AppendCharAndSearch(AscW("{"))
    DTE.ActiveDocument.ActiveWindow.Object.ActivePane.IncrementalSearch.Exit()
    If Not (returnValue = vsIncrementalSearchResult.vsIncrementalSearchResultFound) Then
        Return
    End If
    DTE.ExecuteCommand("Debug.ToggleBreakpoint")
    DTE.ExecuteCommand("Edit.GotoBrace")
    DTE.ActiveDocument.Selection.CharRight()
End While
End Sub

Solution

  • I didn't try the Macros extension myself, but transforming your old VB macro to JS should be straightforward. The global field DTE from VB macro is now called dte. Not tested, but the JS macro could be:

    function TemporaryMacro() {
        dte.ActiveDocument.Selection.StartOfDocument();
        var returnValue;
        while (true) {
            dte.ActiveDocument.ActiveWindow.Object.ActivePane.IncrementalSearch.StartForward();
            returnValue = dte.ActiveDocument.ActiveWindow.Object.ActivePane.IncrementalSearch.AppendCharAndSearch("{".charCodeAt(0));
            dte.ActiveDocument.ActiveWindow.Object.ActivePane.IncrementalSearch.Exit();
            if(returnValue != vsIncrementalSearchResult.vsIncrementalSearchResultFound) {
                return;
            }
            dte.ExecuteCommand("Debug.ToggleBreakpoint");
            dte.ExecuteCommand("Edit.GotoBrace");
            dte.ActiveDocument.Selection.CharRight();
        }
    }
    

    And you probably need to call the function, so place this line before the above code:

    TemporaryMacro();
    

    Or if you don't want to convert the code, you can use directly VB .NET or C# with Visual Commander extension.