While working on a little WinRT app I recently spent WAY too much time trying to figure out why I was getting the following exception

System.UnauthorizedAccessException was unhandled by user code
  HResult=-2147024891
  Message=Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
  Source=Windows.UI
  StackTrace:
       at Windows.UI.Text.ITextDocument.SetText(TextSetOptions options, String value)

when all I was trying to do was programmatically set the text of a RichEditBox. EX:

theRichEditBox.Document.SetText(Windows.UI.Text.TextSetOptions.FormatRtf, textValue);

After searching and searching and eventually taking a walk to cool down, I decided that I would play with the IsReadOnly flag. I have it set to “True” in the XAML because I don’t want users to edit the text. I then wondered if this was causing the problem.

I tweaked the code as shown below and it magically started working. Notice how I’m turning off the IsReadOnly flag off, then setting the text, and returning the read only state after the text has changed.

theRichEditBox.IsReadOnly = false;
theRichEditBox.Document.SetText(Windows.UI.Text.TextSetOptions.FormatRtf, textValue);
theRichEditBox.IsReadOnly = true;

Dear Exception: you were not helpful. Sigh.

Previous post

Windows 8 Share Charm Data Inspector

Next post

Basic JavaScript: Prototypical Inheritance vs. Functional Inheritance