1

in making a MS Word add-in spellchecker, i'm faced with the issue of "underlines". I can use the formatting functions of the Word Object Model in order to color the misspelled words or to underline them just how a user would with the U button, but it's not the same as the native ms-word curly underline which doesn't show if you copy-paste the text elsewhere, doesn't get printed or saved and doesn't show in undo actions. I have been searching how to do it, but everyone keeps saying it's not possible.

There is this English spellchecker called Grammarly, they do have a ms-word add-in and they somehow underline the misspelled words (though it's not curly, just a straight thick line) and it works just like MS-Word:

a) it doesn't get copy-pasted when you move the text with the underlines elsewhere, nor does it gets saved when you save the document

b) it doesn't get added in the undo stack.

So from a) and b) I conclude that it's not text formatting

So there must be a way, I was wondering how can I achieve this.

Thank you in advance for any ideas or tips.

2
  • Stack Overflow is for debugging existing code, not for generating software ideas or requesting how-to articles. Commented Jan 6, 2021 at 2:08
  • 1
    @John Korchok - there is absolutely nothing wrong with the question. I wouldn't mind learning what the answer is as well. Commented Jan 6, 2021 at 3:21

2 Answers 2

4
+400

Unfortunately, MS Word doesn't provide out of the box functionality that would allow you to implement this. The way how Grammarly and other add-ons with similar functionality do it is they show the transparent window on top of MS Word window using Windows API. Then they identify coordinates of words that need to be highlighted and draw the highlighting (rectangles or underlines) inside of that transparent window on top of those words. This is not an easy solution because it requires to identify the visible range which is currently shown on the screen, find the words in that range that need to be highlighted and also track the user's actions such as scrolling and window resizing so that to redraw the highlighting after those actions. But for sure it's possible because we have recently done it in the product I'm working on. As far as I know, there is no ready open-source solution for this so you will need to implement everything yourself.

You may also consider a much easier solution which is highlighting the words using the Find.HitHighlight functionality. An example of how it works you can see when you search for a simple word in MS Word. Although this is not a very flexible solution because the only thing you can change is the color of the highlighting.

Sign up to request clarification or add additional context in comments.

3 Comments

That's interesting and difficult, thank for your input. Identifying the visible range of words, has been a difficult task for me too. Can be done easily if you use the Selection object, but that comes with difficulties of its own, since you see selection flickering on the screen.
You can find the coordinates of the active Word window. Then you can use Window.RangeFromPoint method to get the range.
Hey, @AlexeyAndrushkevich do you have some references, how can we use Windows API with the VSTO C# codebase? And which specific Windows API have you used to add the overlays in the MS WOrd Doc file?
1

I believe the way Grammarly adds emphasis to words and phrases it wants to flag is to change the document temporarily in a very smart and controlled way. Grammarly only works when one clicks the Open Grammarly button on the Grammarly ribbon. If you do a simple experiment, you may be able to observe how Grammarly controls its temporary changes.

First create an unsaved document with some misspelled words that trigger Microsoft Word to display the wavy red lines. Then click the Open Grammarly button. Notice that the add-in turns off spell checking and replaces Word's wavy red lines with its own web-triggered markings. These are probably generated in the document using code similar to:

myRange.Font.Underline = Microsoft.Office.Interop.Word.WdUnderline.wdUnderlineThick; myRange.Font.UnderlineColor = Microsoft.Office.Interop.Word.WdColor.wdColorRed; 

After observing this, save the document. If you look carefully at your screen (and if your computer is not super fast), you will see a small blink wherein the red thick underlines disappear for a brief moment. That is the Document.BeforeSave event removing the Grammarly markings before saving the document. By relying on user actions (such as when the user clicks the Grammarly ribbon button), Grammarly controls at what points in time the markings appear. It then uses code and events to make sure the markings are not saved with the document or interfere with other operations. The main events you would want to look at to replicate this behavior for your own spelling checker add-in would be:

Document.BeforeSave Event

Document.BeforePrint Event

Document.BeforeClose Event

2 Comments

yes. maybe they do it this way. I am not 100% convinced for two reasons: a) their highlighting doesn't get added to the undo actions array. If you do myRange.Font.Underline = you'll see a VBA.Formatting in Word's Undo button. and b) if you get their highlighted text and paste in another document, it's clean. Maybe there is some beforepaste event (though i can't seem to find one).
Agreed, the underline formatting is not how Grammarly replaced the spell checker markings (and the Word Undo List confirms this). I think @AlexyAndrushkevich is correct that the underlines are drawn. If you could see Grammarly's original code or decompile it, I am guessing System.Drawing would be used extensively. This, combined with Grammarly controlling functionality via events (as discussed in my answer), should give you a pretty good picture of how Grammerly works.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.