I'm working in Visual Studio 2008 and I would like for Edit > Outlining > Collapse to Definitions to be run whenever I open a file. It would be nice if, after that, all regions were expanded. I tried the code that Kyralessa offered in a comment on The Problem with Code Folding, and that works very nicely as a macro that I have to run manually. I tried to expand this macro to act as an event by placing the following code in the EnvironmentEvents module in the Macro IDE:
Public Sub documentEvents_DocumentOpened(ByVal Document As EnvDTE.Document) Handles DocumentEvents.DocumentOpened
Document.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
However, this does not seem to do anything when I open a file from my Solution in VS. To test that the macro was getting run, I put a MsgBox()
statement in that subroutine and noticed that code before Document.DTE.ExecuteCommand("Edit.CollapsetoDefinitions")
ran fine, but nothing seemed to get hit after that line. When I debugged and set a breakpoint within the subroutine, I would hit F10 to continue to the next line and control would leave the subroutine as soon as that ExecuteCommand
line ran. Despite this, that line seems to do nothing, i.e. it doesn't collapse the outlining.
I also tried using just DTE.ExecuteCommand("Edit.CollapsetoDefinitions")
within the subroutine but with no luck.
This question tries to obtain the same end result as this one, but I'm asking about what I might be doing wrong in my event-handling macro.