Share via

Change OneNote hyperlink color programmatically

Anonymous
2012-09-27T10:27:22+00:00

Hi all!

If I use the code below, the link's color will be blue:

var oneNoteApp = new Microsoft.Office.Interop.OneNote.Application();

string xml;

oneNoteApp.GetPageContent(OneNoteApp.Windows.CurrentWindow.CurrentPageId, out xml);

var currentPageDoc = XDocument.Parse(xml);

var nms = XNamespace.Get("http://schemas.microsoft.com/office/onenote/2010/onenote");

var textEl = new XElement(nms + "Outline",

new XElement(nms + "OEChildren",

new XElement(nms + "OE",

new XElement(nms + "T",

new XCData(

"text before <a href='http://google.com'><span style='color:black'>test link</span></a> text after"

)))));

currentPageDoc.Root.Add(textEl);

oneNoteApp.UpdatePageContent(currentPageDoc.ToString());

OneNote deletes the style attribute.

But if I change the link color via OneNote UI - it will work! And the page's xml will look like:

<![CDATA[ text before <a href="http://google.com"><span style='color:black'>test link</span></a> text after ]]>

So how can I set OneNote hyperlink color programmatically?

Thanks.

Microsoft 365 and Office | OneNote | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2012-09-27T17:09:47+00:00

Hi Alexander. Sorry that this isn't working as expected. I checked this and it is a bug in OneNote. I reported the issue to OneNote team. Thanks for reporting it.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Anonymous
    2012-09-28T06:52:19+00:00

    Thanks, Omer!

    The next workaround works:

    1. Add QuickStyleDef to the page with necessary font color.
    2. Set the "quickStyleIndex" attribute for hyperlink's parent element  (one:OE) to the value of QuickStyleDef.Attribute("index") that we have added to the page.
    3. Wrap all other text in that one:OE element with <span style='color:black'>{0}</span>
    4. Insert the empty one:T element before the hyperlink

    And the hyperlink text color will be like we defined it in QuickStyleDef.

    Uff. That was hard to find this workaround. Hope it will be fixed soon.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2014-11-07T05:54:40+00:00

    MM_100, OneNote team is investing in the new cloud based API set and the current desktop COM APIs are considered in support mode right now. Therefore unfortunately I don't expect bugs like these to be fixed in these APIs.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2014-11-05T06:03:47+00:00

    Hi Omer, I love your Onetastic work. So I just wanted to follow-up with this Onenote bug since it's been 2 years.

    If you do something like the following Onetastic script:

    For each Text in Current Page

      If property(hyperlink) is not equal to ""

        Modify property(fontSize) --> Set it to "20"

        Modify property(highlightColor) --> Set it to "green"

        Modify property(fontColor) --> Set it to "red"

        Modify property(italic) --> Set it to "true"

        Modify property(selected) --> Set it to "true"

      Else

        Modify property(selected) --> Set it to "false"

    It will find all hyperlinks and try to change size, color, highlight, italics and cause these links to be selected. However, upon Onenote.UpdatePageContent( ), the fontColor and italics styling will actually get stripped out. The size, highlights, and selection will remains At this point with all hyperlinks still selected, the user can manually change the italics and color using the UI.

    However, if at any point in the future Onenote.UpdatePageContent ( ) gets called again from this script, another Onetastic script, Onenote Spy, or some other external program, then all the links will once again lose their color and italics, that the user manually had changed (the hyperlink current size, highlight, and selection status will still remain unaffected)

    I guess my question is, why are certain style elements being stripped out after every UpdatePageContent call. While it is fine to strip these elements out when the casual user pastes content from the clipboard since RichText content (especially from the web) can have undesirable CSS styles. However, it is less adequate to strip these elements out when an external tool changes the current outline element. Power users tend to be the only ones that use external tools, and therefore, they shouldn't be subjected to the excess filtering the casual user might enjoy.

    Thanks for your hard work.

    Was this answer helpful?

    0 comments No comments