MSHTML: clear the undo / redo buffer of MSHTML

To clear the undo stack of MSHTML control you can use undo manager service.
When enabling and disabling the undo service, the undo stack is cleared. To extract the undo manager out of the Document object of MSHTML you need to use the IServiceProvider.

The solution to this is some thing like:

    //Extract undo manager 
    if (m_undoManager == null) 
    {
      IServiceProvider serviceProvider = Document as IServiceProvider;
 
      Guid undoManagerGuid = typeof(IOleUndoManager).GUID;
      Guid undoManagerGuid2 = typeof(IOleUndoManager).GUID;
      IntPtr undoManagerPtr = ComSupport.NullIntPtr;
 
      int hr = serviceProvider.QueryService(ref undoManagerGuid2, ref undoManagerGuid, out undoManagerPtr);
      if ((hr == HRESULT.S_OK) && (undoManagerPtr != ComSupport.NullIntPtr))
      {
        m_undoManager = (IOleUndoManager)Marshal.GetObjectForIUnknown(undoManagerPtr);
        Marshal.Release(undoManagerPtr);
      }
    }
 
    //And to clear the stack 
    m_undoManager.Enable(true);
    Application.DoEvents();

More detailed implementation and more information can be seen at:

http://postxing.net:8080/PostXING/tags/v1.1/PostXING.HtmlComponent/Html/

http://msdn.microsoft.com/en-us/library/ms678623(VS.85).aspx