Opening a document in a specific editor from a ToolWindow

The doctor is back in the house after taking some well needed rest. Back to your Visual Studio Extensibility questions!

 

How do I open a document in a specific editor from a ToolWindow? I tried IVsUIShellOpenDocument::OpenSpecificEditor(...) but had some trouble. Can you help?

Sure! You found the right method to call if you’re a tool window that implements IVsUIHierarchy* (using VSIP of course), but it does take quite a few perplexing parameters. Let’s tackle them one by one:

  [C#] 
 public int OpenSpecificEditor ( 
    uintgrfOpenSpecific, 
    stringpszMkDocument, 
    ref System.Guid rguidEditorType, 
    stringpszPhysicalView, 
    ref System.Guid rguidLogicalView, 
    stringpszOwnerCaption, 
    Microsoft.VisualStudio.Shell.Interop.IVsUIHierarchy pHier, 
    uintitemid, 
    IntPtr punkDocDataExisting, 
    Microsoft.VisualStudio.OLE.Interop.IServiceProvider pSPHierContext, 
    out Microsoft.VisualStudio.Shell.Interop.IVsWindowFrame ppWindowFrame

);

The first argument, grfOpenSpecific, takes flags from the VSOSPEFLAGS enumeration. Usually, you don’t need these and can pass in 0.

Next, pszMkDocument is an identifier to the document that you want to edit. You can just pass in the extension to the document if it is a file. If not, you usually will want to pass in a URL.

Next, rGuidEditorType is the GUID of the editor factory you wish to open the document with.

Next, pszPhysicalView is the name of the physical view for the document. You can send in null in which case the environment will call MapLogicalView() on the editor factory to get the physical view.

Next, rguidLogicalView is the GUID for the logical view that you wish to use when opening the editor.

Next, pszOwnerCaption is the name of the caption that should appear on the editor tab.

Next, pHier is a pointer to the IVsUIHierarchy in the toolwindow that you’re opening this document from. *Note that you can only use OpenSpecificEditor if you implement the IVsUIHierarchy interface in your Tool window class. Otherwise, you will need to use the DTE.ItemOperations.OpenFile() method.

Next, itemid is a unique number that you should use to identify which item from the toolwindow is invoking the editor. This will get passed into the CreateEditorInstance method as the itemid argument there.

Next, punkDocDataExisting is a pointer to the IUnknown interface of the existing DocData object for the item that you’re editing. If you have a simple editor, and there is no existing data, simply pass in 0.

Next, pSPHierContext is a pointer to the service provider for the hierarchy that you’re calling from.

And last (but not least), editorFrame is an out parameter that will be a pointer to the IVsWindowFrame object of the newly created editor.