6

I'm writing a simple VS add-in and would like to programmatically invoke the "Document Format" option (under Edit) within code. Google isn't being very friendly to me today....

BFree
  • 102,548
  • 21
  • 159
  • 201
  • Have you tried Resharper? It can reformat the whole solution for you in one go. (I assume this does not help but I must have tried... ;-) ) – Rashack May 01 '09 at 20:51
  • Nah, just trying to roll my own little tool to automatically close braces (yes, I know Resharper does that too, but where's the fun in that??) – BFree May 01 '09 at 20:56

3 Answers3

6
Command cmd = _applicationObject.Commands.Item("Edit.FormatDocument", -1);
object dummy = null;
_applicationObject.Commands.Raise(cmd.Guid, cmd.ID, ref dummy, ref dummy);
user95144
  • 1,347
  • 8
  • 7
  • As it is a first time to check it, i couldnt understand. Can u please specify type of the '_applicationObject' and its namespace.I browsed a little bit that i should use EnvDTE80 namespace but i couldnt proceed. Any codesnippet will be very much helpful. – Madhan Sekar Jun 28 '16 at 06:17
6

If you have a reference to your document (of type Window), and you have a reference to the _DTE object, you can call it like this:

myDocument.Activate();
myDTE.ExecuteCommand("Edit.FormatDocument", string.Empty);

Most of the time, you can get a reference to the _DTE object from the parameters passed into your add-in.

Jason Williams
  • 1,283
  • 2
  • 11
  • 31
0

You'll need to use the standard command editors, called with the VSStd2KCmdId.FORMATDOCUMENT command enumeration.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373