The folks over at Coding Horror gave an example of a Visual Studio macro for collapsing to definitions but expanding macros. The following was given by Kyralessa in a comment:
Sub CollapseToDefinitionsButExpandAllRegions()
DTE.ExecuteCommand("Edit.CollapsetoDefinitions")
DTE.SuppressUI = True
Dim objSelection As TextSelection = DTE.ActiveDocument.Selection
objSelection.StartOfDocument()
Do While objSelection.FindText("#region", vsFindOptions.vsFindOptionsMatchInHiddenText)
Loop
objSelection.StartOfDocument()
DTE.SuppressUI = False
End Sub
This code works fine when you execute it as a regular macro, but I would prefer it to happen automatically when I open a document.
I tried taking this code and working it into an EnvironmentEvent, but without much luck. When I try debugging the macro, the debugger gets to the ExecuteCommand line and then does not return to the subroutine. It also doesn't collapse to definitions (Edit > Outlining > Collapse to Definitions). Here's my EnvironmentEvent subroutine, for anyone to poke at:
Public Sub documentEvents_DocumentOpened(ByVal Document As EnvDTE.Document) Handles DocumentEvents.DocumentOpened
' Thanks to http://www.codinghorror.com/blog/archives/001147.html
Document.DTE.ExecuteCommand("Edit.CollapsetoDefinitions")
'DTE.ExecuteCommand("Edit.CollapsetoDefinitions")
DTE.SuppressUI = True
Dim objSelection As TextSelection = DTE.ActiveDocument.Selection
objSelection.StartOfDocument()
Do While objSelection.FindText("#region", vsFindOptions.vsFindOptionsMatchInHiddenText)
Loop
objSelection.StartOfDocument()
DTE.SuppressUI = False
End Sub
So beyond trying to offer some potentially useful region-expanding code, I'd also like to register that this is way too much pain to go through just to collapse methods but expand regions when opening a file. Really, VS? That can't just be a preference somewhere?