Carlos J. Quintero seems to know a lot about DTE. In an article updated in March of this year and published on the Web (http://www.mztools.com/articles/2007/mz2007027.aspx) he says that the Open method on an EnvDTE. ProjectItem returns an EnvDTE.Window, whose Document property can be cast to an EnvDTE.TextDocument.
But when I try this, I get an exception (HRESULT: 0x80004002 [E_NOINTERFACE]). It seems that the __ComObject returned by Open doesn't know about TextDocument:
An (extracted and slightly edited) excerpt from my VB .Net code (VS2008 running under Windows 7 Pro 64 in WOW):
...handler for BuildEvents.OnBuildBegin recursively traverses all items in all projects; filters names to find those containing ".Designer.vb" (apparently works fine to here). For each found, want to replace certain text; to do so, need TextDocument object:
'the below returns __ComObject instead of EnvDTE.Window
Dim ItemWindow as EnvDTE.Window = ProjectItem.Open(EnvDTE.Constants.vsext_vk_Code)
'the below throws exception
Dim ItemTextDocument as EnvDTE.TextDocument = CType(ItemWindow.Document, EnvDTE.TextDocument)
complete error:
Unable to cast COM object of type 'System.__ComObject' to interface type 'EnvDTE.TextDocument'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{CB218890-1382-472B-9118-782700C88115}' failed due to the following error: Interface wordt niet ondersteund (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
What is wrong? Any help appreciated.