ms-word object model: custom spellchecking underlines - ms-word

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.

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.

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

Related

Real time Code render mode in Visual Studio Code

For a half year now, I carry an innovative idea for a modern way of code editing. I just realized what Visual Studio Code extensions are already capable of and made me wonder.
The idea is that a real time code render mode for code editors where code is visualized like rendered documentation (for example Doxygen). Non-commented lines of code are displayed like collapsable codeblocks and documentation comments are displayed as the surrounding markdown/HTML/LaTeX document.
This rendered view still can be edited and navigated in same way as the classic text view so that new graphical elements appear after typing them as text into a comment. Therefore, rich interactive documentation reading (like hiding/showing parts, jumping across locations with hyperlinks) and code editing would be combined into one activity without time needed for switching between both or mapping Documentation with actual code locations. Different from the original literate programming idea, the file's physical content is unchanged and doesn't complicate the process or source code reading.
It's conceptually how Moodle text editors work (HTML code view and render view) or like using a graphical word processor to visualize source code instead of a single monospace text. Of course, the cursor would move along lines in their visually presented order and not in their file's actual order.
Do you know, if such a real time render mode is possible in VSCode?
Did someone had such an idea and made an extension?
btw:
This "render mode" idea is actually quite generic and could be used as general customizable GUI technology that would allow GUI elements to be navigated, edited via an underlying text representation.

Marking/commenting Matlab code?

Is there any way to mark Matlab code (in Matlab) via some kind of add-on? I'm looking for something similar to what Microsoft Word has with its "Review" mode where you can highlight a certain phrase/section and make a comment on the side. When I say "comment", I don't mean the regular code comments that you do with "%" in Matab.
I've googled for this, but I couldn't find anything. Anyone know if something like this exists that I may have missed? It would really help me when I'm reading someone else's code and I have to keep track of things / make comments for certain lines. Even if commenting isn't possible, it would be nice to be able to highlight certain lines via some kind of "highlighter" like the one in Word.
No. This isn't possible.
In fact, most editors and IDEs (if not ALL of them) that I know don't even support this feature as each IDE will have to be specific in the way this is implemented (à la Trojanian). The only thing that I can think of is if you print out the code in PDF, then use bubbles within the PDF viewer to tack on your comments.
However, if you want to highlight a specific line in your editor, you'll have to set this up in your settings. Go to preferences, then in the Editor / Debugger options, choose the Display option, then choose Highlight Current Line. Choose whichever colour you see fit. I'm running MATLAB R2013a on Mac OS, and this is the window I get:
You'll see the Highlight Current Line feature. Change that to whichever colour you want. I have it as gray because I do like having the current line highlighted.

How to show code snippets in MS Word document as it shows in StackOverflow (scrollbars and gray background)

I am writing a document which intends to show code changes made in C# project, I am looking for a good format to show code snippet.
I like is the way StackOverflow site shows code snippet in its Questions and Answers (scrollbars and background color).
Can anybody guide me to create similar background/view to show code snippet in word document (mainly scrollbars).
I tried copying and pasting it from StackOverflow but the background does not appear.
I search and found this useful link but it does not change background color and don't have scroll bars How do you display code snippets in MS Word preserving format and syntax highlighting?
Here is what I did, first the results:
Go to stackoverflow copy some code, paste it in the word doc, you will get this gray box and etc.
Mark you text and go to:
Then you just write your code in any IDE, and past it into the word doc, mark it and set the style to code. :)
I could not comment on most voted answer for #AeroVTP, my rep is too low. But when you create a style, you can modify it by right clicking and then Modify..
Then on bottom left, click on Format -> Border. Then on tab Shading you can select whatever color you want as a background. Hope this clears things up :)
In Microsoft Word setup a text box (Insert > textbox) select the "format" tab, go to "fill" and choose the background color, set up a grey background, then use a monospaced font and here is your code-like text!
Here is a link to a Microsoft support page that can drive you better if you need: http://office.microsoft.com/en-gb/word-help/make-the-background-of-a-text-box-invisible-HA010337242.aspx?CTT=1
Fro scrollbars is a little more complicated, but here is someone who found a solution:
http://answers.microsoft.com/en-us/office/forum/office_2007-word/i-need-a-textbox-with-a-scroll-bar-in-my-word-form/534ced3f-f202-46b1-a4b4-3ecdabaa6899
From there you can find a lot of usefull guides for text formatting and general MS Word use.
Just insert a 1x1 table, style it as you want and paste the code from notepad++ (NppExport->Copy all formats to clipboard). Easiest solution.
Even though, it would be best if we write to Bill that he has to put some code styler for us developers in need to document our code via Microsoft word :(

Is there a way to mark specific lines in BBEdit and then iterate through them?

I often find myself jumping between several parts of a lengthy document, and I've found no streamlined way of handling this. I've previously used TextPad for PC, and it had a great feature called Bookmarks:
Press a keystroke, and the current line is marked. The bookmark shows as an arrow in the gutter.
The same key toggles the marker off again.
Another keystroke sends the caret down to the next bookmark.
Yet another keystroke sends the caret up to the previous bookmark.
Is there anything like this in BBEdit? I loved that feature of Textpad, but I haven't found another editor that has something similar. Currently I use the awkward substitute of typing three slashes /// at a place I want to jump back to, then using Ctrl+F to re-find them, but that's a lot of keystrokes to set/remove markers and to iterate through them.
Thanks!
I think what you are after is BBEdit Markers.
This article is a good place to start: macdrifter.com/2012/01/bbedit-markers.html

What is the best way to insert source code examples into a Microsoft Word document?

I have to write some documents that will include source code examples. Some of the examples will be written from the IDE, and others would be written in place. My examples are primarily in Java.
As someone who is used to LaTeX, doing this in Word is extremely painful. However, I am bound to Word for this. The only options I have seen are:
Write or copy into the document,
then use a fixed type font, arrange
formatting and hope that Word didn't
uppercase stuff for you. Needless to
say, this looks like crap.
Copy and paste screenshots of source
code from the IDE. At least I keep
colors. However, if I change my font
size, I'm screwed. I'm also screwed
across page boundaries. And let's
admit it, Word is not great at
managing multiple images on a
document.
Write HTML (not really an option
here)
Is there some better (and ideally portable) way to do this? Is there at least some sort of verbatim style similar to the LaTeX environment? Is there at least some pretty printer that I could copy-and-paste as RTF?
I absolutely hate and despise working for free for Microsoft, given how after all those billions of dollars they STILL do not to have proper guides about stuff like this with screenshots on their damn website.
Anyways, here is a quick guide in Word 2010, using Notepad++ for syntax coloring, and a TextBox which can be captioned:
Choose Insert / Text Box / Simple Text Box
A default text box is inserted
Switch to NPP, choose the language for syntax coloring of your code, go to Plugins / NPPExport / Copy RTF to clipboard
Switch back to word, and paste into the text box - it may be too small ...
... so you may have to change its size
Having selected the text box, right-click on it, then choose Insert Caption ...
In the Caption menu, if you don't have one already, click New Label, and set the new label to "Code", click OK ...
... then in the Caption dialog, switch the label to Code, and hit OK
Finally, type your caption in the newly created caption box
I recently came across this post and found some useful hints. However, I ended up using an entirely different approach which suited my needs. I am sharing the approach and my reasoning of why I chose this approach. The post is longer than I would have liked, but I believe screenshots are always helpful. Hopefully, the answer would be useful to someone.
My requirements were the following:
Add code snippets to a word document, with syntax highlighting for easier visibility and differentiation of code and other text.
Code snippet shall be inline with other text.
Code snippet shall break across pages smoothly without any extra effort.
Code snippet shall have a nice border.
Code snippet shall have spell-check disabled.
My Approach is as listed below:
Use external tool to achieve syntax highlighting requirement 1 above.
One could use notepad plus plus as described above. However, I use the tool present here - https://syntax-highlighter.k26.ch/. This gives me the option to use line number, as well as very nice syntax highlighting (Please use Google Chrome for this step, because syntax highlight is not copied when using Mozilla Firefox, as also pointed out by couple of user comments). Steps to achieve syntax highlighting are listed below:
Open the website provided above in chrome and Copy the code snippet in the text area. I will be using a sample XML to demonstrate this (XML sample from here - http://www.service-architecture.com/articles/object-oriented-databases/xml_file_for_complex_data.html).
Select the language from drop down menu.
Click "Show Highlighted" button. It will open a new tab, with syntax-highlighted code snippet, in this case the XML sample we chose. See image below for example.
To Turn off the line numbers, inspect the page in chrome. Then, under styles, deselect the "margin" property in ".dp-highlighter ol", as shown in the image below. If you want to keep the line numbers, go to next step.
Select the syntax-highlighted code and click copy. Now your code is ready to be pasted into Microsoft word.
Thanks to this blog for providing this information - http://idratherbewriting.com/2013/04/04/adding-syntax-highlighting-to-code-examples-online-and-in-microsoft-word/.
2. To achieve requirements 2, 3 and 4 above, use table in Microsoft word, to insert the code snippet. Steps are listed below:
Insert a table with single column.
Paste the copied text from step 1. in the table column. I have kept the line numbers to show how well this works with Microsoft word.
Apply border, as you like. I have used size 1pt. Resulting Microsoft word snippet will appear as shown in screenshot below. Note how nicely it breaks across the page - NO extra effort needed to manage this, which you would face if inserting "OpenDocument Text" object or if using "Simple TextBox".
To achieve requirement 5, follow the steps below:
Select the entire table or the text.
Go to Review tab. Under Language, choose "Proofing Language". A new pop-up will be presented.
Select "Do not check spelling or grammar". Then, click OK.
Resulting text has spell-check disabled. Final result is shown in the image below and meets all the requirements.
Please provide if you have any feedback or improvements or run into any issues with the approach.
You need to define a style in your Word document and use that for source code. I usually have a style called "Code" which has a monospaced font in a small point size, fixed size tabs, single line spacing, no before/after paragraph spacing, etc. You only need to define this style once and then reuse it. You paste in your source code and apply the "Code" style to it.
Note that some editors (e.g. Xcode on the Mac) add RTF as well as text to the clipboard when copying/pasting between applications - Word recognises RTF and helpfully retains the formatting, syntax colouring, etc.
Source code in Xcode:
Copied and pasted to Word:
(Note: it's a good idea to disable spell-checking in your "Code" style in Word.)
It kind of depends on the IDE. Both Visual Studio and Eclipse, for example, will allow you to copy as RTF and paste into Word, keeping all your formatting.
Notepad++ has a plugin called "NppExport" (comes pre-installed) that allows you to copy to RTF, though I don't care much for Notepad++'s syntax highlighting (it'd definitely be passable though). What it does do is support dozens of languages, whereas the aforementioned IDEs are limited to a handful each (without other plug-ins).
Use this - http://hilite.me/
hilite.me converts your code snippets into pretty-printed HTML format, easily embeddable into blog posts, emails and websites.
How:
Just copy the source code to the left pane, select the language and the color scheme, and click "Highlight!". The HTML from the right pane can now be pasted to your blog or email, no external CSS or Javascript files are required.
For Microsoft Word document: Copy the the content from the Preview section and paste to your Microsoft Word document.
3 sections : Source Code , HTML and Preview
These answers look outdated and quite tedious compared to the web add-in solution; which is available for products since Office 2013.
I'm using Easy Code Formatter, which allows you to codify the text in-place. It also gives you line-numbering options, highlighting, different styles and the styles are open sourced here: https://github.com/armhil/easy-code-formatter-styles so you could extend the styling yourself. To install - open Microsoft Word, go to Insert Tab / click "Get Add-ins" and search for "Easy Code Formatter"
This is related to this answer: https://stackoverflow.com/a/2653406/931265
Creating an object solved all of my problems.
Insert > Object > Opendocument Text
This will open a document window, paste your text, format it how you want, and close it.
The result is a figure. Right click the object, and select 'add a caption'.
You can now make cross references, create a table of figures.
If you are still looking for an simple way to add code snippets.
you can easily go to [Insert] > [Object] > [Opendocument Text] > paste your code > Save and Close.
You could also put this into a macro and add it to your easy access bar.
notes:
This will only take up to one page of code.
Your Code will not be autocorrected.
You can only interact with it by double-clicking it.
On a Mac I find this solution with vim to be wonderful:
https://github.com/zerowidth/vim-copy-as-rtf
There is an easy way if you want simple code formatting.
Open word> Insert tab> click on "Get Add-ins"
search for "Content mixer"
3.click on "Add"
Then content mixer add will open automatically and you can copy paste your code in there and click on "Insert" to insert it in word doc.
You can use Open Xml Sdk for this. If you have the code in html with color and formatting. You can use altchunks to add it to the word documents.
Refer this post Add HTML String to OpenXML (*.docx) Document
Hope this helps!
This is what i did.
End results :
https://stackoverflow.com/a/25092977/1161594
You can using Plugin Syntax Highlight in Ms.Word https://store.office.com/syntax-highlighter-WA104315019.aspx?assetid=WA104315019 . i follow that step and it's work
In Word, it is possible to paste code that uses color to differentiate comments from code using "Paste Keep Source Formatting." However, if you use the pasted code to create a new style, Word automatically strips the color coded text and changes them to be black (or whatever the auto default color is). Since applying a style is the best way to ensure compliance with document format requirements, Word is not very useful for documenting software programs. Unfortunately, I don't recall Open Office being any better.
The best work-around is to use the default simple text box.
I have tried all your methods, but they didn't work for me, in fact I have created an easier method using MS Word Tables.
Pros:
More beautiful
Easier to manage & more consistent
Are less prone to problems
No need for external plugins or MS Word micro coding.
Easier to handle by simple users (such as myself).
Cons:
It will not maintain code colouring although someone could improve my trick.
Steps:
Insert a 3x3 table, in my case I always make the total width of the table equals the free page's width (3 rows minimum to test the tables style).
Use invisible borders ("No Borders" option), and activate "View Gridlines" option. it should have this aspect.
Be ware that those lines are for you to see the table's grid, and the will not be printed.
Make the adjustments to cells' spacing and columns' width to get the aspect you like. (You will have to get in "Table Properties" for fine tuning).
Create a "Paragraph Style" with the name of "Code" just for your code snippets (check https://stackoverflow.com/a/25092977/8533804 to get the idea, you don't have to follow all of it)
Create another "Paragraph Style" with the name of "Code_numberline" that will be based upon the previous created style.
In the newly created "Code_numberline" add the numbering style that you like (this will automate line numbering).
Apply "Code_numberline" to the first column, and "Code" to the 3 column.
Add a fill in the middle column.
Save that table style and enjoy!
So I've found the majority of answers on this question to either be only semi-functional or broken entirely (eg the website used is down). This solution is entirely self-contained in Word, works across multiple pages, and keeps your syntax highlighting.
Here's the steps:
Temporarily set your IDE's theme to a light one, so that the text will fit in with the rest of your document.
Copy the required code (HTML colourising info will be copied with it).
In Word, hit Ctrl+Alt+V and choose HTML Format to paste the coloured text.
Select all your text and then expand the styles gallery on the home tab of the ribbon.
Choose "Create a style". Use a name such as "Code"
Right click your new style in the gallery and choose "Modify".
Under the formatting section, change the font to Consolas or your choice of monospace font. Also set the line spacing to Condensed (this option is next to the text alignment options).
Under the format button in the bottom left, choose "Language", then enable the "Don't check spelling or grammar" option.
If you want this style to be available in future documents, change to the "New documents based on this template" radio button.
Optional: for line numbers...
Back in the Modify Style dialogue, choose the format button again and choose "Numbering".
Choose the "Define new number format" option.
Remove the dot after the number '1' in the number format box.
Choose "Right" as the number alignment.
Hit ok, then select your new number alignment option. Finally, hit ok on the Modify Style dialog.
Finally, select your code, and set it to be the Code style, which should apply all the required formatting.