locked
How do you close an open document programmatically? RRS feed

  • Question

  • Hello everybody,

    I'm finishing a development in Visual Studio isolated shell and I have came across an unexpected problem. With VsShellUtilities I can open documents (OpenDocument method) and check if they are open (IsDocumentOpen method), but surprisingly there is no method for closing a document from code.

    Any help? Thank you.

    Pedro.

    Wednesday, June 24, 2015 2:46 PM

Answers

  • Hi pintope,

    After you call the VsShellUtilities.OpenDocument method, it will return an IVsWindowFrame instance to you, you could use this interface to close the window:

    IVsWindowFrame.CloseFrame Method

    You could also use DTE object to get the currently opened document or the document with paticular name or full path, for example:

    DTE dte = GetService(typeof(SDTE)) as DTE;
                //dte.ActiveDocument.Close();
                //dte.ActiveWindow.Close();
                int documentCount = dte.Documents.Count;
                for (int i = 1; i <= documentCount; i++)
                {
                    Document currentDocument = dte.Documents.Item(i);
                    if (currentDocument.Name.Contains("Program"))
                    {
                        currentDocument.Close();
                    }
                }

    See this article for more information:

    HOWTO: Getting information about Visual Studio windows from an add-in


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by pintope Thursday, June 25, 2015 5:41 AM
    Thursday, June 25, 2015 2:28 AM

All replies

  • Hi pintope,

    After you call the VsShellUtilities.OpenDocument method, it will return an IVsWindowFrame instance to you, you could use this interface to close the window:

    IVsWindowFrame.CloseFrame Method

    You could also use DTE object to get the currently opened document or the document with paticular name or full path, for example:

    DTE dte = GetService(typeof(SDTE)) as DTE;
                //dte.ActiveDocument.Close();
                //dte.ActiveWindow.Close();
                int documentCount = dte.Documents.Count;
                for (int i = 1; i <= documentCount; i++)
                {
                    Document currentDocument = dte.Documents.Item(i);
                    if (currentDocument.Name.Contains("Program"))
                    {
                        currentDocument.Close();
                    }
                }

    See this article for more information:

    HOWTO: Getting information about Visual Studio windows from an add-in


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by pintope Thursday, June 25, 2015 5:41 AM
    Thursday, June 25, 2015 2:28 AM
  • Thank you very much, Caillen, being aware of every corner of MS APIs is difficult, fortunately we have this forum :)
    • Edited by pintope Thursday, June 25, 2015 6:06 AM
    Thursday, June 25, 2015 5:48 AM