Prevent actions to be added to the word undo redo OR Remove actions from the undo redo CommandBarComboBox
Asked Answered
I

1

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?

Inspectorate answered 22/5, 2012 at 14:15 Comment(3)
I see you got answers at social.msdn.microsoft.com/Forums/en/vsto/thread/…Rhynd
No solution for this problem, the best that we can do is to clear the undo stackInspectorate
VERY, VERY SIMILAR QUESTION: #40829873Moses
U
-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

Underwriter answered 19/1, 2022 at 17:33 Comment(1)
This does not solve the problem - it simply demonstrates how to selectively call "Undo(ref times)". See https://mcmap.net/q/879736/-vsto-word-2016-squiggly-underline-without-affecting-undo for a thorough review of this requirement (changes without undo) and options (very limited).Doenitz

© 2022 - 2024 — McMap. All rights reserved.