4

We are inserting content controls programmatically in word document and we don't want to add actions to the undo stack of word. The only way that we found is to access the undo commandbar-combobox and to remove the items related to inserting content control action, we are using the code below :

var commandBars = _wordDocument.CommandBars.Cast<CommandBar>();
var standardCommandBar = commandBars.First(bar => bar.Name.Equals("Standard"));
CommandBarComboBox undoControl = 
                     standardCommandBar.Controls
                     .Cast<CommandBarControl>()
                     .First(control => control.Id == 128) as CommandBarComboBox;

undoControl.RemoveItem(1);

The last line undoControl.RemoveItem(1) throws a ComException HRESULT E_FAIL.

Is there any way to remove actions from the undo redo combo box?

Magnus
  • 45,362
  • 8
  • 80
  • 118
DevTun
  • 865
  • 1
  • 8
  • 24

1 Answers1

-1

As a result of your question, I was having the same problem as you, but now it's solved.

  var commandBars = WordDoc.CommandBars.Cast<CommandBar>();
  var standardCommandBar = commandBars.First(bar => bar.Name.Equals("Standard"));
  CommandBarComboBox undoControl = standardCommandBar.Controls
                   .Cast<CommandBarControl>()
                   .First(control => control.Id == 128) as CommandBarComboBox;

Your code will return the last object from the undo list. Now you can add checks based on your requirements.

        if (undoControl.accName.ToLower().Contains("range.case"))
            WordApp.ActiveDocument.Undo(3);
        else if (undoControl.accName.ToLower().Contains("font.reset"))
            WordApp.ActiveDocument.Undo(2);

        else WordApp.ActiveDocument.Undo(1);

Microsoft Word

  • This does not solve the problem - it simply demonstrates how to selectively call "Undo(ref times)". See https://stackoverflow.com/a/40966385/1318024 for a thorough review of this requirement (changes without undo) and options (very limited). – user1318024 Sep 07 '22 at 18:06