Search code examples
c#visual-studio-2017visual-studio-sdk

How to resurface (activate) a specific document in VS?


I'm writing a small Visual Studio extension for VS2017 in C# and I'm trying something rather simple: If I press a button I want to resurface (make active) a specific document.

I have the RunningDocumentInfo from the RunningDocumentTable for this document, so I have its moniker and hierarchy and all that stuff. In the SDK docs I only found that the resurfacing can be done with IVsUIShellOpenDocument.IsDocumentOpen and the IDO_ActivateIfOpen flag. This sounds a bit inappropriate since I already know that the document is open for sure, but I'd go with it if it works. But how do I get a fitting instance that implements IVsUIShellOpenDocument? Or is there, by any chance, a simpler way that I just didn't find?


Solution

  • I found a way I now accept as the solution:

    foreach (Document doc in dte2.Documents)
    {
        if (doc.FullName == Moniker)
        {
            doc.Activate();
            return;
        }
    }
    

    This traverses the currently opened documents until it finds the one I want to activate and activates it, just like I wanted :)