0

What is the preferred method to send a command to MS Word without using GUI automation to virtually click menus and buttons? I'm referring to commands that can be accessed via the GUI, such as setting font style, adding objects, adding WordArt, etc.

Obviously Word.Interop allows for this, but many of the commands available in the GUI are missing or needs a bit of code to get it working exactly like the GUI command. For example, the GUI has a command to copy selected text, but that needs a line of code :

ActiveDocument.ActiveWindow.Panes(1).Selection.Copy

I'm looking for a large command list that matches the list of commands that Word has:

Cmds

It should be something like : Word.SendCommand("AddShapeAfter");

Is there any such system?

Robin Rodricks
  • 110,798
  • 141
  • 398
  • 607
  • No, I think there is not such abbreviation. There would be three ways: (1) Standard macro, (2) Use WordBasic object (obsolete, but sometimes handy), or (3) Record macro (very easy, see [Record or run a macro](http://office.microsoft.com/en-001/word-help/record-or-run-a-macro-HA010099769.aspx#BM2) – Roman Plischke Jan 21 '14 at 09:45

1 Answers1

1

How about something like this? -

string command = "Copy";
bool flag= Globals.ThisAddIn.Application.CommandBars.GetEnabledMso(command);
if (flag)
{
    Globals.ThisAddIn.Application.ActiveDocument.CommandBars.ExecuteMso(command);
}
ShipOfTheseus
  • 234
  • 1
  • 10
  • Does this actually work? This is amazing!!!! Can you try an advanced command and ping back if it works? And how do you get the command codes? ie. knowing which string to pass to the function? Are these documented? – Robin Rodricks Sep 01 '14 at 17:57
  • I started a new question : http://stackoverflow.com/questions/25610998/are-the-command-codes-for-executemso-documented – Robin Rodricks Sep 01 '14 at 18:04
  • Ok, I think JasonPlutext already answered your next question before I got a chance in the other thread :). It works for most part yet. Just note that – ShipOfTheseus Sep 02 '14 at 08:18
  • 1
    that 1) It doesn't work for "gallery" as mentioned here [link](http://social.msdn.microsoft.com/Forums/vstudio/en-US/ea7b3593-44c1-4822-8b3e-d2092cc1fe52/slidethemesgallery-no-working-with-applicationcommandbarsexecutemso?forum=vsto) 2) Also, this is something relevant you shouldn't miss [link](http://social.msdn.microsoft.com/Forums/sqlserver/en-US/8df615fd-6297-437b-8de0-08a91fc1f581/commandbarsfindcontrolmsocontroltypemsocontrolbutton-436-null-null-dosent-open-chart-wizard?forum=vsto) – ShipOfTheseus Sep 02 '14 at 08:27